list - 如何检索计算机的主要用户列表

标签 list powershell wmi sccm import-csv

如何将 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/

相关文章:

python - 如何将列表中元组的第 i 个元素映射到另一个列表中的键以形成字典

python - 打印格式化的值集列表

powershell - Copy-Item 将项目移动到另一个位置而不创建父目录

java - 如何使用 Powershell 编辑 Tomcat 选项?

python - 在不同的属性上调用 del 时,对象的列表属性会发生变化

java - 重新实例化 List 或调用 clear() 的更好做法

powershell - 命令和ScriptBlock Powershell之间的区别

c# - WMI 太慢了

c# - .net 中的网络打印机监控

c# - 在远程计算机 WMI C# 上搜索文件