2016-12-15 23 views
1

如何將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

回答

5

您正在使用自動流水線 varaible $_,而不必在你的foreach循環管道對象。只需使用$Computer代替:

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

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

0

假設您的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

你能解決這個問題嗎? –

相關問題