user3721640
user3721640

Reputation: 947

read two different strings of same line from a CSV

Sorry, I am a newbie in PowerShell and for this basic one.

I have a CSV file with these contents and with several more lines and wanted to read each line of two variables (Name, User) and run a cmdlet as below.

~# cat test.scv

Name, User
mycomputer1, John
mycomputer2, Jake
….
….
mycomputer100, Susan

I used Import-CSV to import the file.

$csv = Import-CSV “X:\test.csv"

This would result as:

Name            User
———             ——— 
mycomputer1     John
mycomputer2     Jake
..
..
mycomputer100   Susan

How should I modify this script to read each line and use both values (Name, User) in that line to run the Connect-Server cmdlet one at a time?

Connect-Server –Server mycomputer1 –User John
Connect-Server –Server mycomputer2 –User Jake
…
…
Connect-Server –Server mycomputer100 –User Susan

Upvotes: 1

Views: 85

Answers (2)

Esperento57
Esperento57

Reputation: 17472

Solution of "The Shooter" is better, but you can do like this too:

$csv = Import-CSV "C:\temp\test.csv"     
foreach ($row in $csv)
{
    Connect-Server -Server $row.Name -User $row.User 
}

Upvotes: 0

The Shooter
The Shooter

Reputation: 733

Try this:

Import-Csv C:\temp\ps\cs.txt | ForEach-Object { Connect-Server -Server $_.Name -User $_.User }

Upvotes: 1

Related Questions