0
votes

I am trying to write a script that will import a csv file and update a set of custom attributes that were built into our AD Schema.

Here is the basic code:

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}
}

Here is my Error Message:

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

For some reason if I write a basic command like below it works, but not in the array.

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

1 Answers

0
votes

First, the property you are looking for isn't dnsname but dnshostname or name. (If just using name, then skip using filter and pass it in as the identity)

Second, the filters on the AD cmdlets aren't straight forward. If using curly braces, then you can't use an expression. Double quotes then single quotes seems to work fine though.

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

Alternatively, define the expression as a variable.

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