在 Powershell 中更新自定义属性

Update Custom Attributes in Powershell

我正在尝试编写一个脚本来导入一个 csv 文件并更新一组内置到我们的 AD 架构中的自定义属性。

基本代码如下:

Import-Module ActiveDirectory

$USERS = Import-Csv c:\temp\test2.csv

foreach ($user in $users) {
    Get-ADComputer -filter {dnsname -eq $user.ComputerName} |
    Set-ADComputer -replace @{bSBPrimaryUser = $user.bSBPrimaryUser}
}

这是我的错误消息:

Set-ADComputer : Cannot bind parameter 'Replace' to the target. Exception setting "Replace": "Object reference not set to an instance of an object." At C:\Users\nwatson\Documents\Update Computers.ps1:7 char:24

  • Set-ADComputer -replace <<<< @{bSBPrimaryUser = $user.bSBPrimaryUser}}

    • CategoryInfo : WriteError: (:) [Set-ADComputer], ParameterBindingException

    • FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADComputer

出于某种原因,如果我编写如下所示的基本命令,它会起作用,但不会出现在数组中。

Set-ADComputer as-001  -replace @{ bSBPrimaryUser = "Joe Smith";}

首先,您要查找的 属性 不是 dnsname,而是 dnshostnamename。 (如果只使用名称,则跳过使用过滤器并将其作为 identity 传递)

其次,AD cmdlet 上的过滤器并不直接。如果使用大括号,则不能使用表达式。双引号然后单引号似乎工作正常。

Get-ADComputer -filter "dnshostname -eq '$($user.ComputerName)'"

或者,将表达式定义为变量。

$ComputerName = $user.ComputerName
Get-ADComputer -filter {dnshostname -eq $Computername}