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

How do I retrieve a list of primary users of computers

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

假设你的 csv 是这样的

您可以尝试使用以下 PowerShell 脚本:

$Computers = Import-Csv C:\computerlist.CSV
   ForEach ($Computer in $Computers) {
      $Name = (Get-CMUserDeviceAffinity -DeviceName $Computer.Name).uniqueusername
      write-host "$($computer.name) ------> $($Name) "
}

输出: