3

I'm attempting to add a custom column to the table output of some objects retrieved from our MECM instance.

The following code shows the precise format of the code, only gathering data from Get-Process instead.

$types = @{
     "chrome" = "This is Chrome"
     "winlogon" = "This is winlogon"
}

$procs = Get-Process | Select Name,Id | Where { ($_.Name -eq "winlogon") -or ($_.Name -eq "chrome") }
$procsCustom = $procs | Select Name,Id,@{
    Name = "TestColumn"
    Expression = {
        $name = $_.Name
        $types.$name
    }
}
$procsCustom  | Format-Table

This code functions as expected:

Name        Id TestColumn
----        -- ----------
chrome   12428 This is Chrome
chrome   12448 This is Chrome
chrome   12460 This is Chrome
winlogon   880 This is winlogon
winlogon  5076 This is winlogon

When I do the same thing for my actual code:

$refreshTypes = @{
    1 = "Manual Update Only"
    2 = "Scheduled Updates Only"
    4 = "Incremental Updates Only"
    6 = "Incremental and Scheduled Updates"
}

$colls = Get-CMCollection | Where { ($_.RefreshType -eq 4) -or ($_.RefreshType -eq 6) }
$collsCustom = $colls | Select Name,RefreshType,@{
    Name = "RefreshTypeFriendly"
    Expression = {
        $type = $_.RefreshType
        $refreshTypes.$type
    }
}
$collsCustom | Format-Table

The custom column is not populated:

Name         RefreshType RefreshTypeFriendly
----         ----------- -------------------
Collection 1           6
Collection 2           4

The following code shows that $_.RefreshType is being parsed correctly:

$collsCustom = $colls | Select Name,RefreshType,@{
    Name = "RefreshTypeFriendly"
    Expression = {
        $_.RefreshType
    }
}
$collsCustom  | Format-Table
Name         RefreshType RefreshTypeFriendly
----         ---------- -------------------
Collection 1          6                   6
Collection 2          4                   4

The following code forces a fake value for $type in the expression scriptblock and shows that the rest of the code works:

$collsCustom = $colls | Select Name,RefreshType,@{
    Name = "RefreshTypeFriendly"
    Expression = {
        $type = 1
        $refreshTypes.$type
    }
}
$collsCustom | Format-Table
Name         RefreshType RefreshTypeFriendly
----         ----------- -------------------
Collection 1           6 Manual Update Only
Colleciton 2           4 Manual Update Only

So why in the world does the intended code output nothing in the custom column? Given the examples I've provided above, I'm stumped.

FWIW I've also tried using the array syntax ($refreshTypes[$type]) instead of the object property syntax ($refreshTypes.$type) but I get the same behavior.

Thanks for your time.

Environment:
Win10 x64 20H2
Powershell 5.1

mmseng
  • 735
  • 9
  • 24

1 Answers1

3

Looks like your type isn't being interpreted as an int. Try casting $type to Int

Try this

Expression = {
    [int]$type = $_.RefreshType
    $refreshTypes.$type
}
Daniel
  • 4,792
  • 2
  • 7
  • 20
  • That did the trick. Thanks! That's strange, because I had also tried defining `$refreshTypes` using strings as keys (i.e. `"1" = "Manual Update Only"`), and it still didn't work. `$_.RefreshType.GetType()` returns `System.UInt32`, so maybe that's the issue? As a career IT pro, with nearly a decade since any CS classes, let's just say I'm not so good with types anymore. – mmseng Feb 12 '21 at 08:15
  • 1
    @mmseng, yes, that's the issue: Lookup keys must be of the exact same type as the actual keys in the hashtable. With string keys, you could have done `$refreshTypes."$type"` – mklement0 Feb 12 '21 at 14:25