0

我正在尝试找到一种方法来保存属性selectPowerShell 中语句的属性,但它不起作用。我还没有找到一种方法来使整个语句成为文字,以便在打开变量之前不会对其进行审查。

这是有效的:

$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"

我正在尝试获取提到的属性(在Select-Object语句之后开始并在最后一个管道之前结束)放置在一个变量中,以便我可以在不同的范围内多次使用相同的属性。

我试过这个:

$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"

虽然它运行它不提供任何数据,我认为它混淆了 PowerShell。

这给出了相同的响应:

$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"

有什么选择、想法等吗?

4

1 回答 1

2

-Property参数Select-Object需要一个数组,而不是字符串。所以是这样的:

$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")

请注意,您需要将简单的属性名称转换为数组中的字符串。

于 2016-12-15T19:30:11.390 回答