Solved

powershell pause function

Posted on 2016-07-29
10
52 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
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: 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 79

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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell query 2 22
DFS-R questions 4 22
Need help to understand PowerShell code 4 22
How to search all of Group Policy for reference to a server name? 8 21
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

10 Experts available now in Live!

Get 1:1 Help Now