简体   繁体   中英

Powershell CSV Variable Issue

I am having a problem with passing a variable into a CSV. I need to pass an email for a spreadsheet showing all skills. It's the same email for each skill. I just want the $email to populate my csv. It does not pass and only shows the $email instead of the test@test.com in the column.

I am new at powershell so any guidance is greatly appreciated. Thank you.

------------here is my script-------------------------

Add-Content -Path C:\temp\test.csv -Value '"User Name","Skill Name","Level"'

$Email = "test@test.com"

$agent = @(

}

'"$Email","T1","4"'

'"$Email","T2","6"'

'"$Email","T3","7"'

'"$Email","Training","1"'

'"$Email","Supervisor","8"'

)

$agent | foreach { Add-Content -Path C:\temp\temp.csv -Value $_ }

Because you are new to Powershell, I'm showing you two alternative ways of framing the problem. These might help you get used to some of the features of powershell.

$Email = 'test@test.com'

$mytext = @"
"User Name","Skill Name","Level"
"$Email","T1","4"
"$Email","T2","6"
"$Email","T3","7"
"$Email","Training","1"
"$Email","Supervisor","8"
"@


$mytext | Out-file Mycsv.csv

Here, I just set up the Email variable, then create one big here string with the header and the five data records in it. Because I used double quotes on the here string, the variable $Email will be detected inside of it. A here string with single quotes would not have behaved correctly.

Then, I pass $mytext through a pipeline one line at a time, and Out-file collects all this into a file.

Here's the second approach:

$Email = 'test@test.com'

$myarray = @(
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T1"; "Level" = 4}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T2"; "Level" = 6}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "T3"; "Level" = 7}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "Training"; "Level" = 1}
       [PsCustomobject]@{"User Name" = $Email; "Skill Name" = "Supervisor"; "Level" = 8}
)

$myarray | Export-Csv myothercsv.csv

Here, I set up the variable Email, then create an array of custom objects, each with the same named properties.

Then I pass the array through a pipeline to Export-Csv which converts everything to Csv format. It's worth noting that Export-Csv V5 throws in a line that says #TYPE in it. This is not hard to eliminate, using the notype parameter if desired. It's also worth noting that the double quotes in the output file were all added in by Export-csv, and weren't copies of the double quotes in the script.

Edit . Pipelines are a surprisingly easy and flexible way of getting things done in powershell. For this reason, cmdlets like Out-File and Export-Csv are built to work well with pipelines supplying a stream of input. A lot of loop control, initialization, and finalization busy work is being handled behind the scenes by PS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM