Monday, June 17, 2013

Run Powershell against each computer listed in one CSV column.

Today's challenge was a continuation of a previous script. Here I wanted to take a CSV file with two columns. One Column with PCNAME and another with the header MAC. What I want to do here is keep a list of my computers with the MAC Addresses and Computer Names. This will make it easier when auditing the list making sure it is up to date and be able to find mistakes if I have the names associated with the MAC addresses. So here is how to do it.

One we are going to dot source our wake on LAN Function.
./Send-WakeOnLAN.ps1

Now set the path
$Path = X:\Folder\ComputerList.CSV
now for the tricky part
Import-CSV $Path | Select MAC | foreach {Send-WakeOnLAN $_.MAC}

Here we can see the obvious the Import-CSV and the select MAC
This will give us the column we want. However the problem with stopping there is that you will get @MAC=00:22:33:44 etc.
The reason is that it is including its header. Even if you write in such a way to not include the header you will end up getting only one of the many objects you are looking for.

This is the reason for piping it into a foreach command. Now you can send your WOL packet to all listed computers in one column within a csv.

No comments:

Post a Comment