Solved

powershell pause function

Posted on 2016-07-29
10
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 81

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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