Jump to content

Recommended Posts

Posted

Hi,

 

I've have the following PowerShell loop code which is simply prompting for a computer name and testing this to make sure it exists before proceeding.

 

 [font=Lucida Console][size=1][color=#00008b][font=Lucida Console][size=1][color=#00008b][font=Lucida Console][size=1][color=#00008b]do[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] {[/size][/font][/size][/font][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500]$Computername[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9][font=Lucida Console][size=1][color=#a9a9a9]=[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff]Read-Host[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]"Enter a computer name"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1]}[/size][/font][/size][/font]
[font=Lucida Console][size=1][color=#00008b][font=Lucida Console][size=1][color=#00008b][font=Lucida Console][size=1][color=#00008b]until [/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1]([/size][/font][/size][/font][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff][font=Lucida Console][size=1][color=#0000ff]Test-Path[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#000080][font=Lucida Console][size=1][color=#000080][font=Lucida Console][size=1][color=#000080]-path[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1] [/size][/font][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]"\\[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500][font=Lucida Console][size=1][color=#ff4500]$Computername[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000][font=Lucida Console][size=1][color=#8b0000]\c$"[/color][/size][/font][/color][/size][/font][/color][/size][/font][font=Lucida Console][size=1][font=Lucida Console][size=1]) [/size][/font][/size][/font]

 

This works fine. If an invalid name is entered it prompts again for a computer name before continuing with the rest of the script however, I'd like it, if possible, to show a message like "Invalid name. Please try again" before returning to the beginning of the loop.

 

I've been looking at try/catch but could do with a little help if anyone has any ideas?

Posted

Between Do{ and $computer, add a line similar to:

 

if $computer <> null then write-line ("Invalid path")

 

I know syntax is wrong but should get you there, unless I've missed something?

  • Thanks 2
Posted

Thanks for pointing me in the right direction. The following code does what I require.

 

do 
{
$Computername = Read-Host "Enter computer name"
if(-not(Test-Path -path "\\$Computername\c$")) 
 {
  write-host "Invalid computername. Please try again"
 }
}
until (Test-Path -path "\\$Computername\c$")

Posted
Thanks for pointing me in the right direction. The following code does what I require.

 

do 
{
$Computername = Read-Host "Enter computer name"
if(-not(Test-Path -path "\\$Computername\c$")) 
 {
  write-host "Invalid computername. Please try again"
 }
}
until (Test-Path -path "\\$Computername\c$")

 

Just be aware that that will add processing overhead to your script as it performs the path test twice. Probably ok for this script, but not an approach you should generally apply to your scripting.

 

do 
{
$Computername = Read-Host "Enter computer name"
$IsValid = Test-Path -path "\\$Computername\c$"
if(-not($IsValid)) 
 {
  write-host "Invalid computername. Please try again"
 }
}
until ($IsValid)

I think this should do the trick, using your style but with only processing the path test once.

  • Thanks 1
  • 1 month later...
Posted (edited)

Bit of a necrobumpish.. Would you not just want a while loop?

$ComputerName = (Read-Host "Please specify target machine")
While (!(Test-Path \\$ComputerName\c$)) {
   $ComputerName = (Read-Host "Target machine could not be contacted. Check name and retry")
}

Edited by Garacesh
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...