【问题标题】:How do I retrieve a list of primary users of computers如何检索计算机的主要用户列表
【发布时间】:2017-05-01 16:24:48
【问题描述】:

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

【问题讨论】:

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

标签: list powershell wmi sccm import-csv


【解决方案1】:

您正在使用 自动管道 变量 $_,而您的 foreach 循环中没有管道对象。只需改用$Computer

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

【讨论】:

  • 至少应该是$Computers = Import-Csv 'C:\computers.CSV'
【解决方案2】:

假设你的 csv 是这样的

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

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

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    相关资源
    最近更新 更多