Windows - System - Rename
Summary
This script can be used to rename a machine and validate the success or failure of the rename. The script will first check if the machine is domain-joined or not. If it is, then it will attempt to create a PSCredential object to pass into the Rename-Computer cmdlet. This will use the Username and Password parameters. It then attempts the rename the computer and stores the success status in $computerChangeInfo. The reboot will be required to complete the renaming process.
Dependencies
User Parameters
Name | Example | Required | Default | Type | Description |
---|---|---|---|---|---|
NewName | COMPUTER2 | True | Text String | The new name to be set for the Windows System | |
Reboot | True or False | False | Flag | If this is checked, the system will be rebooted after the renaming of the system |
Task Creation
Create a new Script Editor
style script in the system to implement this task.
Name: Windows - System - Rename
Description: This script can be used to rename a machine and validate the success or failure of the rename. The script will first check if the machine is domain-joined or not. If it is, then it will attempt to create a PSCredential object to pass into the Rename-Computer cmdlet. This will use the Username and Password parameters. It then attempts the rename the computer and stores the success status in $computerChangeInfo. The reboot will be required to complete the renaming process.
Category: Management
Parameters
Title:
Add a new parameter by clicking the Add Parameter
button located at the top-right corner of the screen.
This screen will appear.
- Set
NewName
in theParameter Name
field. - Enable the
Required Field
option. - Select
Text String
from theParameter Type
dropdown menu. - Click the
Save
button.
Add another new parameter by clicking the Add Parameter
button located at the top-right corner of the screen.
This screen will appear.
- Set
Reboot
in theParameter Name
field. - Select
Flag
from theParameter Type
dropdown menu. - Select the Default Value to 'False'
- Click the
Save
button.
Task
Navigate to the Script Editor section and start by adding a row. You can do this by clicking the Add Row
button at the bottom of the script page.
A blank function will appear.
Row 1 Function: Set Pre-defined Variable
Note: Before setting the Pre-defined Variable please create the custom fields [Rename-Machine custom fields](/docs/ba3c12eb-f166-4982-ab21-a2337a989614)
Select function "Set Pre-Defined Variable"
Variable Name: UserName
Select Custom Field
Select Domain_Admin_User
Click Save
Row 2 Function: Set Pre-defined Variable
Add a new row by clicking the Add Row button.
Select function "Set Pre-Defined Variable"
Variable Name: Password
Select Custom Field
Select Domain_Admin_Pws
Click Save
Row 3 Function: Set Pre-defined Variable
Add a new row by clicking the Add Row button.
Select function "Set Pre-Defined Variable"
Variable Name: CurrentName
Select System Variable
Select friendlyName
Click Save
Row 4 Function: PowerShell Script
Add a new row by clicking the Add Row button.
Search and select the PowerShell Script
function.
The following function will pop up on the screen:
Paste the following PowerShell script and set the Expected time of script execution in seconds
to 600
. Click the Save
button.
[Net.ServicePointManager]::SecurityProtocol = [enum]::ToObject([Net.SecurityProtocolType], 3072)
#region Setup - Variables
$BaseURL = 'https://file.provaltech.com/repo'
$PS1URL = "$BaseURL/script/Rename-Machine.ps1"
$WorkingDirectory = "C:\ProgramData\_automation\script\Rename-Machine"
$PS1Path = "$WorkingDirectory\Rename-Machine.ps1"
$PS1Log = "$WorkingDirectory\Rename-Machine-log.txt"
$PS1ErrorLog = "$WorkingDirectory\Rename-Machine-error.txt"
$Reboot = '@Reboot@'
#endregion
#region Setup - Folder Structure
if ( !(Test-Path $WorkingDirectory) ) {
try {
New-Item -Path $WorkingDirectory -ItemType Directory -Force -ErrorAction Stop | Out-Null
}
catch {
Write-Error -Message "An error occurred: Failed to Create $WorkingDirectory. Reason: $($Error[0].Exception.Message)" -Level Error
}
} if (-not ( ( ( Get-Acl $WorkingDirectory ).Access | Where-Object { $_.IdentityReference -Match 'EveryOne' } ).FileSystemRights -Match 'FullControl' ) ) {
$ACl = Get-Acl $WorkingDirectory
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule('Everyone', 'FullControl', 'ContainerInherit, ObjectInherit', 'none', 'Allow')
$Acl.AddAccessRule($AccessRule)
Set-Acl $WorkingDirectory $Acl
}
$response = Invoke-WebRequest -Uri $PS1URL -UseBasicParsing
if (($response.StatusCode -ne 200) -and (!(Test-Path -Path $PS1Path))) {
Write-Error -Message "An error occurred: No pre-downloaded script exists and the script '$PS1URL' failed to download. Exiting."
return
}
elseif ($response.StatusCode -eq 200) {
Remove-Item -Path $PS1Path -ErrorAction SilentlyContinue
[System.IO.File]::WriteAllLines($PS1Path, $response.Content)
}
if (!(Test-Path -Path $PS1Path)) {
Write-Error -Message 'An error occurred: The script was unable to be downloaded. Exiting.'
return
}
#endregion
Row 5: Logic: If/Then
Add a new logic and select If/Then
.
Row 5a: Condition: Output Contains
In the IF part, enter An error occurred
in the right box of the "Output Contains" part.
Row 5b: Function: Script Exit
Add a new row in the IF section and select Script Exit
.
In the script exit message, simply type The Rename-Machine.ps1 download failed. Refer to the logs: %output%
and click Save
.
Row 6 Function: Command Prompt (CMD) Script
Add a new row by clicking the Add Row button.
A blank function will appear.
Search and select the Command Prompt
function.
Paste the following Command and set the Expected time of script execution in seconds
to 300
. Click the Save
button.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command "& 'C:\ProgramData\_automation\script\Rename-Machine\Rename-Machine.ps1' -NewName '@NewName@' -Username '@Username@' -Password '@Password@'"
Row 7 Function: Script log
Add a new row by clicking the Add Row button.
A blank function will appear.
Search and select Script Log
The following function will pop up on the screen:
In the script log message, simply type The system was renamed from @CurrentName@ to the @NewName@. Refer to the logs: %output%
and click the Save
button.
Row 8 Function: PowerShell Script
Add a new row by clicking the Add Row button.
Search and select the PowerShell Script
function.
The following function will pop up on the screen:
Paste the following PowerShell script and set the Expected time of script execution in seconds
to 600
. Click the Save
button.
Also, Select the Continue on Failure
.
$WorkingDirectory = 'C:\ProgramData\_automation\script\Rename-Machine'
$PS1Log = "$WorkingDirectory\Rename-Machine-log.txt"
$PS1ErrorLog = "$WorkingDirectory\Rename-Machine-error.txt"
$Reboot = '1'
if (Test-Path -Path $PS1ErrorLog) {
$errorcontent = Get-Content -path $PS1ErrorLog
Write-Error -Message "An error occurred: The script encountered an error when running the process. Refer to the log: $errorcontent."
return
}
$timeout = 60
$elapsed = 0
while ($elapsed -lt $timeout -and !(Test-Path -Path $PS1Log)) {
Start-Sleep -Seconds 1
$elapsed++
}
if (Test-Path -Path $PS1Log) {
$content = Get-Content -path $PS1Log
$content[ $($($content.IndexOf($($content -match 'Rename-Machine$')[-1])) + 1)..$($Content.length - 1) ]
Write-Output "The system renamed successfully. Reboot is required to reflect the new name i.e. @NewName@"
}
else {
Write-Error -Message "An error occurred: The log file was not created within the expected time."
}
if ($Reboot -eq 'True' -or $Reboot -eq 1) {
Write-Output 'Rebooting the agent for the system rename'
shutdown -r -t 00
}
Row 9 Logic: If/Then
Add a new logic and select If/Then
.
Row 9a: Condition: Output Contains
In the IF part, enter An error occurred
in the right box of the "Output Contains" part.
Row 9b: Function: Script Exit
Add a new row in the IF section and select Script Exit
.
In the script exit message, simply type The error was detected in renaming the machine. Refer to the logs: %Output%
.
Row 10: Function: Script Log
Add a new row by clicking the Add Row button.
A blank function will appear.
Search and select Script Log
The following function will pop up on the screen:
In the script log message, simply type %output%
and click the Save
button.
Click Save
at the top right corner of the script
Completed Task
Output
- Script log