Powershell 为什么这不起作用尝试在变量中保存属性以在函数中多次使用

Powershell 为什么这不起作用尝试在变量中保存属性以在函数中多次使用,powershell,select-object,Powershell,Select Object,我正试图找到一种方法来保存PowerShell中select语句的属性,但它不起作用。我还没有找到一种方法使整个语句成为一个文本,以便在打开变量之前不会对其进行检查 以下是有效的方法: $wsus.GetSummariesPercomputerTarget($CurrentMonthUpdateScope, $ComputerScope) | Select-Object @{L="WSUSServer";E={$Server}}, @{L="FromDate";E={$(

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

以下是有效的方法:

$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"
我试图将所提到的属性(从
selectobject
语句之后开始,在最后一个管道之前结束)放在一个变量中,这样我就可以在不同的范围内多次使用相同的属性

我试过这个:

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

任何选项、想法等?

选择对象的
-Property
参数需要数组,而不是字符串。比如说:

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

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

选择对象的
-property
参数需要数组,而不是字符串。比如说:

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

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

查看
$Properties
变量的值,您应该马上看到问题所在。单引号字符串不会插入
$(…)
表达式。您将需要使用双引号字符串(并用反勾号转义嵌入的引号)。我也用双引号尝试了它,它给出了相同的回答。更新:我更新了原始问题,使用双引号和反勾号包含$Properties-与第一个版本相同的回答…您可以尝试将其拆分为一个大@{}. 不要使用计算属性格式,而是使用脚本块的散点格式?例如:$Properties=@{'WSUSServer'='$Server';'FromDate'='{$($CurrentMonthUpdateScope.FromCreationDate).ToString(
“MM/dd/yyyyy
”)}我不确定它是否有效。更多关于splatting的信息:谢谢,Tim。请查看
$Properties
变量的值,您应该会立即看到问题所在。单引号字符串不会插入
$(…)
表达式。您将需要使用双引号字符串(并用反勾号转义嵌入的引号)。我也用双引号尝试了它,它给出了相同的回答。更新:我更新了原始问题,使用双引号和反勾号包含$Properties-与第一个版本相同的回答…您可以尝试将其拆分为一个大@{}. 不要使用计算属性格式,而是使用脚本块的散点格式?例如:$Properties=@{'WSUSServer'='$Server';'FromDate'='{$($CurrentMonthUpdateScope.FromCreationDate).ToString(
“MM/dd/yyyyy
”)}我不确定它是否有效。这里有更多关于splatting的信息:谢谢,Tim。因此本质上,您将使用@(…)将properties变量中的所有项包装到一个数组中。这是正确的吗(还是我遗漏了其他东西)?我想在那个例子中我不需要反勾号(它们只是用来转义双引号。这很有效(在删除反勾号之后)。谢谢!
@()
是可选的。逗号分隔的列表就足够了。重要的是不要将整个列表放在引号中(这样它就不会变成单个字符串)。因此,本质上,您将使用@(…)将properties变量中的所有项包装到一个数组中。这是否正确(或者我是否遗漏了其他内容)?我想在该示例中我不需要反勾号(它们仅用于转义双引号)。这很有效(在删除反勾号之后)。谢谢是可选的。逗号分隔的列表就足够了。重要的是不要将整个列表放在引号中(这样它就不会变成单个字符串)。