如何将 CSV 文件的对象通过管道传输到 Get-CMUserDeviceAffinity 的 DeviceName 参数中?
我有一个计算机列表。我想为此列表中的每台计算机生成一个主要用户列表。我正在使用 powershell 和 SCCM 模块,到目前为止,我已经尝试过单线以及更冗长的东西无济于事。
单线失败:
Import-CSV C:\temp\computerlist.csv | Get-CMUserDeviceAffinity -DeviceName $($_.Name)
脚本失败:
$Computers = C:\computers.CSV
ForEach ($Computer in $Computers) {
Get-CMUserDeviceAffinity -DeviceName $_.Name
两者都失败的结果:
Cannot validate argument on parameter 'DeviceName'. The argument is null or empty. Provide
an argument that is not null or empty, and then try the command again.
At line:1 char:77
+ ... p\computerlist.csv | Get-CMUserDeviceAffinity -DeviceName $($_.Name)
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-CMUserDeviceAffinity], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ConfigurationManagement.Cmdlets.Collections.C
ommands.GetUserDeviceAffinityCommand
最佳答案
您正在使用自动管道变量 $_
而您的 foreach 循环中没有管道对象。只需使用 $Computer
反而:
$Computers = C:\computers.CSV
ForEach ($Computer in $Computers) {
Get-CMUserDeviceAffinity -DeviceName $Computer.Name
}
关于list - 如何检索计算机的主要用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41174800/