Почему это не работает? - Попытка сохранить свойства в переменной для многократного использования в функции
Я пытаюсь найти способ сохранить свойства для select
заявление в PowerShell, но оно не работает. Я не нашел способа сделать весь оператор буквальным, чтобы он не просматривался до тех пор, пока не будет открыта переменная.
Вот что работает:
$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"
Есть варианты, мысли и т. Д.?
1 ответ
-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")
Обратите внимание, вам нужно будет превратить простые имена свойств в строки в вашем массиве.