在运行时指定 Select-对象详细信息

Specify Select-Object Details at Runtime

我正在使用 Get-WmiObject 通过以下结构动态查询 SCCM 名称空间:

$wmi = Get-WmiObject -ComputerName <SCCM Server> -Namespace Root\SMS\SITE_<ID> -Query $query -Credential $credential

有没有办法允许在运行时指定 Select-Object 详细信息?我尝试了以下但没有成功:

$properties = Read-Host 'Set Select-Object Properties'
...
$wmi | select $select | Export-Csv ${ENV:USERPROFILE}\Desktop$filename.csv

它所做的只是 return 我在 CSV 的第二行中输入的内容,但没有结果。每当我将属性硬编码到 select 语句中时,它都可以正常工作。

这是一个Gist with the full script signature

您需要 Properties 是一个数组。您可以这样做,这将允许用户指定 space 或逗号分隔列表。

$properties = (Read-Host 'Set Select-Object Properties') -split ' +|, *'