cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 11

PowerShell/PowerCLI Monitor Problem

I've just installed PowerCLI (VMWare PowerShell integration) and successfully created a PowerShell script to connect to an ESX server (and verified it works).  I've also tried it from the command line without issues.

The issue is when I try to use this as an APM component it gives an error:

 

Add-PSSnapin : The type initializer for 'VMware.VimAutomation.Sdk.Interop.V1.CoreServiceFactory' threw an exception. 
At line:1 char:13 
+ Add-PSSnapin <<<< -Name VMware.VimAutomation.Core 
+ CategoryInfo : NotSpecified: (:) [Add-PSSnapin], TypeInitializationException 
+ FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.Commands.AddPSSnapinCommand 

All the script has is:
Add-PSSnapin -Name VMware.VimAutomation.Core 
I'm setting the script to run against the local Orion server since that is where PowerCLI is installed.  If I remove that line the script runs but complains about the VMWare cmdlet not being known (which is expected if I'm not including the snap-in).  
Any reason why adding the snap-in fails?  I've even run "Get-PSSnapin -registered" (from APM PowerShell component) and it shows VMware.VimAutomation.Core as registered.
7 Replies
Level 12

Hello,

Using "Remote Mode" I was able to run the script containing "Add-PSSnapin -Name VMware.VimAutomation.Core" command without any error (I had to set some WinRM settings to be able to executes the remote scripts in PowerShell).

But when I tried to run some real script to get some information from VMware ESX server, I got "Exception of type 'System.OutOfMemoryException' was thrown." error when Connect-VIServer command is executed.

When I execute the script in PowerShell console I see that memory consumption of poweshell.exe process grows up to 450-500 MB (see screenshot). For that reason we probably aren't able to run this script in our SAM PowerShell monitor (that is executed in JobEngineWorker.exe process together with another polling jobs).

PowerShellMemory.png

The using of VB script as wrapper is good workaround - the VB script solves the issue with importing VMware snapin and also executes the script in separate powershell.exe process.

Kind Regards,

Lukas Belza (SolarWinds development)

0 Kudos
Level 12

Hello,

Are you running your PowerShell script in "Local Host" or "Remote Host" mode? When you run PowerShell script in "Local Host" execution mode in APM, the user is NT AUTHORITY\SYSTEM (and maybe this user can't add snap-in correctly).

Please try to switch to "Remote Host" and assign the administrator credential to the monitor.

 

Kind Regards,

Lukas Belza (SolarWinds development)

0 Kudos

It's an ESX server, there are no remote credentials to run the script.

The script needs to be run locally.  I'll have a dig around and see if there are permission issues.

0 Kudos



It's an ESX server, there are no remote credentials to run the script.

The script needs to be run locally.  I'll have a dig around and see if there are permission issues.



 

I believe what lukas is saying is that under local user mode the script executes under the local "system" account. In remote mode the script uses the account specified under the credentials section of that component monitor. It may be that the "system" account doesn't have adequate permissions to properly execute but the local or domain user account you use to log into the machine does.

0 Kudos

Hello,

In APM we can use another approach to run PowerShell script - using Windows VBscript monitor and running the local script. That should solve your issue. Please create the application with "Windows Script Monitor" and place following script:

fileName = "C:\PowerShellScript.ps1"
Set objShell = CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    + "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
    + strComputer + "\root\cimv2")
Set colSettings = objWMIService.ExecQuery _
    ("SELECT Architecture FROM Win32_Processor")
For Each objProcessor In colSettings
    If objProcessor.Architecture = 9 Then
                      powerpath = "%windir%\sysnative"
                        Exit For
    ElseIf objProcessor.Architecture = 0 Then
                      powerpath = "c:\windows\system32"
                        Exit For
    End If
Next
Set vPSscrIpt = objShell.Exec(powerpath + "\windowspowershell\v1.0\powershell.exe " + fileName)
vPSscrIpt.StdIn.Close()
Do While vPSscrIpt.Status = 0
    WScript.Sleep 100
Loop
WScript.Echo(vPSscrIpt.StdOut.ReadAll)
WScript.Quit vPSscrIpt.ExitCode

The first line specifies the path to PowerShell script, then we find the path of PowerShell.exe. Then we run the PowerShell script, wait for finish of script execution and the script output and exit code from PowerShell are passed to VBscript.

Please let me know if this solution works for you.

 

Thanks,

Lukas Belza (SolarWinds development)

Hey all, I have been trying to get the VB wrapper script working with SAM and can't for the life of me get the returned value back to the SAM Windows Script Monitor.  I confirmed that the powershell script works on the local server, I confirmed that the VB wrapper that calls the powershell script works locally on the server as well.  The WScript Echo does return the "Statistic: #" value that I'm looking for.  However, when running this in SAM and testing with the get output I get an error that the predefined columns are not present...essentially meaning that the Statistic: # is not being received properly by the script to the monitor.

Any ideas?

0 Kudos

Could you post the script here, as well as the resulting output of the script? It may be something simple, such as your exit codes not being correct, or that your statistic/message values are improperly formatted.

0 Kudos