Home > other >  Skip Empty CSV cells for Set-ADUser script
Skip Empty CSV cells for Set-ADUser script

Time:10-16

I've been struggling to figure out the way to fix my script. I'm constantly getting 'Replace' errors for any user that has an empty cell in the CSV.

Example: We have some users that don't have mobile phones. The mobile phone cell for that user will be blank.

The 'Replace' I'm getting is only happening to users that don't have a value for every field.

I've linked the image of the csv columns below.

I've seen a few posts about resolving this, but none seem to match my case. Any help will greatly appreciated.

Thanks,

$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv


foreach( $user in $testusers )
{ Set-ADUser -Identity $user.sAMAccountName `
 -Title $user.Title `
 -Description $user.Title `
 -OfficePhone $user.TelephoneNumber `
 -MobilePhone $user.Mobile `
 -Fax $user.facismileTelephoneNumber `
 -StreetAddress $user.streetAddress `
 -City $user.city `
 -State $user.st `
 -PostalCode $user.postalCode `
}

csv columns

CodePudding user response:

In this case, you can use the alternate -Instance on Set-ADUser.

$TestUsers = Import-Csv -Path C:\Users\test.admin\Documents\ADDummyTestUsers.csv

foreach( $user in $testusers )
{
    $ADUser = Get-ADUser -Identity $user.sAMAccountName

    $ADUser.Title = $user.Title 
    $ADUser.Description = $user.Title 
    $ADUser.OfficePhone = $user.TelephoneNumber 
    $ADUser.MobilePhone = $user.Mobile 
    $ADUser.Fax = $user.facismileTelephoneNumber 
    $ADUser.StreetAddress = $user.streetAddress 
    $ADUser.City = $user.city 
    $ADUser.State = $user.st 
    $ADUser.PostalCode = $user.postalCode 

    Set-ADUser -Instance $ADUser
}

CodePudding user response:

The "Replace" error OP is referring to is something like:

Set-ADUser : replace At line:15 char:5

  • Set-ADUser -Instance $User
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [Set-ADUser], ADInvalidOperationException
    • FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

I'd suggest simply testing for a value before attempting to set it. I've demonstrated this below, while also changing the approach slightly (using instances)

$TestUsers = Import-Csv -Path C:\tmp\file.csv

$TestUsers | ForEach-Object {
    $User = $null
    $User = Get-ADUser -Identity $_.samaccountname -Properties description,telephonenumber,mobile,facsimiletelephoneNumber,streetaddress,city,st,postalcode

    if ('*' -like $_.description)              {$User.description              = $_.description}
    if ('*' -like $_.telephonenumber)          {$User.telephonenumber          = $_.telephonenumber}
    if ('*' -like $_.mobile)                   {$User.mobile                   = $_.mobile}
    if ('*' -like $_.facsimiletelephoneNumber) {$User.facsimiletelephoneNumber = $_.facsimiletelephoneNumber}
    if ('*' -like $_.streetaddress)            {$User.streetaddress            = $_.streetaddress}
    if ('*' -like $_.city)                     {$User.city                     = $_.city}
    if ('*' -like $_.st)                       {$User.st                       = $_.st}
    if ('*' -like $_.postalcode)               {$User.postalcode               = $_.postalcode}

    Set-ADUser -Instance $User
}
  • Related