I need pass an array of strings to -ComputerName parameter of Restart-Service cmdlet
My computer names are just string with number appended to it. I tried following but it did not work (-ComputerName (1…10| % {“COMPUTERNAME$_”}) ). How do I elegantly do it inline?
Restart-Service doesn’t have a -ComputerName parameter, so far as I know. But assuming that you were using a cmdlet that did have such a parameter, the only problem I see with your post is that you’ve placed the whole thing into a set of parentheses. The argument should be in parens, but the parameter itself should not. For example:
Unlike Restart-Service, Get-Service does have a -ComputerName parameter. I’m not sure if you’ll be able to pipe the resulting ServiceController objects into Restart-Service (depends on how it’s written; it may wind up trying to restart a service by the same name on the local computer), but if nothing else, you could do something like this: