Jay Thomas
asked on
Basic Powershell question question. Hopefully
Hi all.
Many a time I run a one liners against multiple servers and so end up running the liner many times. I am looking for a way I can loop in a list of servers from a text file.
My current one liner is this:
Get-ADcomputer -Identify "HostName" -Properties Lastlogon, passwordlastset
Simple! And I can use it but if a have over 100 hostnames in a text file called servers.txt on the C:\temp folder there must be an easy way I can run that command but pipe in that text file?
If I can get the answer to this question it will help not only for my current taks but many times in the future.
Thanks all.
Many a time I run a one liners against multiple servers and so end up running the liner many times. I am looking for a way I can loop in a list of servers from a text file.
My current one liner is this:
Get-ADcomputer -Identify "HostName" -Properties Lastlogon, passwordlastset
Simple! And I can use it but if a have over 100 hostnames in a text file called servers.txt on the C:\temp folder there must be an easy way I can run that command but pipe in that text file?
If I can get the answer to this question it will help not only for my current taks but many times in the future.
Thanks all.
In general, you can do that with a ForEach-Object loop, with $_ being the current loop element. So you first read the contents of the file, then do something for each element:
Get-Content C:\Temp\servers.txt | ForEach-Object {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}
If you want to save some typing for one-liners, there are two Aliases for ForEach-Object: ForEach and %:Get-Content C:\Temp\servers.txt | ForEach {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}
Get-Content C:\Temp\servers.txt | % {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}
This was the ForEach-Object cmdlet. There's a ForEach statement as well:ForEach ($Line In (Get-Content C:\Temp\servers.txt)) {Get-ADcomputer -Identity $Line -Properties Lastlogon, passwordlastset}
ASKER
Brilliant response that you guys. Just going to try each method out now. Back in a bit.
ASKER
@ Dustin.
I get an error saying 'Cannot bind argument to parameter 'path' it is null.
I get an error saying 'Cannot bind argument to parameter 'path' it is null.
Ah, try
$file = "C:\Temp\servers.txt"
$computers = Get-Content $file
foreach ($computer in $computers)
{
Get-ADComputer -Identity $computer -Properties lastlogon,passwordlastset
}
ASKER
That got it thank you. I hope I am not going off topic but if I add
> c:\temp\svresult.txt
to the end of the Get-ADcomputer code it creates the text file but only has the data from the first server in the server list. Is this to of topic to ask this at this time?
Thank you
> c:\temp\svresult.txt
to the end of the Get-ADcomputer code it creates the text file but only has the data from the first server in the server list. Is this to of topic to ask this at this time?
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Still another way you can do this is piping directly to the cmdlet. It depends on the cmdlet what type of input it accepts, but here this would work.
Get-Content "C:\Temp\servers.txt" | Get-ADcomputer -Properties Lastlogon, passwordlastset
ASKER
Wow. you PS dudes blow me away. That works and so do the one liners oBDa (like they ever weren't going to work :)
I'm giving most the points to Justin, hope that feels fair to all. I would like to say a big thank you chaps. I can see both options here saving me countless hours in the future so thank you very much.
I'm giving most the points to Justin, hope that feels fair to all. I would like to say a big thank you chaps. I can see both options here saving me countless hours in the future so thank you very much.
ASKER
Hats off!
Next, use a foreach loop to iterate through the computers in the list.
Open in new window