question

dkarthi avatar image
1 Vote"
dkarthi asked dkarthi commented

How do I update MAC address in existing AD users

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-serverwindows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

For the benefit of forum users... Please edit your post and format the entire script as "Code Sample".

What statement fails?
What error do you get?
Have you researched the property? Is it read only?

1 Vote 1 ·

When you see something like this, why are you trying to modify the property???

"The msNPCallingStationID attribute is used internally. Do not modify this value directly."
"The msNPSavedCallingStationID attribute is used internally. Do not modify this value directly."

a-msnpcallingstationid
a-msnpsavedcallingstationid



1 Vote 1 ·

1 Answer

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered dkarthi commented

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}
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$samAccountName | Set-ADUser -Replace @{msNPCallingStationID=$msNPCallingStationID; msNPSavedCallingStationID=$msNPSavedCallingStationID}

the above sample code slightly modified and working for me. Thanks for the code sample.

1 Vote 1 ·