PowerCLI

 View Only

  • 1.  vCenter Alarms and calling a powershell script

    Posted Mar 10, 2009 02:48 PM

    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!



  • 2.  RE: vCenter Alarms and calling a powershell script
    Best Answer

    Posted Mar 10, 2009 05:54 PM

    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
    



  • 3.  RE: vCenter Alarms and calling a powershell script

    Posted Mar 10, 2009 07:37 PM

    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.



  • 4.  RE: vCenter Alarms and calling a powershell script

    Posted Mar 10, 2009 07:42 PM

    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?



  • 5.  RE: vCenter Alarms and calling a powershell script

    Posted Mar 10, 2009 07:45 PM

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



  • 6.  RE: vCenter Alarms and calling a powershell script

    Posted Mar 10, 2009 07:46 PM

    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!



  • 7.  RE: vCenter Alarms and calling a powershell script

    Posted Mar 11, 2009 08:42 AM

    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.