2

I have a very basic PowerShell that accepts a list of Windows Server and Services on them and then stops the Services

Get-Service -ComputerName machine1 -Name service1 -ErrorAction Stop | Stop-Service -WarningAction SilentlyContinue 

Although I had explicitly mentioned -ErrorAction as Stop, in a case if one of the Server is not reachable, the Powershell doesn't come back. How do I change this behaviour at least in a way to stop processing after a certain amount of time as "n" secs which again can be passed thru parameter?

2
  • 2
    Simplest would be to enclose it in a Test-Connection like this if (Test-Connection machine1 -Count 1 -ErrorAction SilentlyContinue) { ... } Commented Apr 16, 2018 at 5:01
  • Wonderful, I just thought of making it a little easier by wrapping the script like this if (Test-Connection -ComputerName machine1 -Quiet) {all my current code} Thanks a ton for your suggestion of Test-Connection Commented Jul 19, 2018 at 5:56

1 Answer 1

1

Incorporating both comments, we end up with following

Enclose the current script in a Test-Connection that allows you to first verify if a computer is actually reachable:

if (Test-Connection -ComputerName machine1 -Quiet) { all your current code } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.