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

1
  • 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? stackoverflow.com/questions/15113413/… Commented Sep 12, 2017 at 20:47

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

This only shows a way to concatenate a string but not how to aggregate it.
Wouldn't that just be $i += Get-WmiObject [...] } $i?
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

Comments

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

3 Comments

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
@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.
Thanks for updating your answer. It had been flagged as low quality, so I added this comment during the review process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.