I recently had to perform an Office 365 migration for a client with hundreds of users / mailboxes with a terrible assortment of mismatched on-premises Active Directory attributes.
The script below is a script I spent quite a while writing that will fully clean up AD attributes and prepare your users for migration to Office 365 from on-prem Exchange.
The script can be downloaded below, and I’ve posted the raw code as well for review:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
<# .SYNOPSIS CleanUpADAttributesforO365.ps1 - Cleanly formats AD attributes for a migration to Office 365 .DESCRIPTION This powershell script fully cleans up the following attributes and prepares users to be synced to Office 365 using Microsoft AD Connect: ♦Attribute: SamAccountName ○ Converts the current user SamAccountName attribute to lower case. No other modifications are made to this attribute. ♦Attribute: mail ○ Converts the current user mail attribute to lower case. No other modifications are made to this attribute. ♦Attribute: mailNickname ○ Converts the current user mailNickname attribute to lower case. No other modifications are made to this attribute. ♦Attribute: UserPrincipalName ○ Converts the current user UserPrincipalName attribute to lower case and changes the UPN ♦Attribute: proxyAddresses ○ Converts all of the current user proxyAddresses to lower case and adds an additional proxyAddress for the new Office 365 .onmicrosoft.com URL, using their primary email alias as the alias for the .onmicrosoft.com new proxyAddress .NOTES ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ │ ORIGIN STORY │ ├─────────────────────────────────────────────────────────────────────────────────────────────┤ │ DATE CREATED : 2020.02.07 │ │ DATE MODIFIED : 2020.02.07 │ │ AUTHOR : Vince Cantrell │ │ DESCRIPTION : Initial Draft │ │ VERSION : 1.3 │ └─────────────────────────────────────────────────────────────────────────────────────────────┘ Please see the SWITCHES and VARIABLES sections below. #> ################################################################################################# <# ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ │ SWITCHES │ ├─────────────────────────────────────────────────────────────────────────────────────────────┤ │ Review the 12 switches below and set them to true or false based on your needs. │ │ Note: The bottom two switches cannot both be set to True (for the mail attribute), │ │ pick only one. │ └─────────────────────────────────────────────────────────────────────────────────────────────┘ #> #Switches for adding new domains / proxyAddresses. These can all be set to true: #Add your O365 tenant domains to your on-premis AD domain as additional UPN suffixes - recommended: $addO365DomainstoUPNSuffixesforAD = $true #Set the switch below to true if you want to add a new proxy address for the .onmicrosoft.com domain of your O365 tenant: $addNewProxyAddressesForO365Tenant = $true ###Switches for converting variables to lower-case. These can all be set to true:### #Set the switch below to true if you want to convert all proxyAddresses to lower-case: $convertProxyAddressestoLowerCase = $true #Set the switch below to true if you want to convert their mailNickname attribute to lower case: $convertMailNicknametoLowerCase = $true #Set the switch below to true if you want to convert their SamAccountName attribute to lower case: $convertSamAccountNametoLowerCase = $true #Set the switch below to true if you want to convert their UserPrincipalName attribute to lower case: $convertUserPrincipalNametoLowerCase = $true #Set the switch below to true if you want to convert their mail attribute to lower case: $convertMailAttrtoLowerCase = $true #Switches for changing existing paramaters to new values using the variables you specify in the Variables section: #Set the switch below to true if you want to replace the user UPN with a new domain specified below: $changeUserUPNtoNewDomain = $true #Set the switch below if you want to change their primary SMTP address to their UserPrincipalName @ the new UPN suffix you specify below: $changePrimaryProxyAddrToUPNatNewDomain = $true #Set the switch below if you want to set the mail attribute for each user to match their UserPrincipalName @ The new UPN suffix you specify below: $changeMailAttributetoUPNwithNewDomain = $true #Set the switch below to true if you want to set their mail attribute to match their primary SMTP proxyAddress (in case it is different): $changeMailAttributeToPrimaryProxyAddr = $false ################################################################################################# <# ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ │ VARIABLES │ ├─────────────────────────────────────────────────────────────────────────────────────────────┤ │ Review the variables below and set them based on your needs. │ │ Note: If you change the log file paths to have spaces, you need to enclose them in quotes.│ │ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘ #> #Set the variable below to the OU that you want to search within for selecting the users to modify: $userOU = "OU=SCA Users,DC=sca,DC=titan,DC=1sourcing,DC=net" #Set the variable below to the DN of the security group that you want to search within for selecting the users to modify: $groupDN = "CN=TestGroup,CN=Users,DC=siskinspine,DC=com" #Fill out the variable below if you want to filter a user search by users that already have a valid proxyAddress matching the domain suffix below. $searchDomain = "siskinspine.com" #Fill out the variable below if you would like to add a new UPN suffix to the domain / forest for changing the user UPN. $newUPNsuffix = "siskinspine.com" #Fill out the two variables below with your O365 tenant domains name without the @ if you chose to add a new SMTP Proxy Address in the above switches: $newO365TenantDomain = "siskinspine.onmicrosoft.com" $secondNewO365TenantDomain = "mail.siskinspine.onmicrosoft.com" #Set the variable below to the location for the master log file: $masterLogFile = "C:\temp\ADCleanupMasterLog.log" #Set the variable below to the location for the log file containing the unmodified settings for each user before we change them: $usersBeforeChangeLogFile = "C:\temp\UsersBeforeChange.log" #Set the variable below to the location for the log file containing the "after" version of the settings for each user to compare with the before file: $usersAfterChangeLogFile = "C:\temp\UsersAfterChange.log" ################################################################################################# <# ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ │ Search OU │ ├─────────────────────────────────────────────────────────────────────────────────────────────┤ │ Use the uncommented line below if you want to search by group. │ │ If you want to search by user OU, comment the first line and uncomment the second line │ │ Alternatively, if you want to filter a users search by users that already have an │ │ existingproxyAddress that matches a given domain, comment the first two lines and │ │ uncomment the third line, making sure you filled out the $searchDomain variable. │ │ You do not need to fill out the $userOU or $groupDN variables when using the third line. │ └─────────────────────────────────────────────────────────────────────────────────────────────┘ #> $usersInitial = Get-ADUser -filter {Memberof -eq $groupDN} -Properties * | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname #$usersInitial = Get-ADUser -Filter * -SearchBase $userOU -Properties * | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname #$usersInitial = Get-ADUser -Filter * -Properties * | Where-object {$_.ProxyAddresses -clike "SMTP:*@$searchDomain"} | Select-Object Name, SamAccountName, mail, ProxyAddresses, UserPrincipalName, mailNickname <# ┌─────────────────────────────────────────────────────────────────────────────────────────────┐ │ END OF CUSTOMIZATION! │ ├─────────────────────────────────────────────────────────────────────────────────────────────┤ │ │ │ !!!!!!!!!!!! DO NOT MODIFY ANYTHING BELOW THIS LINE !!!!!!!!!!!! │ │ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘ #> ################################################################################################# ################################################################################################# #PRESECTION: Start of script. Get domain and UPN Suffixes and set the timestamp for all log files. ################################################################################################# ################################################################################################# #First things first, check our dependencies: if ($changeMailAttributeToPrimaryProxyAddr -and $changeMailAttributetoUPNwithNewDomain) { Write-Warning "You have set both of the 'change' switches for the mail attribute to 'True' - Only one can be set to true. Please fix or the script will not run." Write-Warning "Exiting" EXIT } #Get the current time and format it for the log file names: $timestamp = Get-Date -UFormat "%Y-%m-%d--%T" | ForEach-Object { $_ -replace ":", "-" } #Append the date and time to the master log file: $masterLogDirName = [io.path]::GetDirectoryName($masterLogFile) $masterLogFileName = [io.path]::GetFileNameWithoutExtension($masterLogFile) $masterLogExt = [io.path]::GetExtension($masterLogFile) $masterLogFile = "$masterLogDirName\$masterLogFileName--$timestamp$masterLogExt" #Append the date and time to the untouched users log file: $untouchedUsersLogDirName = [io.path]::GetDirectoryName($usersBeforeChangeLogFile) $untouchedUsersLogFileName = [io.path]::GetFileNameWithoutExtension($usersBeforeChangeLogFile) $untouchedUsersLogExt = [io.path]::GetExtension($usersBeforeChangeLogFile) $usersBeforeChangeLogFile = "$untouchedUsersLogDirName\$untouchedUsersLogFileName--$timestamp$untouchedUsersLogExt" #Append the date and time to the modified users log file: $modifiedUsersLogDirName = [io.path]::GetDirectoryName($usersAfterChangeLogFile) $modifiedUsersLogFileName = [io.path]::GetFileNameWithoutExtension($usersAfterChangeLogFile) $modifiedUsersLogExt = [io.path]::GetExtension($usersAfterChangeLogFile) $usersAfterChangeLogFile = "$modifiedUsersLogDirName\$modifiedUsersLogFileName--$timestamp$modifiedUsersLogExt" ################################################################################################# #Read off the selected options and prompt for confirmation to proceed: ################################################################################################# "You chose the following options for this script:" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow if ($addNewProxyAddressesForO365Tenant) { "addNewProxyAddressesForO365Tenant: You chose to add a new proxyAddresses for each user for your O365 domains: $newO365TenantDomain and $secondNewO365TenantDomain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($addO365DomainstoUPNSuffixesforAD) { "addO365DomainstoUPNSuffixesforAD: You chose to add a your O365 tenant domains as domain suffixes in your AD: $newO365TenantDomain and $secondNewO365TenantDomain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($convertProxyAddressestoLowerCase) { "convertProxyAddressestoLowerCase: You chose to convert the proxyAddress for each user " | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($convertMailNicknametoLowerCase) { "convertMailNicknametoLowerCase: You chose to convert the mailNickname attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($convertSamAccountNametoLowerCase) { "convertSamAccountNametoLowerCase: You chose to convert the SamAccountName attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($convertUserPrincipalNametoLowerCase) { "convertUserPrincipalNametoLowerCase: You chose to convert the UserPrincipalName attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($convertMailAttrtoLowerCase) { "convertMailAttrtoLowerCase: You chose to convert the mail attribute for each user to lower case." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($changeUserUPNtoNewDomain) { "changeUserUPNtoNewDomain: You chose to set the UPN suffix for each user to match the new domain you specified: $newUPNsuffix - This also adds the UPN to your AD suffix list." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($changeMailAttributetoUPNwithNewDomain) { "changeMailAttributetoUPNwithNewDomain: You chose to change the mail attribute for each user to [email protected]$newUPNsuffix" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($changeMailAttributeToPrimaryProxyAddr) { "changeMailAttributeToPrimaryProxyAddr: You chose to change the mail attribute for each user to match their primary proxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } if ($changePrimaryProxyAddrToUPNatNewDomain) { "changePrimaryProxyAddrToUPNatNewDomain: You chose to change the primary proxyAddress attribute for each user to SMTP:[email protected]$newUPNsuffix - the old primary proxyAddress will be set as an alias." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } $confirmation = Read-Host "Are you sure you want to proceed with the changes above?: `n[Y]es or [N]o" if ($confirmation -eq 'y') { "You pressed 'y' for Yes - Proceeding with script." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow ################################################################################################# ################################################################################################# #Section 1: Check if new UPN already exitst in domain, and add it to domain if not. ################################################################################################# ################################################################################################# if ($changeUserUPNtoNewDomain) { $domain = Get-ADDomain $UPNsuffix = Get-ADforest $domain.dnsroot | select UPNSuffixes if ($UPNsuffix.UPNSuffixes -eq $newUPNsuffix) { "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: The new UPN you entered: | $newUPNsuffix | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 } else { Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$newUPNsuffix} "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: The new UPN suffix you entered: | $newUPNsuffix | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 "Result: The new UPN suffix | $newUPNsuffix | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 } } else { "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 1: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: You opted not to change the UPN for your users, so this section was skipped." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } ################################################################################################# ################################################################################################# ################################################################################################# #Section 2: Check if O365 domains already exitst in domain, and add them to domain if not. ################################################################################################# ################################################################################################# if ($addO365DomainstoUPNSuffixesforAD) { $domain = Get-ADDomain $UPNsuffix = Get-ADforest $domain.dnsroot | select UPNSuffixes if ($UPNsuffix.UPNSuffixes -eq $newO365TenantDomain) { "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 2: Add Primary O365 tenant domain suffix to domain---------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } else { "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 2: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$newO365TenantDomain} "Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 } if ($UPNsuffix.UPNSuffixes -eq $secondNewO365TenantDomain) { "Result: The O365 primary tenant domain you entered: | $newO365TenantDomain | already exists as a valid UPN suffix in your domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 } else { "Result: The O365 secondary tenant domain you entered: | $secondNewO365TenantDomain | does not exist in the domain: $domain - We are adding it now." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 Set-ADForest $domain.dnsroot -UPNSuffixes @{Add=$secondNewO365TenantDomain} "Result: The O365 secondary tenant domain you entered: | $secondNewO365TenantDomain | is now an available suffix in the domain: $domain" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 2 } } else { "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 2: Add new UPN suffix to domain----------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Result: You opted not to add the O365 tenant domains as UPN suffix options for your AD, so this section was skipped." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } ################################################################################################# #Let's save a header to the "Before Change" list of user properties "`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile "`---List of users and settings prior to modification on $timestamp-------------------------" >> $usersBeforeChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersBeforeChangeLogFile $initialUser >> $usersBeforeChangeLogFile ################################################################################################# ################################################################################################# #Section 3: Modify the users. ################################################################################################# ################################################################################################# "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`---Section 3: Modify the users----------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`----------------------------------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Foreach ($initialUser in $usersInitial) { "Name: $($initialUser.Name)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow ################################################################################################# #SECTION - USER PRINCIPAL NAME ################################################################################################# if ($changeUserUPNtoNewDomain) { if ($convertUserPrincipalNametoLowerCase) { #Set a new variable that has their UPN with the new UPN suffix converted to lower case: $newUpn = $initialUser.UserPrincipalName.Replace($initialUser.UserPrincipalName.Split("@")[1],$newUPNsuffix).ToLower() "Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn } else { #Set a new variable that has their UPN with the new UPN suffix: $newUpn = $initialUser.UserPrincipalName.Replace($initialUser.UserPrincipalName.Split("@")[1],$newUPNsuffix) "Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn } } else { if ($convertUserPrincipalNametoLowerCase) { #Set a new variable that has their UPN with the new UPN suffix converted to lower case: $newUpn = $initialUser.UserPrincipalName.ToLower() "Current UPN: $($initialUser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New UPN: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -UserPrincipalName $newUpn } else { #Set a new variable that has their UPN with the new UPN suffix: $newUpn = $initialUser.UserPrincipalName "Unmodified UserPrincipalName for user: $($newUpn)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } ################################################################################################# #SECTION - SAM ACCOUNT NAME ################################################################################################# if ($convertSamAccountNametoLowerCase) { #Set a new variable that has their existing SAN converted to lower case: $newSAN = $initialUser.SamAccountName.ToLower() "Current SamAccountName: $($initialUser.SamAccountName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New SamAccountName: $($newSAN)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{SamAccountName = $newSAN} } ################################################################################################# #SECTION - PROXY ADDRESSES ################################################################################################# #Get the user proxy addresses into a variable: $oldProxyAddresses = (Get-ADUser $initialUser.SamAccountName -Properties proxyaddresses).proxyaddresses #Check if we're changing the primary proxyAddress or not: if ($changePrimaryProxyAddrToUPNatNewDomain) { #If we are, set the new primary proxyAddress equal to the newUPN with SMTP in front: $newPrimaryProxyAddress = "SMTP:"+$newUpn #Set a new variable with lower-case SMTP that we can use to check if the new desired primary email exists already as an alias: $newSecondaryProxyAddress = "smtp:"+$newUpn $existingPrimaryProxyAddress = $newUpn #Check if the desired new primary proxyAddress is already set as the primary proxyAddress: if ($oldProxyAddresses -ccontains $newPrimaryProxyAddress) { #If so, let's log that: "User primary proxyAddress is already set to $($newPrimaryProxyAddress) - no change necessary." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 #Next let's see if we're supposed to convert it to lower case: if ($convertProxyAddressestoLowerCase) { #If we are, let's parse each proxyAddress, splitting our existing proxyAddress by colon: $newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newPrimaryProxyAddress }) $newProxyAddresses = foreach($newPaddress in $newProxyAddresses) { $newPaddress.ToLower() } $newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower()) #$newPrimaryProxyAddress #Let's read out the old and new proxyAddresses to the console and log: "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress} } #Since the desired new primary proxyAddress is already primary and we aren't supposed to convert them to lower-case, nothing needs to be done: else { "Unmodified proxyAddresses for user: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } #We know the new desired UPN isn't the primary proxyAddress, let's check if it's set as an alias already: elseif ($oldProxyAddresses -ccontains $newSecondaryProxyAddress) { #If so, let's log that: "The desired new Primary proxyAddress for the user already exists as a secondary alias. We'll set it as primary." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 #Next let's see if we're supposed to convert the proxyAddresses to lower-case: if ($convertProxyAddressestoLowerCase) { #If we are, let's parse each proxyAddress, splitting our existing proxyAddress by colon: $newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newSecondaryProxyAddress }) $newProxyAddresses = foreach($newPaddress in $newProxyAddresses) { $newPaddress.ToLower() } $newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower()) #Let's read out the old and new proxyAddresses to the console and log: "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress} } #If we aren't supposed to convert the proxyAddresses to lower-case, we still need to split our desired new primary proxyAddress out from the existing aliases: else { $newProxyAddresses = @($oldProxyAddresses | Where-object { $_ -cne $newSecondaryProxyAddress }) #Let's read out the old and new proxyAddresses to the console and log: "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress} } } #Lastly, if the new desired UPN does not exist as either the primary nor any of the secondary proxyAddresses, let's proceed without it and add it in: else { #Let's see if we're supposed to make the proxyAddresses lower-case or not: if ($convertProxyAddressestoLowerCase) { #If so, we can just convert all proxyAddresses to lower-case since we'll be adding a new primary: $newProxyAddresses = foreach($oldAddress in $oldProxyAddresses) { $oldAddress.tolower() } $newPrimaryProxyAddress = $newPrimaryProxyAddress.Replace($newPrimaryProxyAddress.Split(":")[1],$newPrimaryProxyAddress.Split(":")[1].ToLower()) #Let's read out the old and new proxyAddresses to the console and log: "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress} } #Since we aren't supposed to convert the proxyAddresses to lower-case, we still need to parse them so we can convert the SMTP: for the existing primary proxyAddress to lower-case to make it an alias: else { Write-Host "Proxy Road 13" $newProxyAddresses = $oldProxyAddresses -Replace 'SMTP:','smtp:' #Let's read out the old and new proxyAddresses to the console and log: "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: $newPrimaryProxyAddress" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses= $newPrimaryProxyAddress} } } } else { if ($convertProxyAddressestoLowerCase) { #For each proxy address, make it lower case: $newProxyAddresses = foreach($oldAddress in $oldProxyAddresses) { #Let's make an exemption for the primary SMTP: address (the one that has SMTP: in caps) so we don't accidently remove their primary by converting it to lower case: if ($oldAddress.StartsWith("SMTP:")) { #Convert the second part of their primary address to lower case: $newPrimaryProxyAddress = $oldAddress.Replace($oldAddress.Split(":")[1],$oldAddress.Split(":")[1].ToLower()) #Convert the second part of their primary address to lower case: $existingPrimaryProxyAddress = $oldAddress.Split(":")[1] #Set the new lower-case second part of their primary SMTP address to a variable: $primaryAlias = $existingPrimaryProxyAddress.Split("@")[0] } else { #Convert the non-primary SMTP addresses to lower case: $oldAddress.tolower() } } "Old Proxy Addresses: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New Proxy Addresses: $($newProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "With a new primary proxy address of: SMTP:$($existingPrimaryProxyAddress)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{proxyaddresses = $newProxyAddresses} Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="SMTP:"+$existingPrimaryProxyAddress} } else { $temp1 = foreach($oldAddress in $oldProxyAddresses) { if ($oldAddress.StartsWith("SMTP:")) { $existingPrimaryProxyAddress = $oldAddress.Split(":")[1] $primaryAlias = $existingPrimaryProxyAddress.Split("@")[0] } } "Unmodified proxyAddresses for user: $($initialUser.ProxyAddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } ################################################################################################# #SECTION - MAIL ATTRIBUTE ################################################################################################# #Check to see if the mail attribute is set for this user and if not, set it to primary SMTP address: if (!$initialUser.mail) { #First check if we're supposed to convert the mail attribute to lower-case: if ($convertMailAttrtoLowerCase) { #If so, check if we are supposed to set the mail attribute to their primary proxyAddress: if ($changeMailAttributeToPrimaryProxyAddr) { $newmailAttr = $existingPrimaryProxyAddress.ToLower() "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If not, check if we are supposed to set the mail attribute to their their new UPN: elseif ($changeMailAttributetoUPNwithNewDomain) { $newmailAttr = $newUpn.ToLower() "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If neither of those are true, set their missing mail attribute to their existing UPN: else { $newmailAttr = $initialUser.UserPrincipalName.ToLower() "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } } else { #If we aren't supposed to convert the case, check if we are supposed to set the mail attribute to their primary proxyAddress: if ($changeMailAttributeToPrimaryProxyAddr) { $newmailAttr = $existingPrimaryProxyAddress "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If not, check if we are supposed to set the mail attribute to their their new UPN: elseif ($changeMailAttributetoUPNwithNewDomain) { $newmailAttr = $newUpn "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If neither of those are true, set their missing mail attribute to their existing UPN: else { $newmailAttr = $initialUser.UserPrincipalName "User did not have the mail attribute set. We set it to: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } } } #Assuming the mail attribute isn't missing: else { #First check if we're supposed to convert the mail attribute to lower-case: if ($convertMailAttrtoLowerCase) { #If so, check if we are supposed to set the mail attribute to their primary proxyAddress: if ($changeMailAttributeToPrimaryProxyAddr) { $newmailAttr = $existingPrimaryProxyAddress.ToLower() "Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If not, check if we are supposed to set the mail attribute to their their new UPN: elseif ($changeMailAttributetoUPNwithNewDomain) { $newmailAttr = $newUpn.ToLower() "Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If neither of those are true we'll leave the mail attribute alone: else { "Unmodified mail attribute for user: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } else { #If we aren't supposed to convert the case, check if we are supposed to set the mail attribute to their primary proxyAddress: if ($changeMailAttributeToPrimaryProxyAddr) { $newmailAttr = $existingPrimaryProxyAddress "Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If not, check if we are supposed to set the mail attribute to their their new UPN: elseif ($changeMailAttributetoUPNwithNewDomain) { $newmailAttr = $newUpn "Old mail attribute: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New mail attribute: $($newmailAttr)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mail = $newmailAttr} } #If neither of those are true we'll leave the mail attribute alone: else { "Unmodified mail attribute for user: $($initialUser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } } ################################################################################################# #SECTION - ADD NEW PRIMARY PROXY ADDRESS IF SWITCH IS SET ################################################################################################# if ($addNewProxyAddressesForO365Tenant) { "Adding a new proxyaddress of: smtp:$($initialUser.SamAccountName)@$($newO365TenantDomain)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="smtp:"+$initialUser.SamAccountName+"@"+$newO365TenantDomain} "Adding a second new proxyaddress of: smtp:$($initialUser.SamAccountName)@$($secondNewO365TenantDomain)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Add @{proxyAddresses="smtp:"+$initialUser.SamAccountName+"@"+$secondNewO365TenantDomain} } ################################################################################################# #SECTION - MAIL NICKNAME ################################################################################################# #Check if they have an existing mailNickname, if not move on: if ($initialUser.mailNickname) { #Check switches to see if we are supposed to convert mailNickname to lower case: if ($convertMailNicknametoLowerCase) { #Convert the mailNickname to lower case. $newmailNick = $initialUser.mailNickname.ToLower() "Current mailNickname: $($initialUser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "New mailNickname: $($newmailNick)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 Set-ADUser $initialUser.SamAccountName -Replace @{mailNickname = $newmailNick} } else { "Unmodified mailNickname attribute for user: $($initialUser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } } else { "The mailNickname attribute was not set for this user." | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 } "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 1 "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } ################################################################################################# ################################################################################################# #END OF USER MODIFICATIONS ################################################################################################# ################################################################################################# #Let's save a header to the "After Changes" list of user properties "`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile "`---List of users and settings after modification on $timestamp----------------------------" >> $usersAfterChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile "`----------------------------------------------------------------------------------------------------" >> $usersAfterChangeLogFile $usersChanged = $usersInitial $usersChanged >> $usersAfterChangeLogFile Foreach ($newuser in $usersChanged) { "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Final Output:" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`nName: $($newuser.Name)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "SamAccountName: $($newuser.SamAccountName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "EmailAddress: $($newuser.mail)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "mailNickname: $($newuser.mailNickname)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "UserPrincipalName: $($newuser.UserPrincipalName)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "ProxyAddresses: $((get-aduser $newuser.SamAccountName -Properties proxyaddresses).proxyaddresses)" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`-------------------------------------------------------------------------" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } "#################################################################################################" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "Execution of this script is now complete. Thank you for using this powerscript!" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "`n" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow "#################################################################################################" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow Start-Sleep -Seconds 3 } else { "You pressed a key other than Y - Exiting" | Tee-Object -FilePath $masterLogFile -Append | Write-Host -ForegroundColor Yellow } |
Comments