Skip to content

Instantly share code, notes, and snippets.

@thegooddoctorgonzo
Created November 15, 2021 03:57
Show Gist options
  • Select an option

  • Save thegooddoctorgonzo/ae49a248fee6d27862da8fe327ffc243 to your computer and use it in GitHub Desktop.

Select an option

Save thegooddoctorgonzo/ae49a248fee6d27862da8fe327ffc243 to your computer and use it in GitHub Desktop.
:
Start-Transcript -Path "C:\Scripts\Populate-AttributeField\Populate-AttributeField.log" -Append -Force
$Error.Clear()
$Header = (Get-Date -Format yyyyMMdd-HH:mm:ss.ff).toString() + " -- INFO -- Script: " + $MyInvocation.MyCommand.Source
$NotificationLog = new-object system.collections.arraylist
. PATH\Functions\Write-ToMasterLog.ps1
. PATH\Functions\Collect-Errors.ps1
. PATH\Functions\Get-CurrentLine.ps1
try{
$OUs = Get-ADOrganizationalUnit -SearchBase "OU=PEOAVN,OU=PEO Aviation Users,DC=Ad,DC=DOMAIN" -Filter * | Where-Object {$_.name -eq "Users" -and $_.distinguishedname -notlike "*test*"}
}
catch{
Collect-Errors -LogLines $_ -Label "Error building list of OUs" -Level FATAL -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog)
Write-ToMasterLog -Entries $NotificationLog -Header $Header
exit
}
foreach($OU in $OUs)
{
try{
$users = Get-ADUser -SearchBase $OU -Filter * -properties extensionattribute1
}
catch{
Collect-Errors -LogLines $_ -Label ("Error building list of users in " + $OU) -Level FATAL -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog)
Write-ToMasterLog -Entries $NotificationLog -Header $Header
exit
}
foreach($user in $users)
{
$extAttr1 = $user.extensionattribute1
$PMO = $ou.DistinguishedName.Split(",")[1].split("=")[1]
if($extAttr1 -ne $PMO){
Set-ADObject -Identity $user -replace @{"extensionattribute1" = $PMO}
if($Error){
Collect-Errors -LogLines $Error -Label ("Error changing ExtensionAttribute for " + $user + " from $($extAttr1) to $($PMO)") -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog)
}
else{
$NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).toString() + " -- INFO -- User $($user.Name) successfully updated ExtensionAttribute1 from $($extAttr1) to $($PMO)") | Out-Null
}
}
#Commented out - very excessive logging
<#else{
$NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).toString() + " -- INFO -- User $($user.Name) does not require update of ExtensionAttribute1") | Out-Null
} #>
}
}
Collect-Errors -LogLines $Error -Level ERROR -LineNum (Get-CurrentLine) -PassedList ([ref]$NotificationLog)
$NotificationLog.Add((Get-Date -Format yyyyMMdd-HH:mm:ss.ff).ToString() + " -- INFO -- Script EXITING normally") | Out-Null
Write-ToMasterLog -Entries $NotificationLog -Header $Header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment