PowerCLI

 View Only
  • 1.  sleep command in powercli code

    Posted Jul 05, 2019 05:41 AM

    Hi Luc,

    can you please suggest what needs to be modified to get following functionality.

    what i want to achieve is if there is no response  in orange line within 10 seconds it shud move to next snippet .

    $vms_tools_upgrade_needed=$vms | Where-Object {$_.Guest.GuestFamily -eq 'windowsGuest' -and $_.ExtensionData.guest.toolsversionstatus -eq 'guesttoolsneedupgrade'}

       write-host "there are" $vms_tools_upgrade_needed.count "windows vms pending for tool upgrade in  " $cluster.name -ForegroundColor Yellow

         $response = Read-Host "Would you like to update vmware tools?"

       

       if ($response -eq "yes")

       {

       Write-Host "based on your response updating vmware tools which will take into effect in next powercycle " -ForegroundColor Green

       #code

       if($response -eq "")

       {

       Start-Sleep -Seconds 10

       Write-Host "moving to next snippet" -ForegroundColor Gray

       }



  • 2.  RE: sleep command in powercli code
    Best Answer

    Posted Jul 05, 2019 06:48 AM

    With Read-Host there is no timeout possible.
    You can create a loop where you wait for a key press with a timeout.

    Something like this for example

    Write-Host "Press y/n (you have 10 seconds)"

    $timer = [Diagnostics.Stopwatch]::StartNew()

    $answer = ''

    while($timer.Elapsed.TotalSeconds -lt 10){

       if ($host.UI.RawUI.KeyAvailable) {

       $answer = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp,IncludeKeyDown")

       if ($answer.KeyDown -eq "True"){

       break

       }

       }

       Write-Host '.' -NoNewline

      sleep 1

    }

    $timer.Stop()


    if($timer.Elapsed.TotalSeconds -lt 10){

       Write-Host "`nAnswer : $($answer.character)"

    }

    else{

       Write-Host "`nTimer expired"

    }

    ---------------------------------------------------------------------------------------------------------

    Was it helpful? Let us know by completing this short survey here.



  • 3.  RE: sleep command in powercli code

    Posted Jul 05, 2019 06:49 AM

    Note that this will not work in the ISE or VSC, only from the PS prompt



  • 4.  RE: sleep command in powercli code

    Posted Jul 05, 2019 07:14 AM

    thnaksiam checking this .