Ping an Array of Computers
June 7, 2008 at 5:06 PM
—
Andy Schneider
I have found on occasion the need to ping a list of computers, whether they be in a text file, an array in Powershell, or a CSV file. It turns out building commands as a string and then executing that string as a command in Powershell is not exactly intuitive.
You need to use invoke-expression -command $cmd where $cmd is the string you want to execute.
So, we can do the following, using my New-Array Function
1: function new-array {$args} 2: $servers = new-array powershell-dev1 powershell-dev2 powershell-dev3
3: $servers | foreach-object {invoke-expression -command "ping -n 1 $_"}
Using invoke-expression -command you can have an executable and arguments and everything just works. There are situations where you will need to use the back tick character " `" to escape characters.
You can use this technique to execute just about any string that you build using Powershell and variables.
1253fa21-e67b-4690-a8b3-2edbcbe8b433|1|5.0
Posted in:
Tags: