Search code examples
powershellvariablesselect-object

Variables not working as expected inside Select-Object expression statement


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


Solution

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

    Try this

    Expression = {
        [int]$type = $_.RefreshType
        $refreshTypes.$type
    }