7

I have a CSV file with two different columns, one with PC names and one with MAC addresses like this:

PCxxxx 00-11-22-33-44-55
...
...

These values should be placed into the following CLI command:

wdsutil /Set-Device /Device:PCxxxx /ID:00-11-22-33-44-55

Now since there are about 200 of them, I want to automate this.

As a matter of interest, could one do this with batch? It would be quite complicated, right? I thought of arrays, but don't think one can do this in batch.

Maybe with PowerShell it'd be a bit easier.

1

2 Answers 2

12

In a batch file:

for /f "tokens=1,2 delims= " %%a in (foo.csv) do (
    wdsutil /Set-Device /Device:%%a /ID:%%b
)

Actually, you can do that as a one-liner from cmd directly:

for /f "tokens=1,2 delims= " %a in (foo.csv) do wdsutil /Set-Device /Device:%a /ID:%b

In PowerShell you can use a similar idea:

Get-Content foo.csv | ForEach-Object {
  $name,$mac = -split $_
  wdsutil /Set-Device /Device:$name /ID:$mac
}

Or use the CSV import cmdlet, but given your question you don't seem to have column headers, so you need to provide them manually:

Import-CSV -Delim ' ' -Path foo.csv -Header Name,Mac | ForEach-Object {
    wdsutil /Set-Device "/Device:$($_.Name)" "/ID:$($_.Mac)"
}
Sign up to request clarification or add additional context in comments.

2 Comments

The FOR ... delims= " option is not necessary because the space is the default delimiter. You may use just for /f "tokens=1,2" %%a in ...
Yes, I just chose to be explicit here. Besides, the default delimiters are spaces and tabs.
3
# First column in csv file must be titled PCName, second column must be titled MacAddress.
Import-Csv myfile.csv | %{ wdsutil /Set-Device /Device:$_.PCName /ID:$_.MacAddress } 

Warning: not tested.

1 Comment

The file doesn't appear to have headers so you'd need to provide them manually. Also the default delimiter wouldn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.