VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

sleep command in powercli code

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

   }

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaksiam checking this .

0 Kudos