t3chb0t
t3chb0t

Reputation: 18655

Looping over multiple commands

I'm writing a script that should output pending changes for multiple workspaces in TFS (Team Foundation Server).

So far I've got this:

$collection="http://tfs-abc/"

$stats = @{
    #'WS1' = tf stat /workspace:"WS1" /collection:$collection | select -last 1,
    'WS2' = tf stat /workspace:"WS2" /collection:$collection | select -last 1
}

$stats.Keys | ForEach-Object { Write-Host "$_ - $($stats[$_])" }

It only works with a single item and produces:

WS2 - 45 change(s), 263 detected change(s)

If I uncomment the first workspace then it doesn't work anymore and says:

Select-Object : Cannot convert 'System.Object[]' to the type 'System.Int32' required by parameter 'Last'.

Upvotes: 0

Views: 2410

Answers (1)

whatever
whatever

Reputation: 891

Just remove the "," between the two lines, elements in hashes need to be seperated by ";" or by linebreaks:

$collection="http://tfs-abc/"

$stats = @{
'WS1' = tf stat /workspace:"WS1" /collection:$collection | select -last 1
'WS2' = tf stat /workspace:"WS2" /collection:$collection | select -last 1
}

$stats.Keys | ForEach-Object { Write-Host "$_ - $($stats[$_])" }

"," is used in powershell to seperate listitems like

$Var = @("a","b","c")

Upvotes: 1

Related Questions