Solved

powershell pause function

Posted on 2016-07-29
10
60 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
 
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 80

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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