0

So here is my problem... I have exported a list (CSV file) of computer names, MAC Addresses, and IP addresses for Wake on LAN via a PowerShell script. I'd like to have a read-host option at the top asking the user the host name of the computer they want to wake. That part is easy enough with a simple Read-Host.

Here's my question: how can i search through the exported CSV for the userinput that the user input?

Here's an example of my csv:

Hostame              MAC               IP Address

Computer 1     00-15-60-97-5B-8E       192.168.0.1
etc                    etc                 etc

so if the user specifies they want to wake computer1 by inputting computer1@domain.com setting the Read-Host variable, how can I have a script find the computer the user wants to wake, and then grab the mac and the ip to use in combination with WOLCMD.exe (Wake on LAN command line tool)

abatishchev
  • 98,240
  • 88
  • 296
  • 433

3 Answers3

1

If your users have V3 or better, you can also use Out-GridView to get user input:

Import-Csv c:\somedir\computerlist.csv | sort computername
 Out-GridView -Title 'Select a computer, and press OK to send Wake command:' -OutputMode Single |
 foreach { & "wolcmd $_.mac 255.255.255.255 255.255.255.255" }

That will present a grid view of the CSV to the use, and they'll be able to pick one computer from the grid to restart.

mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Using Out-GridView as a way of selecting list members from a user as part of a pipeline operation is quite slick. – HipCzeck Jul 14 '14 at 15:07
0

If a user specifies a computer name, then you'll need to filter the CSV file for that computer name, and grab the MAC address and IP address.

$ComputerName = Read-Host -Prompt 'Please enter a computer name.';
$Csv = Import-Csv -Path c:\test\test.csv;
$Record = $Csv | Where-Object -FilterScript { $PSItem.Computer -eq $ComputerName; };

Write-Host -Object ("Computer's IP address is: {0} and MAC address is: {1}" -f $Record.'IP Address', $Record.MAC);
0

Here's my tested solution. This should work like a charm and will broadcast for the MAC address in case the target is not in your arp table.

$csvpath = <<modify this with the path to the csv file>>
$comp = read-host -prompt "Enter the target computer name: "
$dataset = import-csv -path $csvpath
$row = ($dataset | where{$_.hostname -eq $comp})
$mac = $row.mac
wolcmd $mac 255.255.255.255 255.255.255.255

Be sure to put the full path to the csv file on that 1st line before running