Link to home
Start Free TrialLog in
Avatar of JM D.
JM D.

asked on

PowerShell script which executes a command for each line of a variable containing a list

Sorry but I am not a Powershell expert. And I want to modify a PowerShell script which executes a command for each line of a variable containing a list.

To make it all understandable, I will simplify my example  (in the reality, that's for vmware scripting, but my problem concerns the Powershell syntax )

Imagine a list of computers, stored in a variable, result of another powershell command.
It might look like this.:
SERVERA
SERVERB
SERVERC
SERVERD

And It's stored in a variable "list_computers" ($list_computers)
I want for each computer some (powershell) commands to be executed.
For example

display the computername
ping the computername

How to do that?
How to read each line of the variable ?
Should I use Foreach of Foreach-Object?
How to execute several commands ?

NB:
command "write $list_computers" give:

SERVERA
SERVERB
SERVERC
SERVERD

Could the following syntax be correct?

$list_computers | foreach-object {
$computer = $_
write $computer
command1 $computer
command2 $computer
}

Thanks

Jm
Avatar of footech
footech
Flag of United States of America image

What you have in your variable is an array.  Some example code to see this info:
$list_computers.GetType()

Open in new window

Each individual item in an array is called an element.  To see the type of each element you can call the GetType() method for an individual element, or pipe to the Get-Member cmdlet.
$list_computers[0].GetType()
$list_computers | Get-Member # this also shows the methods and properties available of the element type

Open in new window


When you have an array stored in a variable you can use pretty much any of the various looping constructs to process each element one at a time.  Looping constructs include the foreach statment, the ForEach-Object cmdlet, for, do-while, while, etc.  Many times  it doesn't matter which you use, though there may be some benefits to choosing one method over another in specific circumstances.  However, suffice it to say that the ForEach-Object cmdlet is the one that you will likely use the most, and your syntax in the end is correct.  Many times you don't have to assign the current item in the loop ( $_ ) to a variable, but it's very safe to do so.

Check this:

ForEach ($computer in $list_computers) { 
	Write-Host $computer 
	Test-Connection -ComputerName  $computer -Count 1 
	Test-Connection -ComputerName  $computer -Count 1 
}

Open in new window

Or better try-catch:

ForEach ($computer in $list_computers) { 
	Write-Host $computer 
	Try { 
		Test-Connection -ComputerName  $computer -Count 1 -ErrorAction Stop 
		Test-Connection -ComputerName  $computer -Count 1 -ErrorAction Stop 
	} Catch { 
		Write-Host "$($computer) was not found: $($_.Exception.Message)" -ForegroundColor Green 
	} 
}

Open in new window

below is for command prompt

Open in new window

FOR /f %i IN (c:\TEMP\list.txt) DO ping %i 

Open in new window

for powershell

cmd /c 'FOR /f %i IN (c:\TEMP\list.txt) DO ping %i'


Sorry only one lookup/ping:

ForEach ($computer in $list_computers) { 
	Write-Host $computer 
	Try { 
		Test-Connection -ComputerName  $computer -Count 1 -ErrorAction Stop 
	} Catch { 
		Write-Host "$($computer) was not found: $($_.Exception.Message)" -ForegroundColor Green 
	} 
}

Open in new window

Still analyzing?

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.