0

I am trying to find a way to save the properties for a select statement in PowerShell but it isn't working. I haven't found a way to make an entire statement a literal so that it isn't reviewed until the variable is opened.

Here is what works:

$wsus.GetSummariesPercomputerTarget($CurrentMonthUpdateScope, $ComputerScope) |
    Select-Object @{L="WSUSServer";E={$Server}},
        @{L="FromDate";E={$($CurrentMonthUpdateScope.FromCreationDate).ToString("MM/dd/yyyy")}},
        @{L="ToDate";E={$($CurrentMonthUpdateScope.ToCreationDate).ToString("MM/dd/yyyy")}},
        @{L='Computer';E={($wsus.GetComputerTarget([guid]$_.ComputerTargetID)).FullDomainName}},
        DownloadedCount,
        NotInstalledCount,
        InstalledPendingRebootCount,
        FailedCount,
        Installedcount |
    Sort-Object -Property "Computer"

and I am trying to get the properties mentioned (starting just after the Select-Object statement and ending just before the last pipe) placed in a variable so that I can use the same properties multiple times with different scopes.

I have tried this:

$Properties = '@{L="WSUSServer";E={$Server}},
@{L="FromDate";E={$($CurrentMonthUpdateScope.FromCreationDate).ToString("MM/dd/yyyy")}},
@{L="ToDate";E={$($CurrentMonthUpdateScope.ToCreationDate).ToString("MM/dd/yyyy")}},
@{L="Computer";E={($wsus.GetComputerTarget([guid]$_.ComputerTargetID)).FullDomainName}},
DownloadedCount,
NotInstalledCount,
InstalledPendingRebootCount,
FailedCount,
Installedcount'

$wsus.GetSummariesPercomputerTarget($CurrentMonthUpdateScope, $ComputerScope) |
    Select-Object $Properties |
    Sort-Object -Property "Computer"

While this runs it doesn't give any data and I think it confuses PowerShell.

This gives the same response:

$Properties = "@{L=`"WSUSServer`";E={$Server}},
@{L=`"FromDate`";E={$($CurrentMonthUpdateScope.FromCreationDate).ToString(`"MM/dd/yyyy`")}},
@{L=`"ToDate`";E={$($CurrentMonthUpdateScope.ToCreationDate).ToString(`"MM/dd/yyyy`")}},
@{L=`"Computer`";E={($wsus.GetComputerTarget([guid]$_.ComputerTargetID)).FullDomainName}},
DownloadedCount,
NotInstalledCount,
InstalledPendingRebootCount,
FailedCount,
Installedcount"

Any options, thoughts, etc.?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
J.C.
  • 143
  • 1
  • 8
  • Look at the value of your `$Properties` variable and you should see the problem right away. Single-quoted strings do not interpolate `$(...)` expressions. You'll need to use a double-quoted string instead (and escape your embedded quotes with backticks). – Joe White Dec 15 '16 at 18:53
  • I tried it with double-quotes as well and it gives the same response. – J.C. Dec 15 '16 at 19:16
  • Update: I updated the original question to include the $Properties using double-quotes and backticks - same response as the first version... – J.C. Dec 15 '16 at 19:20
  • Can you try splatting it into one big @{}. Rather than using the calculated properties format, use the splatting format with script blocks? For example: $Properties = @{'WSUSServer'='$Server'; 'FromDate'='{$($CurrentMonthUpdateScope.FromCreationDate).ToString(`"MM/dd/yyyy`")}'; } I'm not sure if it works. More info on splatting here: https://technet.microsoft.com/en-us/library/gg675931.aspx?f=255&MSPPError=-2147217396 Thanks, Tim. – Tim Haintz Dec 15 '16 at 19:41

1 Answers1

2

The -Property argument of Select-Object expects an array, not a string. So something like this:

$Properties = @(@{L="WSUSServer";E={$Server}},
                @{L="FromDate";E={$($CurrentMonthUpdateScope.FromCreationDate).ToString("MM/dd/yyyy")}},
                @{L="ToDate";E={$($CurrentMonthUpdateScope.ToCreationDate).ToString("MM/dd/yyyy")}},
                @{L="Computer";E={($wsus.GetComputerTarget([guid]$_.ComputerTargetID)).FullDomainName}},
                "DownloadedCount",
                "NotInstalledCount",
                "InstalledPendingRebootCount",
                "FailedCount",
                "Installedcount")

Note, you will need to turn the simple property names into strings within your array.

zdan
  • 28,667
  • 7
  • 60
  • 71
  • So essentially you would be wrapping all of the items in the properties variable into a array using the @(...). Is that correct (or am I missing something else there)? I suppose I don't need the backticks in that example (they were only there to escape the double-quotes. – J.C. Dec 15 '16 at 19:48
  • That worked (after removing the backticks). Thanks! – J.C. Dec 15 '16 at 22:32
  • The `@()` is optional. A comma-separated list would suffice. The important part is to not put the whole list in quotes (so that it doesn't become a single string). – Ansgar Wiechers Dec 15 '16 at 23:34