This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

Alert Script - PowerShell Unable to Load SolarWinds Module?

So I have a simple PowerShell script that does a couple simple things. I'll Include it below but essentially it attempts to unmanage a node via the API given the NodeID from the Alert. I also have the output going to a log file.
Here is my Problem: It won't unmanage. Now I have the Alerting Engine and Module Engine services running as a user that has Admin rights on the machine.
If I run the script manually on the machine it works.
If I log in as the User i have the Service running as, It Works.
But When i Trigger it from the Alert, It fails.
My various logs that are generated have indicated that the error is :"The term 'Invoke-SwisVerb' is not recognized as the name of a cmdlet..." So It seems that it is able to run the script, however unable to load the Module when the Alert Triggers. Again if I log in as the user on the Windows Machine, I can run the script no problem.
Its a headscratcher for me so I'm hoping someone has an idea?





--------------------------------------PS SCRIPT-------------------------------------

# Specify required parameters

[CmdletBinding()]

Param(   

    [Parameter(Mandatory=$True,  Position=1)][String]$NodeID   

    );

function Main()

{

$OutFilePath = "C:\SolarWinds_Scripts\ScriptTest.txt"

    #Attempt to load the Swis Powershell Module. If failure, Run Funtion to download/install.

    Try

    {

       # Out-file $OutFilePath -Append -InputObject "Modules: $(Get-Module) "

       

         $importResults = $(Import-Module SwisPowerShell -Verbose) 4>&1

         Add-PSSnapin SwisSnapin -ErrorAction SilentlyContinue

        # Out-file $OutFilePath -Append -InputObject $(Get-PSSnapin)

         Out-file $OutFilePath -append -InputObject "$(Get-TimeStamp) - SW Import Test Success? - $importResults"

    }

    Catch

    {

        SWModule

        $Errormsg1 = $_.Exception.Message

        Out-file $OutFilePath -append -InputObject "$(Get-TimeStamp) - SW Import Test Failure - $NodeID - $Errormsg1"

    }

     # SolarWinds user name and password section. Create an Orion local account that only has node management rights. Enter the user name and password here.

    $username = "SWLocalAccountW/UnmanageRights"

    $password = "*************"

    $Hostname

    # This section allows the password to be embedded in this script. Without it the script will not work.

    $swis = Connect-Swis -UserName $username -Password $password -Hostname $Hostname

    #$swis = Connect-Swis -Trusted

    Try

    {

      

        $SucResult = Invoke-SwisVerb $swis Orion.Nodes Remanage @("N: $NodeID")

       

        Out-file $OutFilePath -append -InputObject "$(Get-TimeStamp) - SW Script Test Success - $NodeID"

        Exit 0

    }

    Catch

    {

    $user =[Environment]::UserName

    $Errormsg = $_.Exception.Message

     Out-file $OutFilePath -append -InputObject "$(Get-TimeStamp) - SW Script Test Failure - $NodeID - $Errormsg"

    

        Exit 1

    }

}

function Get-TimeStamp {

   

    return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)

   

}

#Function to search for and install the Swis Powershell Module.

Function SWModule

{

if (Get-Module -ListAvailable -Name SwisPowershell) {

    Write-Host "Module exists"

}

Else

    {

        Try

        {

            Install-Module -Name SwisPowerShell

        }

        Catch

        {

            Write-Output "Can Not Install Swis Powershell"

            Exit 2

        }

    }

}

Main;



-----------------------------ScriptTest.Txt Output (with Comments)-----------------------------------------------

##Ran as User, Via CMD##

[08/03/18 16:21:16] - SW Import Test Success? - Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\SwisPowerShell\2.3.0.108\SwisPowerShell.psd1'. Importing cmdlet 'Connect-Swis'. Importing cmdlet 'Get-SwisData'. Importing cmdlet 'Get-SwisObject'. Importing cmdlet 'Invoke-SwisVerb'. Importing cmdlet 'New-SwisObject'. Importing cmdlet 'Remove-SwisObject'. Importing cmdlet 'Set-SwisObject'.

[08/03/18 16:21:17] - SW Script Test Success - 299

##Ran as User, Via Alert##

[08/03/18 16:21:45] - SW Import Test Success? -

[08/03/18 16:21:45] - SW Script Test Failure - 299 - The term 'Invoke-SwisVerb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

  • Hi rfackrell​, were you able to resolve this? My case is kinda the same, I have a Powershell module that loads fine in the CLI, but when I use it as a script for SAM, error returns that no valid module file was found.

  • Not Really.
    I couldn't find a reason behind this so Instead I now have the alert call a PowerShell script that uses the Curl Commands instead. The big disadvantage to this work around however is that the Curl command need credentials to be defined where the SWIS calls I could use the Trusted. Works for now but i'm still investigating this one.
    Actually,  KMSigma​ Have you ever ran into this?

  • I didn't have this problem with alerts, but I've also changed over to using the module (PowerOrion) and not the snap-in.  The big thing with that, is that you need to install it to the global space, not into any specific users' space, so it requires the parameter "-Scape AllUsers" [Install-Module -Name PowerOrion -Scope AllUsers]

    I've written scripts before that trigger after an action.  I save the scripts to the local file system and call them via execute an external program and using:

    "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -File "C:\Path\To\Script.ps1"

    The execution policy is inherited from the operating system (or GPO).

  • I have seen the module not being loaded for a SAM Template, but the execution mechanism for SAM PowerShell is different than the Alert engine.

  • Hi Kevin, so I wouldn't be able to load the module through a SAM powershell template at all?

  • Yes and no.  Some would load others would not.  For example, I never got the MSOnline module to load.  Not sure exactly why, but it was a "one-off" situation that I was trying to handle, so I didn't stress, I just found another way to get the data.