Applications Systems
PowerShell script that uses VMware PowerCLI functions in SAM
When you try to run PowerShell script in SAM that uses VMware.VimAutomation.Core snapin, you receive following error: "Add-PSSnapin : The type initializer for 'VMware.VimAutomation.Sdk.Interop.V1.CoreServiceFactory' threw an exception. At line:31 char:13 + add-pssnapin <<<< VMware.VimAutomation.Core + CategoryInfo : NotSpecified: (:) [Add-PSSnapin], TypeInitializationException + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.Commands.AddPSSnapinCommand"
First published date
Last published date
Overview
When you try to run PowerShell script in SAM that uses VMware.VimAutomation.Core snapin, you receive the following error message:
"Add-PSSnapin : The type initializer for 'VMware.VimAutomation.Sdk.Interop.V1.CoreServiceFactory' threw an exception. At line:31 char:13 + add-pssnapin <<<< VMware.VimAutomation.Core + CategoryInfo : NotSpecified: (:) [Add-PSSnapin], TypeInitializationException + FullyQualifiedErrorId : System.TypeInitializationException,Microsoft.PowerShell.Commands.AddPSSnapinCommand"
Product section
Resolution
It is possible to configure the PoweShell Script Monitor in SAM to correctly load this snapin (using Remote Mode and other advanced settings.) However, connecting to the VMware server using the Connect-VIServer command consumes memory (up to 400 MB) and the polling job fails with an “Out Of Memory” exception. It is not practical to run this type of script in SAM.
Workaround: Use the Windows Script Monitor with a Visual Basic script as a wrapper for the PowerShell script.
- Place you PowerShell script in a folder where everyone can access it (e.g. C:\Windows\Temp\MyVMwareScript.ps1)
- Create the Windows Script Monitor with following script body:
fileName = "C:\Windows\Temp\MyVMwareScript.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 PowerShell script is now executed in an external poweshell.exe process (not in SAM's JobEngineWorker.exe process) and the text output with an exit code is passed to SAM through the outer VB script.
Note: Only exit codes 0 (Up) and 1 (Down) are allowed in this case.
*** PLEASE NOTE ***
SolarWinds support does not support custom scripting. Please use the above information at your own discretion.