gangstanthony

code_review_rename_computer_and_join_ad_on_first

Apr 14th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/4es9ll/code_review_rename_computer_and_join_ad_on_first/
  2.  
  3. # Get Campus Code, $CampusCode
  4. $CampusCode = Read-Host 'Input campus code: (01)'
  5. if ($CampusCode -eq $null -or $CampusCode -eq '') { $CampusCode = '01' }
  6.  
  7. # Get Building Number, $BuildingNumber
  8. $BuildingNumber = Read-Host 'Input building number: (01)'
  9. if ($BuildingNumber -eq $null -or $BuildingNumber -eq '') { $BuildingNumber = '01' }
  10.  
  11. # Get Computer Code, $ComputerCode
  12. $ComputerCode = Read-Host 'Input computer code: (01)'
  13. if ($ComputerCode -eq $null -or $ComputerCode -eq '') { $ComputerCode = '01' }
  14.  
  15. # Get Last 6 characters of Mac Address, $MacLast6
  16. $MacLast6 = ''
  17. try {
  18.     $MacLast6 = ((Get-NetAdapter -Name 'Local Area Connection').MacAddress -replace '-').Substring(6)
  19. } catch {
  20.     $Confirmation = ''
  21.     while ($Confirmation.ToUpper() -ne 'Y') {
  22.         $MacLast6 = Read-Host 'Error detecting Mac Address, input last 6 characters manually:'
  23.         $Confirmation = Read-Host "Is this the correct last 6 characters of the Mac Address? '$MacLast6' [Y]/n"
  24.         if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
  25.     }
  26. }
  27.  
  28. # Generate Computer Name, $NewName
  29. $NewName = 'newnamegeneratedhere'
  30.    
  31. $Confirmation = ''
  32. while ($Confirmation.ToUpper() -ne 'Y') {
  33.     # Prompt for confirmation, $Confirmation
  34.     $Confirmation = Read-Host "Set computer name to '$NewName'? [Y]/n"
  35.     if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
  36. }
  37.  
  38. try {
  39.     #code removed for safety
  40. } catch {
  41.     #code removed for safety
  42. }
  43.  
  44. # Setting 'rootuserhere' password
  45. Write-Host "Setting root administrator password to '$NewPassword'."
  46.  
  47. try {
  48.     ([adsi]'WinNT://./Administrator').SetPassword($NewPassword)
  49.     $admin = [adsi]'WinNT://./Administrator,user'
  50.     $admin.psbase.Rename('rootuserhere')
  51. } catch {
  52.     Write-Error 'Error setting administrator username and/or password!'
  53. }
  54.  
  55. # Ask for join or just restart
  56. $Confirmation = ''
  57. $Confirmation = Read-Host 'Would you like join the domain after renaming the computer? [Y]/n'
  58. if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
  59.  
  60. if ($Confirmation.ToUpper() -eq 'Y') {
  61.     Add-Computer -Domain ad.contoso.com -NewName $NewName -Credential $(Get-Credential -Message 'Input credentials for Active Directory (ad.contoso.com).') -Restart
  62. } else {
  63.     Rename-Computer -NewName $NewName
  64.     Restart-Computer -Force
  65. }
Add Comment
Please, Sign In to add comment