From 8412ed6065189289cb0054e1782b4c1bfee4c489 Mon Sep 17 00:00:00 2001 From: Irving Date: Sun, 22 Aug 2021 16:36:19 -0400 Subject: [PATCH] Change Win_Rename_Computer.ps1 community script to add domain joined computer rename functionality and refactor per standards. --- scripts/Win_Rename_Computer.ps1 | 92 ++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/scripts/Win_Rename_Computer.ps1 b/scripts/Win_Rename_Computer.ps1 index 98b8b7eb..8e97d2e9 100644 --- a/scripts/Win_Rename_Computer.ps1 +++ b/scripts/Win_Rename_Computer.ps1 @@ -1,19 +1,83 @@ -# Chanage the computer name in Windows -# v1.0 -# First Command Parameter will be new computer name -# Second Command Parameter if yes will auto-restart computer +<# +.SYNOPSIS + Rename computer. -if ($Args.Count -eq 0) { - Write-Output "Computer name arg is required" - exit 1 +.DESCRIPTION + Rename domain and non domain joined computers. + +.PARAMETER NewName + Specifies the new computer name. + +.PARAMETER Username + Specifies the username with permission to rename a domain computer. + Required for domain joined computers. + Do not add the domain part like "Domain01\" to the username as that is already extracted and appended to the Rename-Computer cmdlet. + +.PARAMETER Password + Specifies the password for the username. + Required for domain joined computers. + +.PARAMETER Restart + Switch to force the computer to restart after a successful rename. + +.OUTPUTS + Results are printed to the console. + +.EXAMPLE + PS C:\> .\Win_Rename_Computer.ps1 -Username myuser -Password mypassword -NewName mynewname -Restart + +.NOTES + Change Log + V1.0 Initial release + V2.0 Added domain join +#> + +param( + [string] $Username, + [string] $Password, + [switch] $Restart, + [string] $NewName +) + +if (!$NewName){ + Write-Host "-NewName parameter required." + Exit 1 } -$param1=$args[0] -$ToRestartTypeYes=$args[1] +if ((Get-WmiObject win32_computersystem).partofdomain -eq $false) { + # Rename Non Domain Joined Computer -Rename-Computer -newname "$param1" + if ($Restart) { + Rename-computer -NewName $NewName -Force -Restart + } else { + Rename-computer -NewName $NewName -Force + } + Write-Host "Attempted rename of computer to $NewName." + +} +else { + # Rename Domain Joined Computer -# Restart the computer for rename to take effect -if ($ToRestartTypeYes -eq 'yes') { - Restart-Computer -Force -} + if (!$Username){ + Write-Host "-Username parameter required on domain joined computers." + Exit 1 + } + + if (!$Password){ + Write-Host "-Password parameter required on domain joined computers." + Exit 1 + } + + $securePassword = ConvertTo-SecureString -string $Password -asPlainText -Force + + $domainUsername = (Get-WmiObject Win32_ComputerSystem).Domain + "\$Username" + + $credential = New-Object System.Management.Automation.PSCredential($domainUsername, $securePassword) + + if ($Restart) { + Rename-computer -NewName $NewName -DomainCredential $credential -Force -Restart + } else { + Rename-computer -NewName $NewName -DomainCredential $credential -Force + } + Write-Host "Attempted rename of domain computer to $NewName." +} \ No newline at end of file