1

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

wp78de
  • 18,207
  • 7
  • 43
  • 71
user1736786
  • 139
  • 1
  • 4
  • 11
  • It's about concatenation and aggregation, isn't it. Otherwise this would be just a run of the mill duplicate of How do I concatenate strings and variables in PowerShell? https://stackoverflow.com/questions/15113413/how-do-i-concatenate-strings-and-variables-in-powershell – wp78de Sep 12 '17 at 20:47

3 Answers3

1

Give this a try:

$result=@()
Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution | %{
$result += "$($_.PKG_Manufacturer) $($_.PKG_Name) - $($_.PKG_MIFVersion)"}
$result
ShanayL
  • 1,217
  • 2
  • 14
  • 29
0
$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
  • Code-only answers are discouraged because they do not explain how they resolve the issue in the question. Consider updating your answer to explain what this does and how it addresses the problem. Please review [How do I write a good answer](https://stackoverflow.com/help/how-to-answer) – FluffyKitten Sep 12 '17 at 21:25
  • @FluffyKitten In general I'd agree - but in the context of this question and my very similar answer I don't think I've to express the very obvious. –  Sep 13 '17 at 15:30
  • Thanks for updating your answer. It had been flagged as low quality, so I added this comment during the review process. – FluffyKitten Sep 14 '17 at 03:07
0

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:

  1. You can concat strings in PowerShell by using the + operator (as applied by you)
  2. You can use the -f formatting approach (like in C#) "My PC {0} has {1} MB of memory." -f "L001", "4096"
  3. Or you can use a double quoted string with variables $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:

  1. Using a dynamically-typed array $testArray = @() with $testArray += $_
  2. Using a typed array [string[]]$testArray = [string[]] also with += as shown below.
  3. Using a generic List $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
wp78de
  • 18,207
  • 7
  • 43
  • 71