randle Posted February 18, 2015 Posted February 18, 2015 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?
howartp Posted February 18, 2015 Posted February 18, 2015 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? 2
randle Posted February 19, 2015 Author Posted February 19, 2015 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$")
howartp Posted February 19, 2015 Posted February 19, 2015 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. 1
randle Posted February 19, 2015 Author Posted February 19, 2015 Ah good call. Thanks for that. Much tidier now
Garacesh Posted March 23, 2015 Posted March 23, 2015 (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 March 23, 2015 by Garacesh 1
randle Posted March 24, 2015 Author Posted March 24, 2015 Yes, also works a treat thanks. Great to see alternative ways for future scripts.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now