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

问题描述:

如何将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 
+2

'Import-CSV C:\ temp \ computerlist.csv | ForEach-Object {Get-CMUserDeviceAffinity -DeviceName $ _。Name}' – sodawillow

您正在使用自动流水线 varaible $_,而不必在你的foreach循环管道对象。只需使用$Computer代替:

$Computers = C:\computers.CSV 
    ForEach ($Computer in $Computers) { 
     Get-CMUserDeviceAffinity -DeviceName $Computer.Name 
} 
+0

至少它应该是'$ Computers = Import-Csv'C:\ computers.CSV'' – n01d

假设您的CSV是这样
enter image description here

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

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

输出:
enter image description here

+0

你能解决这个问题吗? –