PowerCLI

 View Only
  • 1.  Remote executing powershell script

    Posted Aug 19, 2011 06:16 AM

    Hi,

    I want to implement this "datastore emergency" script in my environment. http://www.vcritical.com/2009/10/powershell-prevents-datastore-emergencies/

    But i have a server with all the scripting tools setup which is not the vcenter server. When the datstore alert is triggered in vcenter which i want to use "Run a command" option and to invoke & process the script in the scripting server, without installing any powershell tools in my vcenter server.

    Any ideas? Thanks in advance!



  • 2.  RE: Remote executing powershell script

    Posted Aug 19, 2011 08:39 AM

    I would look at the psexec command to start the PowerShell session on your scripting server.

    The Alarm on the vCenter would contain a psexsec command, which starts the PowerShell session + script on the scripting server.

    See Lee Holmes's post called Using PowerShell and PsExec to invoke expressions on remote computers

    You would only have to make psexec available on the vCenter.

    The alternative could be to use PowerShell remoting.

    But that requires running a PowerShell script, which starts the remote session, on the vCenter.



  • 3.  RE: Remote executing powershell script

    Posted Aug 24, 2011 07:57 AM

    Thanks Luc.  I'm checking on psexec method currently, but looks bit complicated. Is there a easy explanation available somewhere?



  • 4.  RE: Remote executing powershell script

    Posted Aug 24, 2011 08:16 AM

    If you're using PowerShell v2, I would strongly advise to go for the PowerShell Remoting feature.

    There is a very good (and free) ebook from Ravikanth Chaganti



  • 5.  RE: Remote executing powershell script

    Posted Aug 24, 2011 08:22 AM

    Thanks Luc.

    As far as i understood powershell remoting needs powershell on the source machine, which is vCenter in my case where i cant install any powershell related stuff. Pls correct me if i'm wrong.



  • 6.  RE: Remote executing powershell script

    Posted Aug 24, 2011 08:41 AM

    That is correct, so back to psexec then :smileyplain:

    What do you find difficult in the psexec command ?



  • 7.  RE: Remote executing powershell script

    Posted Aug 24, 2011 09:43 AM

    I do not know,

    1. How i will capture the datastore name which produces the alert and pass it to the remote computer as a input to the script using psexec command.

    2. I will scheduling this on multiple virtual centers, so what will happen if a datastore alert was produced and the script was migrating a VM, and at the same time one more vcenter send a input via psexec to the scripting server?



  • 8.  RE: Remote executing powershell script

    Posted Aug 24, 2011 11:54 PM

    1. In the script action definition you can pass predefined values to your command.

    For example, to start a remote PS script and pass the datastorename as a parameter you could do

    $tgtName = "ScriptServer"
    $psExec = "C:\Temp\psexec.exe"
    $psParms = " \\" + $tgtName + " cmd /c"
    $psCommand = ' "echo . | C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -noprofile -nologo -noninteractive c:\Temp\script.ps1 {datastoreName}"'
    $alarm
    = Get-AlarmDefinition -Name "Test psexec"
    New-AlarmAction -AlarmDefinition
    $alarm -Script -ScriptPath ($psExec + $psParms + $psCommand)

    Via the $Args parameter you can access the passed value in your script.

    In this script we add a script action to an existing alarm called "Test psexec".

    2. These alarms will each start a PowerShell session to run your script.

    These instances run in their proper environment, so they won't bother each other.

    You are of course limited by the number of parallel (s)vMotions your vCenter allows.

    But that should not be a problem, since the (s)vMotion tasks will be queued.



  • 9.  RE: Remote executing powershell script

    Posted Aug 25, 2011 03:31 AM

    Luc,

    Sorry for asking again, looks like i need step by step explanation, since this concept is very new to me.

    1. I have vcenter as my source machine and i have defined a alarm at DC level named "Datastore overusage". For this alarm, in the action's tab, i select "Add" and select "Run a command".  Please note this vcenter source machine is without powershell installed.

    2. For this "Run a command" parameter what command should i give? is it like i should save the code u gave into bat file and pass bat file as a parameter?

    3. The below given code is saved in the scripting server as c:\scripts\thinmig.ps1

    Connect-VIServer <vcserver>

    $vmToMove = get-vm -Datastore $env:VMWARE_ALARM_TARGET_NAME | select-object -first 1

    $destDS = Get-Datastore "datastore1"  #"datastore1" is a rescue datastore kept empty

    if ($destDS) {
    move-vm -VM $vmToMove -Datastore $destDS -RunAsync
    }


    Please clarify.