Update 'Update_Departments_By_Cost_Center_ID.ps1'

This commit is contained in:
Tre Hayes 2022-01-05 07:16:37 -06:00
parent 605582de98
commit ef79d32b19

View File

@ -26,25 +26,27 @@ $users = Get-ADUser -Filter * -Properties DeptCode,VP,Department,Office | Select
#loop through all departments $deptCodes
foreach ($dept in $deptCodes) {
$Wildcard_VP = ($dept.'Vice President').Replace('.','*').Replace(' ','') #Replace 'FirstInitial. LastName' with 'FirstInitial*LastName' and remove any extra spaces
$Wildcard_VP = ($dept.'Vice President').Replace(', M.D.', '').Replace(' ','').Replace('.','*') #Replace 'FirstInitial. LastName' with 'FirstInitial*LastName' and remove any extra spaces
$vp = Get-ADUser -Filter "Name -like '$wildcard_VP'" #Get AD object for the VP of the department
$deptCode = $dept.'DEPT.' #Variable containing the department code
#loop through all users. Set department, Office, VP attribute. Export CSV logging changes for each user
$seconds += (Measure-Command {foreach ($user in $users) {
if ($user.DeptCode -eq $deptCode) {
$user | Add-Member -MemberType NoteProperty -Name 'New Department' -Value $dept.DEPARTMENT #Add 'New Department' column to table
$user | Add-Member -MemberType NoteProperty -Name 'New Office' -Value $dept.DEPARTMENT #Add 'New Office' column to table
$user | Add-Member -MemberType NoteProperty -Name 'New VP' -Value $vp #Add 'New VP' column to table
Write-Host $($user | Format-Table | Out-String)
#If user department name != correct department name OR user office name != correct office name OR user VP != current department VP
foreach ($user in $users) {
if ($user.nkchDeptCode -eq $deptCode) {
# If user department name != correct department name OR user office name != correct office name OR user VP != current department VP
if ($user.Department -ne $dept.DEPARTMENT -or $user.Office -ne $dept.DEPARTMENT -or $user.VP -ne $vp) {
if ($vp -is [array]) {
$temp_vp = $($vp)
$vp = $user.VP
Write-Host "Updating $($user.Name)`'s VP from $temp_vp TO:`n $vp`n`n"
}
$user | Add-Member -MemberType NoteProperty -Name 'New Department' -Value $dept.DEPARTMENT #Add 'New Department' column to table
$user | Add-Member -MemberType NoteProperty -Name 'New Office' -Value $dept.DEPARTMENT #Add 'New Office' column to table
$user | Add-Member -MemberType NoteProperty -Name 'New VP' -Value $vp #Add 'New VP' column to table
$user | Export-Csv $employees_Updated -Append -NoTypeInformation
Set-ADUser -Identity $user.SamAccountName -Department $dept.DEPARTMENT -Office $dept.DEPARTMENT -Replace @{VP = $vp} -WhatIf
}
}
}}).TotalSeconds
$seconds
}
}
Remove-Item $DeptList_XLS