Include string in "piped loop"

by arepare at 2013-03-21 05:58:28

Hi,

Please excuse any brainfarts, I’m pretty new at this. Anyhow, I have a script that takes input from a textfile and pipes to different commands. The script looks like this:

Get-Content C:\Scripts\machinelist.txt | Get-QADComputer -Properties operatingsystem, LastLogonTimestamp, distinguishedName | Select Name, operatingsystem, LastLogonTimeStamp, distinguishedName | export-csv output.csv -noType

(The QADcomputer cmdlet is from Quest ActiveRoles, it’s only used because it autoformats LastLogonTimeStamp nicely)

Now, I would like to include a string on each row in the csv along the lines of "http://server/hostinfo.jsp?name=$name". I would be comfortable foding this with for-loops in other scripting languages, but from what I’ve been told one shouldn’t use for-loops in Powershell. I’ve tried to pipe to write, write-host and so forth, but I only get "Write-Host: The input object cannot be bound to any parameters (blahblah)".

I guess I have two questions:

1) Is this possible, and if so how?
2) What would I use for $name in the absence of arrays/variables?

Any help appreciated, thanks in advance.
by Klaas at 2013-03-21 06:19:24
1. You can add an expression
2. you do have the name (if I understand you well)

I think this should do it:
Get-Content C:\Scripts\machinelist.txt | Get-QADComputer -Properties operatingsystem, LastLogonTimestamp, distinguishedName | Select Name, operatingsystem, LastLogonTimeStamp, distinguishedName, @{l='Url';e={'http://server/hostinfo.jsp?name=' + $_.name}} | export-csv output.csv -noType
by arepare at 2013-03-21 06:37:01
Great, thanks - that worked a charm!

A few questions though, just so I get this straight.

1. Are the "l=" and "e=" in the expression random operators? Or to pose the question differently; are there different operators for formatting, seen as "l" and "e" end up on different rows in the CSV?
2. Where is $name picked from? Preceding instance of "Name" ((…)Select Name, (…))?
3. Is there a way to choose a different separator than comma for export-csv? The result for "distinguishedName" is a comma-separated string, which breaks the formatting when importing csv to spreadsheet or database.

Cheers
by Klaas at 2013-03-21 08:11:30
1. ‘l’ stands for ‘label’ and 'e ’ for expression. you can’t choose those freely, although you could use ‘label’, ‘n’, ‘na’, ‘nam’ or ‘name’ and ‘expression’
2. $
.name (mind the dot) is the name property of the object you’re getting from the pipeline, in this case a computer object
3. add the -delimiter parameter to the Export-Csv cmdlet
Try Get-Help Export-Csv -Full | more to read all about the cmdlets options