Solved

powershell pause function

Posted on 2016-07-29
10
42 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article will help you understand what HashTables are and how to use them in PowerShell.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

17 Experts available now in Live!

Get 1:1 Help Now