VMware Cloud Community
bazvink
Contributor
Contributor
Jump to solution

vCenter Alarms and calling a powershell script

Ok, either I'm missing something, or I'm doing something incredibly stupid.

I have created a simple powershell script that starts a pre-defined VM. I've tested it in PowerGUI, it works fine, no problems so far.

Now all I want to do is attach an alarm to ANOTHER VM in vCenter, such that if the VM goes down (state power off), the aforementioned script is called and the "failover" VM is started.

I've set up the alarm with an action to "run a script", the script being : "C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe C:\SCRIPTS\FAILOVER.ps1" (including quotes).

And you guessed it: the script doesn't run. I'm pretty sure it doesn't run, because part of the script is creating a log file, and this file isn't creaed (it IS created when I run the script from PowerGUI).

What am I doing wrong? How do I get the alarm to kick-off my script?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I have never succeeded in running a PS script this way either.

But I have found a bypass.

You can start your PS script indirectly via a BAT or CMD file.

This is what I define in the value field

c:\windows\system32\cmd.exe /c c:\Scripts\alarmscript.bat

And the BAT file contained this

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsolefile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -Command C:\Scripts\Test-Alarm.ps1

To sample script I used

Get-VIToolkitVersion | Out-File "C:\Scripts\PSOut.log" -Append
Get-PSSnapin | Out-File  "C:\Scripts\PSOut.log" -Append


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

I have never succeeded in running a PS script this way either.

But I have found a bypass.

You can start your PS script indirectly via a BAT or CMD file.

This is what I define in the value field

c:\windows\system32\cmd.exe /c c:\Scripts\alarmscript.bat

And the BAT file contained this

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsolefile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -Command C:\Scripts\Test-Alarm.ps1

To sample script I used

Get-VIToolkitVersion | Out-File "C:\Scripts\PSOut.log" -Append
Get-PSSnapin | Out-File  "C:\Scripts\PSOut.log" -Append


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

A nice feature with alarms and the "Run a script" action is that you can pass parameters to the script.

See the VIC help under "Setting and Viewing Alarms : Creating and Editing Alarms : Alarm Settings – Actions".

The way I do this is as follows:

1) Specify the parameters you want to pass in the Action field

c:\windows\system32\cmd.exe /c c:\Scripts\alarmscript2.bat {targetName} {alarmName}

2) Pass the parameters in the BAT file to the PS script

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsolefile "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -Command C:\Scripts\Test-Alarm.ps1 %1 %2

3) Define the parameters in the PS script

Param (
  [string] $targetName,
  [string] $alarmName
)

$targetName | Out-File "C:\Scripts\PSOut.log" -Append
$alarmName | Out-File  "C:\Scripts\PSOut.log" -Append

Watch out with global alarms.

Each entity for which the alarm is triggered will start a PS shell to execute the script.

That can give quit some overhead on the VC server.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

bazvink
Contributor
Contributor
Jump to solution

Thanks for the tip, but unfortunately that didn't work.

I tried a few things, like putting the "cmd-call" between quotes, and without.

Is there anyway I can see some logging of vCenter? Someway I can check if the alarm is even triggering properly?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You do have PowerShell and the VITK installed on your VC ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

bazvink
Contributor
Contributor
Jump to solution

I think I got it working!

In the batch file, I put the following:

start "" c:/windows/system32... blah blah.

I recalled from some batch files I made a while back, that 'start "" ' is required to actually start an exe.

Anyhow, this seems to have done the trick.

Thanks again!

0 Kudos
bazvink
Contributor
Contributor
Jump to solution

Just to confirm: you need to add " start "" " in front of any exe you call in the batch file.

And don't put quotes around the call in the alarm in vCenter.

0 Kudos