How to loop through a CSV package file or PowerShell script?

I have a CSV file with two different columns: one with computer names and one with MAC addresses, for example:

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

These values ​​should be placed in the following CLI command:

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

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

As an interest, could this be done with the help of the party? That would be pretty complicated, right? I was thinking about arrays, but I don't think it can be done in batch mode.

Perhaps with PowerShell this will be a little easier.

+6
source share
2 answers

In the batch file:

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

Actually, you can do this as a single line 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 considering your question, you have no 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)" } 
+10
source
 # 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 verified.

+3
source

Source: https://habr.com/ru/post/914197/


All Articles