Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://www.reddit.com/r/PowerShell/comments/4es9ll/code_review_rename_computer_and_join_ad_on_first/
- # Get Campus Code, $CampusCode
- $CampusCode = Read-Host 'Input campus code: (01)'
- if ($CampusCode -eq $null -or $CampusCode -eq '') { $CampusCode = '01' }
- # Get Building Number, $BuildingNumber
- $BuildingNumber = Read-Host 'Input building number: (01)'
- if ($BuildingNumber -eq $null -or $BuildingNumber -eq '') { $BuildingNumber = '01' }
- # Get Computer Code, $ComputerCode
- $ComputerCode = Read-Host 'Input computer code: (01)'
- if ($ComputerCode -eq $null -or $ComputerCode -eq '') { $ComputerCode = '01' }
- # Get Last 6 characters of Mac Address, $MacLast6
- $MacLast6 = ''
- try {
- $MacLast6 = ((Get-NetAdapter -Name 'Local Area Connection').MacAddress -replace '-').Substring(6)
- } catch {
- $Confirmation = ''
- while ($Confirmation.ToUpper() -ne 'Y') {
- $MacLast6 = Read-Host 'Error detecting Mac Address, input last 6 characters manually:'
- $Confirmation = Read-Host "Is this the correct last 6 characters of the Mac Address? '$MacLast6' [Y]/n"
- if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
- }
- }
- # Generate Computer Name, $NewName
- $NewName = 'newnamegeneratedhere'
- $Confirmation = ''
- while ($Confirmation.ToUpper() -ne 'Y') {
- # Prompt for confirmation, $Confirmation
- $Confirmation = Read-Host "Set computer name to '$NewName'? [Y]/n"
- if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
- }
- try {
- #code removed for safety
- } catch {
- #code removed for safety
- }
- # Setting 'rootuserhere' password
- Write-Host "Setting root administrator password to '$NewPassword'."
- try {
- ([adsi]'WinNT://./Administrator').SetPassword($NewPassword)
- $admin = [adsi]'WinNT://./Administrator,user'
- $admin.psbase.Rename('rootuserhere')
- } catch {
- Write-Error 'Error setting administrator username and/or password!'
- }
- # Ask for join or just restart
- $Confirmation = ''
- $Confirmation = Read-Host 'Would you like join the domain after renaming the computer? [Y]/n'
- if ($Confirmation -eq $null -or $Confirmation -eq '') { $Confirmation = 'Y' }
- if ($Confirmation.ToUpper() -eq 'Y') {
- Add-Computer -Domain ad.contoso.com -NewName $NewName -Credential $(Get-Credential -Message 'Input credentials for Active Directory (ad.contoso.com).') -Restart
- } else {
- Rename-Computer -NewName $NewName
- Restart-Computer -Force
- }
Add Comment
Please, Sign In to add comment