Using Powershell commands in WhatsUp ActiveScripts


Sometimes it’s useful to be able to run something other than jscript or VBscript. For instance VMware offer a powershell interface from which you can collect historical statistics.

In this example I’m querying the average value for the Cpu.Ready.Summation statistic for a particular VM over the last minute rounded up to 2 decimal places using Math::Round(returned value,2):

[Math]::Round(((get-vm -Name " & strComputer & " | Get-Stat -Stat Cpu.Ready.Summation -Start (get-date).addMinutes(-1) -Finish (get-Date) | Measure-Object -Property Value -Average).Average)/200,2) 

The script uses the ActiveXPosh from SAPIEN which is a free COM component. You can download it from here. You’ll need to install it on the same machine on which you’re running the code, which in this case is the WhatsUp server.

To use the VMware powershell commands we also need to add the snap-in (Add-PSSnapin VMware.VimAutomation.Core), so you’ll also need to have powershell and VMware powershell installed on the WhatsUp server.

' This is the VMware vcenter server strVMwareServer="MyVCServer.MyDomain.com" 
' Get the windows credentials for the target VM in WhatsUp 
strUsername = Context.GetProperty("CredWindows:DomainAndUserid") 
strPassword = Context.GetProperty("CredWindows:Password") 
Const OUTPUT_CONSOLE = 0 
Const OUTPUT_WINDOW = 1 
Const OUTPUT_BUFFER = 2 

'Grab the target computer name from the DeviceID 
strComputer=getDisplayNamefromID(context.getproperty("DeviceID")) 
context.logmessage "Target VM Name is " & strComputer 
context.logmessage "Logging in as " & strUsername 

'This is the Powershell Command to send 
strPowerShellCommand="[Math]::Round(((get-vm -Name " & strComputer & " | Get-Stat -Stat Cpu.Ready.Summation -Start (get-date).addMinutes(-1) -Finish (get-Date) | Measure-Object -Property Value -Average).Average)/200,2)" 
context.logmessage strPowerShellCommand 

'Create a new power shell object 
set powershell = new class_powershell 
context.logmessage "Fetching statistics from " & strVMwareServer 
powershell.execute strVMwareServer,strUsername,strPassword,strPowerShellCommand 

for each line in powershell.output 
 context.logmessage cdbl(line) 
next 

context.logmessage "Done." 

class class_powershell 
 private p_array_commandoutput 
 private p_sessionid 
 public property get output 
 set output = p_array_commandoutput 
 end property 

 public sub init 
 end sub 

 public sub clear 
 ActiveXPosh.ClearOutput 
 end sub 

 public sub eval(strtmpServer,strtmpUsername,strtmpPassword,strtmpPSCommand) 
 dim outtext,str 
 Set ActiveXPosh = CreateObject("SAPIEN.ActiveXPosh") 
 ActiveXPosh.Init(vbTrue) 
 ActiveXPosh.Execute("Add-PSSnapin VMware.VimAutomation.Core") 
 ActiveXPosh.Execute("connect-viserver -Server " & strtmpServer & " -User " & strtmpUserName & " -Password " & strtmpPassword) 
 ActiveXPosh.OutputMode = OUTPUT_BUFFER 
 context.logmessage "Running " & strtmpPSCommand 
 
 'Execute command in powershell 
 if ActiveXPosh.eval(strtmpPSCommand) = vbTrue then 
 p_array_commandoutput = vbTrue 
 end if 

 Set ActiveXPosh = nothing 
 end sub 

 public sub execute(strtmpServer,strtmpUsername,strtmpPassword,strtmpPSCommand) 
 dim outtext,str 
 Set ActiveXPosh = CreateObject("SAPIEN.ActiveXPosh") 
 ActiveXPosh.Init(vbtrue) 
 ActiveXPosh.Execute("Add-PSSnapin VMware.VimAutomation.Core") 
 ActiveXPosh.Execute("connect-viserver -Server " & strtmpServer & " -User " & strtmpUserName & " -Password " & strtmpPassword) 
 ActiveXPosh.OutputMode = OUTPUT_BUFFER 
 'context.logmessage "Running " & strtmpPSCommand 
 'Execute command in powershell 
 ActiveXPosh.Execute(strtmpPSCommand) 
 ' for each str in ActiveXPosh.output 
 ' outtext = outtext & str 
 ' outtext = outtext & VbCrLf 
 ' Next 
 ' wscript.echo outtext 
 set p_array_commandoutput = ActiveXPosh.output 
 Set ActiveXPosh = nothing 
 end sub 
end class 

function getDisplayNamefromID(strtmpDeviceID) 
 dim strReturn 
 ' Get the DB instance used by WhatsUp 
 set objDatabase = Context.GetDB 
 ' Check it worked OK 
 if "" = objDatabase then 
 Context.SetResult 1, "Problem connecting to database" 
 else 
 context.logmessage "Connected to DB OK" 
 ' We need to find the reference used for this device in the PivotStatisticalMonitorTypeToDevice table first 
 strQuery = "SELECT sDisplayName FROM [WhatsUp].[dbo].[Device] where nDeviceID=" & strtmpDeviceID objResultSet = objDatabase.Execute(strQuery) 
 strReturn = objResultSet(0) 
 end if 
 getDisplayNamefromID=strReturn 
end function 

This just outputs the value to the log. To make it into an active monitor you’ll need to evaluate the result and to a performance monitor update context.setvalue, for example:

for each line in powershell.output context.setvalue cdbl(line) next 

Running a test from an active monitor should return the value from the powershell command, in this case 0.27.

Connected to DB OK 
Target VM Name is MyVMServer 
Logging in as MyDomain\MyUser 
[Math]::Round(((get-vm -Name MyVMServer | Get-Stat -Stat Cpu.Ready.Summation -Start (get-date).addMinutes(-1) -Finish (get-Date) | Measure-Object -Property Value -Average).Average)/200,2) 
Fetching statistics from MyVCServer.MyDomain.com 
0.27 
Done.
Advertisements
Tagged , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: