Reputation: 55
I'm trying to Run Advertised Programs using PowerShell
$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
| Select-Object -Property PKG_Manufacturer, PKG_Name, PKG_MIFVersion
The output will be:
PKG_Manufacturer PKG_Name PKG_MIFVersion
---------------- -------- --------------
Microsoft Word v1234
Google Chrome v987
Microsoft Excel v987
etc
How do I concatenate it into a string? I tried this:
[string[]]$result = $tpObject.PKG_Manufacturer + $tpObject.PKG_Name + " - " + $tpObject.PKG_MIFVersion
$result
But it display all the PKG_Manufacturer, then PKG_Name, then PKG_MIFVersion
I would like it to display this, Microsoft Word - v1234 as a string?
Any suggestions or comments would be greatly appreciated.
tks
Upvotes: 1
Views: 3083
Reputation:
$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
$tpobject | ForEach-Object{
"{0} {1} - {2}" -f $_.PKG_Manufacturer, $_.PKG_Name, $_.PKG_MIFVersion
}
See details of the -f
format operator
Sample output:
Microsoft Word - v1234
Google Chrome - v987
Microsoft Excel - v987
Upvotes: 0
Reputation: 1247
Give this a try:
$result=@()
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution | %{
$result += "$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)"}
$result
Upvotes: 1
Reputation: 18950
I guess what you want is something like this
$list = New-Object 'System.Collections.Generic.List[string]'
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
| ForEach-Object $list.Add("$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)")
To concatenate and aggregate the values in a string array you have several options.
First, you can concatenate a string:
+
operator (as applied by you)-f
formatting approach (like in C#) "My PC {0} has {1} MB of memory." -f "L001", "4096"
$x = "Max"; Write-Output "I'm $x"
(Hind: Sometimes you need sub-expressions expansion, as shown in my example above. See the Windows PowerShell Language Specification Version 3.0, p34).Second, you can aggregate your results:
$testArray = @()
with $testArray += $_
[string[]]$testArray = [string[]]
also with +=
as shown below.$list = New-Object 'System.Collections.Generic.List[string]'
with the Add-Method $list.Add($_)
And there is more...In general, I would try to stay with the PS Objects as long as possible.
Aggregating stuff in a string array to process it later is too much (C#) programmer thinking.
In your Code:
1. You cannot add a new array element with the =
operator, use +=
instead:
$testArray = @()
$tempArray = "123", "321", "453"
$tempArray | ForEach {$testArray += $item }
$testArray
Upvotes: 0