diff --git a/Update_Departments_By_Cost_Center_ID.ps1 b/Update_Departments_By_Cost_Center_ID.ps1 new file mode 100644 index 0000000..3668e71 --- /dev/null +++ b/Update_Departments_By_Cost_Center_ID.ps1 @@ -0,0 +1,50 @@ +Invoke-WebRequest 'https:///DEPTLIST_for_Intranet.xls' -OutFile '\DEPTLIST_for_Intranet.xls' #download Department List from Intranet +$DeptList_XLS = '\DEPTLIST_for_Intranet.xls' +$log_Time = Get-Date -Format "MM-yyyy" +$DeptList_CSV = '\DEPTLIST_for_Intranet_' + $log_Time + '.csv' +$employees_Updated = '\Employees_Updated_' + $log_Time + '.csv' + +$excel = New-Object -ComObject excel.application -Property @{Visible = $false} #open Excel in the background +$row = 1 #starting row position +Start-Sleep -Seconds 1 #pause for 1 second + +#Remove the first 5 rows and any blank rows +Add-Type -AssemblyName "\Microsoft.Office.Interop.Excel.dll" #Add Microsoft.Office.Interop.Excel.dll for excel functionality +$workbook = $excel.Workbooks.Open("$DeptList_XLS") #open DEPTLIST_XLS file +$sheet = $workbook.Sheets.Item(1) #select sheet 1 +while($sheet.Cells.Item(1, 1).Value() -ne 'DEPT.') {[void]$sheet.Cells.Item(1, 1).EntireRow.Delete()} #Remove the first 5 rows +while ($row -le $sheet.UsedRange.Rows.Count) {if([string]::IsNullOrEmpty($sheet.Cells.Item($row, 1).Value())) {[void]$sheet.Cells.Item($row, 1).EntireRow.Delete(); $row--}; $row++} #remove blank rows. if one is removed, check same row again +$workbook.SaveAs("$DeptList_CSV",[Microsoft.Office.Interop.Excel.XLFileFormat]::xlCSV) #save XLS as CSV +$workbook.close($true) #close file +$excel.Quit() #close excel + +$deptCodes = Import-Csv -Path $DeptList_CSV #import new csv for following script use +$deptCodes #print department codes to terminal + +$seconds = 0 +$users = Get-ADUser -Filter * -Properties DeptCode,VP,Department,Office | Select-Object DeptCode,SamAccountName,Name,Department,Office,VP #create local list of all AD users + +#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 + $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 + if ($user.Department -ne $dept.DEPARTMENT -or $user.Office -ne $dept.DEPARTMENT -or $user.VP -ne $vp) { + $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 \ No newline at end of file