Solved

powershell pause function

Posted on 2016-07-29
10
31 Views
Last Modified: 2016-08-01
i am trying to pause the item but my syntax keeps coming in correct


$servers = "192.168.171","192.168.172"

Foreach($s in $servers){
  if (Test-Connection -ComputerName $s -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$s, Up :)" -foregroundcolor “green”
  }
  else{
    Write-Host "$s, Down :( -Please Fix " -foregroundcolor “red”
  }
}
0
Comment
Question by:Jorge Ocampo
  • 5
  • 4
10 Comments
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41735119
Do you want a pause until the user hits a key or a timed pause.

To for user to hit a key you can use:
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

For a timed pause use the Start-Sleep command
Start-Sleep -s 10    #pauses for 10 seconds.
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41735123
until key is good, how would the code look like?
0
 
LVL 12

Accepted Solution

by:
Gary Dewrell earned 250 total points
ID: 41735127
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41735131
i have not working
0
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41735132
Past the code and results from running it here.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41735134
$servers = "162.49.9.71","162.49.3.9","192.165.245.0"

Foreach($s in $servers){
  if (Test-Connection -ComputerName $s -Count 1 -ErrorAction SilentlyContinue){
    Write-Host "$s, Up :)" -foregroundcolor “green”
  }
  else{
    Write-Host "$s, Down :( -Please Fix This Panel" -foregroundcolor “red”
  }
}
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
0
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41735139
Works for me, what error are you getting?
0
 
LVL 78

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 41735741
$host.readkey doesn't work in ISE

$servers = '162.49.9.71','162.49.3.9','192.165.245.0'
Function Pause {
param (
    $Message = 'Press any key to continue . . . '
    ) 
    If ($psISE) {
        # The "ReadKey" functionality is not supported in Windows PowerShell ISE.
        $Shell = New-Object -ComObject 'WScript.Shell'
        $Button = $Shell.Popup('Click OK to continue.', 0, 'Script Paused', 0)
        Return
    }
 
    Write-Host -NoNewline $Message
 
    $Ignore =
        16,  # Shift (left or right)
        17,  # Ctrl (left or right)
        18,  # Alt (left or right)
        20,  # Caps lock
        91,  # Windows key (left)
        92,  # Windows key (right)
        93,  # Menu key
        144, # Num lock
        145, # Scroll lock
        166, # Back
        167, # Forward
        168, # Refresh
        169, # Stop
        170, # Search
        171, # Favorites
        172, # Start/Home
        173, # Mute
        174, # Volume Down
        175, # Volume Up
        176, # Next Track
        177, # Previous Track
        178, # Stop Media
        179, # Play
        180, # Mail
        181, # Select Media
        182, # Application 1
        183  # Application 2
 
    While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
        $KeyInfo = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
    }
 
    Write-Host
}
Foreach($s in $servers){
    $status = $false
    do {
        if ($status = Test-Connection -ComputerName $s -Count 1 -ErrorAction SilentlyContinue)
        {
        Write-Host "$s, Up :)" -ForegroundColor Green
        }
    else {
          write-host "$status $s,Down :( - Please Fix this Panel" -ForegroundColor Red
          Write-Host 'Press any key to continue ...'
          pause
          $status = $false  #will continue until fixed otherwise remove this line
          } 
    } while  ($status -eq $false)
}

Open in new window

0
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 41735742
True it does not run in ISE but it will work fine in powershell (not powershell ise) or if  you run it as a script file.
0
 
LVL 2

Author Closing Comment

by:Jorge Ocampo
ID: 41737433
it did work, just was trying it in ISE thanks david for the explanation
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now