How do I update MAC address in existing AD users

dkarthi 21 Reputation points
2020-12-30T13:54:34.187+00:00

The below script can't work while updating the MAC addresses for existing AD users. If anybody knows kindly resolve these issues.

$ErrorActionPreference = "SilentlyContinue"

function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="CSV Files (.csv)|.csv")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$objForm.ShowHelp = $true

$Show = $objForm.ShowDialog()

If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Exit
}
}

$FileName = Select-FileDialog -Title "Import an CSV file" -Directory "c:\"

$ExchangeUsersOU = "xyz"

$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
$DomainDN = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName
$final = "LDAP://$DomainDN"
$DomainPath = [ADSI]"$final"
$cOU = $DomainPath.Create("OrganizationalUnit",$ExchangeUsersOU)
$cOU.SetInfo()

$UserInformation = Import-Csv $FileName

$OUPath = "LDAP://$ExchangeUsersOU,$DomainDN"
$UserPath = [ADSI]"$OUPath"
Write-Host "---------------------------------------------------------------"
Write-Host "xyz"
Write-Host ""
Write-Host "---------------------------------------------------------------"

Foreach ($User in $UserInformation){

$CN = $User.cn
$Given = $User.givenName
$samAccountName = $User.samAccountName
$msNPCallingStationID = $User.msNPCallingStationID
$msNPSavedCallingStationID = $User.msNPSavedCallingStationID

$LABUser = $UserPath.Update("User","CN=$CN")
Write-Host "Update User: $User.samAccountName"
$LABUser.Put("samAccountName",$samAccountName)
$LABUser.Put("cn",$cn)
$LABUser.Put("givenName",$Given)
$LABUser.Put("msNPCallingStationID",$msNPCallingStationID)
$LABUser.Put("msNPSavedCallingStationID",$msNPSavedCallingStationID)

$LABUser.SetInfo()

$Pwrd = $User.Password

$LABUser.psbase.invoke("setPassword",$Pwrd)
$LABUser.psbase.invokeSet("AccountDisabled",$False)
$LABUser.psbase.CommitChanges()

}
Write-Host "Script Completed"

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,730 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,629 questions
{count} votes

Accepted answer
  1. Rich Matheisen 47,601 Reputation points
    2020-12-31T21:07:24.063+00:00

    I'm curious. Have you tried something like this? I don't have an AD to work with (although I spent 16 years working with them!), so I can't test this.

    $UserInformation = Import-Csv $FileName |
        ForEach-Object{
            $Params = @{
                Name = $_.cn
                GivenName = $_.givenName
                SamAccountName = $_.samAccountName
                Path = "OU=xyz,DC=MyRootDomain,DC=LOCAL"
            }
    
            $newuser = New-ADUser @Params -Passthru
            # NOT ADDRESSED: setting user password
            # $newuser = $newuser | Set-ADAccountPassword ... -Passthru
            $newUser | Set-ADUser -Replace @{msNPCallingStationID=$_.msNPCallingStationID; msNPSavedCallingStationID=$_.msNPSavedCallingStationID}
        }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.