Search code examples
powershellcsvwake-on-lan

Parsing a csv for Read-Host user input


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)


Solution

  • 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