Commvault / CVPowershellSDK

Developer SDK - Powershell
12 stars 12 forks source link

Automating the use of the powershell scripts using Telegraf #3

Closed bogski87 closed 4 years ago

bogski87 commented 4 years ago

Hi, firstly, nice scripts. They work great. I have one issue though and I'm hoping someone can help.

I need to run the powershell commands from these scripts using the Telegraf data collection agent (from influx data). This means the data needs to be in the right format. Unfortunately (for me) this causing an issue. When the script runs in the background, it connects to my commvault server no problem but then outputs "Commserve login successful" and a few other bits (server, name, user and port).

When telegraf runs the script and connects, the first piece of data that comes back is that message which isn't valid line protocol resulting in none of the data being written to my database.

I've tried commenting out and completely removing the section that does this (although i suspect it isn;t the recommended option) - Is there a way to easily omit that connection message? Essentially I only need to get job information and send that to the database. I don't want to start chopping out bits of code in case i break it!

Any suggestions on a graceful way to handle this? or is it possible to access the values in this block

$response = (GetSessionToken $Server $User $Password $Port)

            if ($response.IsValid) {
                Write-Host 'CommServe login successful'
                $global:CVConnectionPool = @{
                    server = $Server
                    token  = $response.Content.token
                    user   = $User
                    port   = $Port
                }

                $global:CVConnectionPool.GetEnumerator() | Where-Object -FilterScript {
                    $_.name -notmatch 'token' | Out-Null
                }

from another script? IF i can pass $response to my powershell script i can format or remove the data i don't need.

Thanks

gstoops commented 4 years ago

Hi bogski87, thank you for the kind words on the scripts. I have put a lot of work into them!

Please try replacing that process block in Commvault.RESTSession.psm1 with this:

process { Write-Debug -Message "$($MyInvocation.MyCommand): process"

    try {
        $response = (GetSessionToken $Server $User $Password $Port)

        if ($response.IsValid) {
            Write-Verbose 'CommServe login successful'
            $global:CVConnectionPool = @{
                server = $Server
                token  = $response.Content.token
                user   = $User
                port   = $Port
            }

            $global:CVConnectionPool.GetEnumerator() | Where-Object -FilterScript {
                $_.name -notmatch 'token' | Out-Null
            }

            ((Get-Variable -Scope Global CVConnectionPool).Value | Format-Table) | Write-Verbose
        }
        else {
            Write-Host 'CommServe login failed'
            Write-Host $response.Content
            if (HasProperty $response.Content 'errList') {
                Write-Host $response.Content.errList[0]
            }
        }
    }
    catch {
        $PSCmdlet.ThrowTerminatingError($_) 
    }
}
bogski87 commented 4 years ago

Hi @gstoops thanks coming back to me on this. You've done a good job with scripts, I was hesitant about where to begin writing something like this (more novice scripter than anything else), these scripts are a life and time saver!

I found in the cvps.ps1 file i could comment out the write-host sections which helped but I'd rather use the proper or recommended option. Always feels hacky just chopping bits out and seeing what happens.

I'll replace the current process block with the one above and report back tomorrow. Thanks again!

bogski87 commented 4 years ago

Hi,

It took longer than expected to get round to this, but the updated code has worked :) - There were a couple of issues getting it running but that was down to the machine i was working from.

Thank you.

PhilB

gstoops commented 4 years ago

Hi, glad to hear suppression of the successful login output solved your issue. I will be updating the package to use the Write-Verbose vs. Write-Host in this case (Connect-CVServer) shortly. To see the explicit login result output, pass the -Verbose switch: Connect-CVServer -Verbose.