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.