PowerCLI

 View Only
  • 1.  Wait until host goes offline and then execute a command

    Posted Nov 18, 2014 03:03 PM

    Hi everyone,

    I have a shutdown script for my virtual infrastructure, now I have to add some code to shutdown an Equallogic array AFTER the last host is powered off.

    I know the commands for doing that but i need some help in writing the loop that will test the management ip of the last host and wait until this stop responding to pings.

    In my script i establish what host will be powered off for last so this is not a problem... how can i keep pinging the host management IP until it dies and then run the Equallogic commands?

    I hope I managed to explain myself... i was exploring the Test-Connection CmdLet but it seems like it outputs che echo of the ping instead of the status... i got a little lost around this, anyone can help me please?



  • 2.  RE: Wait until host goes offline and then execute a command

    Posted Nov 18, 2014 03:53 PM

    While i think i found a way around my problem then another one arises... but let's start from my solution:

    DO {

      write-host "$(get-date -f HH:mm:ss) $(get-date -f dd/MM/yyyy) - Host is online"

      sleep 5

      } While (Test-Connection 192.168.110.85 -BufferSize 16 -Count 1 -Quiet)

    Write-Host "$(get-date -f HH:mm:ss) $(get-date -f dd/MM/yyyy) - Host is offline"

    It works, the problem is that this loop might never exit because if for some reason the host never powers down this script will run forever.

    I was thinking to try to execute this for a number of times and then exit anyway but i dont' know how to do this.

    Suggestions?



  • 3.  RE: Wait until host goes offline and then execute a command
    Best Answer

    Posted Nov 18, 2014 06:08 PM

    Would something like this do the trick ?

    $count = 0

    $maxWait = 10

    Do {

      $count += 1

      Write-Output "$(get-date -f HH:mm:ss) $(get-date -f dd/MM/yyyy) - Host is online"

      sleep 5

    } until (!(Test-Connection 192.168.110.85 -BufferSize 16 -Count 1 -Quiet) -or ($count -gt $maxWait))

    if($count -gt $maxWait){

        Write-Output "Host still online"

    }

    else{

        Write-Output "$(get-date -f HH:mm:ss) $(get-date -f dd/MM/yyyy) - Host is offline"

    }



  • 4.  RE: Wait until host goes offline and then execute a command

    Posted Nov 19, 2014 06:54 PM

    I'm sorry but it doesn't seem to work for me.

    It keeps pinging forever and never seems like getting out of the loop.



  • 5.  RE: Wait until host goes offline and then execute a command

    Posted Nov 19, 2014 07:45 PM

    I think the 1st part of the condition should be negated (I added the -not, alias is !) to the 1st condition.

    Try like that



  • 6.  RE: Wait until host goes offline and then execute a command

    Posted Nov 19, 2014 10:54 PM

    In this way it just does one ping and then exit the loop, no matter what i set for $maxWait



  • 7.  RE: Wait until host goes offline and then execute a command

    Posted Nov 20, 2014 09:09 AM

    My mistake, the while should have been an until



  • 8.  RE: Wait until host goes offline and then execute a command

    Posted Nov 20, 2014 09:36 AM

    That's right, I shuold have seen that too.

    Thanks, works exactly as I need it. Kudos!