PowerCLI

 View Only
  • 1.  how to run a command in background ?

    Posted Apr 24, 2013 03:19 PM

    Hello there,

    is it possible to run powercli command in backgroud?

    what I mean is that; for example there is a datastore named "XXX" and there are 40 VMs in it. I need to shutdown all of them. when I try following script it waits for every VM to shutdown and then next VM etc. if for example "shutdown" command starts in background for each VM then script will finish quickly. and so, shutdown process for all 40 Vms starts in parallel. is it possible?

    $datastore = "XXX"

    $vms = get-vm -datastore (get-datastore $datastore)

    foreach ($vm in $vms) {
         Get-VM $vm| Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false
    }

    for example in unix, linux system if I add "&" to end of the command then it works in background. is there anything like this?

    Thank you.



  • 2.  RE: how to run a command in background ?

    Posted Apr 24, 2013 04:10 PM

    Some PowerCLI cmdlets have a -RunAsync parameter that runs the command asynchronously. Unfortunately the Shutdown-VMGuest cmdlet does not have this parameter, due to the vSphere API which does not have a ShutdownGuest_Task method for virtual machines.

    You could also use the PowerShell Start-Job cmdlet to run tasks in parallel.

    In this case you can use the Shutdown-VMGuest -VM parameter that accepts an array of vm's as input. This will also make your script faster because Shutdown-VMGuest is called only once:

    $datastore = "XXX"
    $vms = Get-VM -Datastore (Get-Datastore -Name $datastore) |
      Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guest.State -eq "Running"}
    Shutdown-VMGuest -VM $vms -Confirm:$false
    
    



  • 3.  RE: how to run a command in background ?

    Posted Apr 24, 2013 06:01 PM

    I will try it when I get back to office.

    I need to rescan HBA for new LUNS, say that there are 10 host in cluster. than I use following command:

         Get-VMHost | Get-VMHostStorage -RescanAllHBA -RescanVmfs

    this takes about 5 mins for each host. so it takes 50 minutes totally. but when I do this on cluster on vmclient, it scan all HBAs in parallel. as I understand I can use PowerShell Start-Job cmdlet to start scanHBA on each hosts, it will start scanHBA in parallel. is it true?

    Thank you very much.