Skip to main content

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

Custom Fields Rename-Machine

Rename-Machine

User Parameters

NameExampleRequiredDefaultTypeDescription
NewNameCOMPUTER2TrueText StringThe new name to be set for the Windows System
RebootTrue or FalseFalseFlagIf this is checked, the system will be rebooted after the renaming of the system

User Parameters

Task Creation

Create a new Script Editor style script in the system to implement this task.

Add Task Add Task Continue

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

Task Creation

Parameters

Title:

Add a new parameter by clicking the Add Parameter button located at the top-right corner of the screen.

Add Parameter

This screen will appear.

Add New Parameter

  • Set NewName in the Parameter Name field.
  • Enable the Required Field option.
  • Select Text String from the Parameter Type dropdown menu.
  • Click the Save button.

NewName Parameter

Add another new parameter by clicking the Add Parameter button located at the top-right corner of the screen.

Add Parameter

This screen will appear.

Add New Parameter

  • Set Reboot in the Parameter Name field.
  • Select Flag from the Parameter Type dropdown menu.
  • Select the Default Value to 'False'
  • Click the Save button.

Reboot Parameter

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.

Add Row

A blank function will appear.

Blank Function

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"

Set Pre-defined Variable

Variable Name: UserName

Select Custom Field

Select Domain_Admin_User

Click Save

Username

Row 2 Function: Set Pre-defined Variable

Add a new row by clicking the Add Row button.

alt text

Select function "Set Pre-Defined Variable"

Set Pre-defined Variable

Variable Name: Password

Select Custom Field

Select Domain_Admin_Pws

Click Save

Password

Row 3 Function: Set Pre-defined Variable

Add a new row by clicking the Add Row button.

alt text

Select function "Set Pre-Defined Variable"

Set Pre-defined Variable

Variable Name: CurrentName

Select System Variable

Select friendlyName

Click Save

CurrentName

Row 4 Function: PowerShell Script

Add a new row by clicking the Add Row button.

Add Row

Search and select the PowerShell Script function.

PowerShell Function

The following function will pop up on the screen:

PowerShell Popup

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

PowerShell Script 1

Row 5: Logic: If/Then

Add a new logic and select If/Then.

If/Then

If/Then Popup

Row 5a: Condition: Output Contains

In the IF part, enter An error occurred in the right box of the "Output Contains" part.

Condition

Row 5b: Function: Script Exit

Add a new row in the IF section and select Script Exit.

Add row

Script Exit

In the script exit message, simply type The Rename-Machine.ps1 download failed. Refer to the logs: %output% and click Save.

Script Exit Message

Row 6 Function: Command Prompt (CMD) Script

Add a new row by clicking the Add Row button.

Add Row

A blank function will appear.

Blank Function

Search and select the Command Prompt function.

Command Prompt (CMD) Script

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@'"

Command Prompt Code

Row 7 Function: Script log

Add a new row by clicking the Add Row button.

Add Row

A blank function will appear.

Blank Function

Search and select Script Log

Script Log Selection

The following function will pop up on the screen:

Script Log Popup

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.

Script log Message 1

Row 8 Function: PowerShell Script

Add a new row by clicking the Add Row button.

Add Row

Search and select the PowerShell Script function.

PowerShell Function

The following function will pop up on the screen:

PowerShell Popup

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.

PowerShell 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
}

PowerShell Script 2

Row 9 Logic: If/Then

Add a new logic and select If/Then.

If/Then

If/Then Popup

Row 9a: Condition: Output Contains

In the IF part, enter An error occurred in the right box of the "Output Contains" part.

Condition

Row 9b: Function: Script Exit

Add a new row in the IF section and select Script Exit.

Add row

Script Exit

In the script exit message, simply type The error was detected in renaming the machine. Refer to the logs: %Output%.

Script Exit Message 2

Row 10: Function: Script Log

Add a new row by clicking the Add Row button.

Add Row

A blank function will appear.

Blank Function

Search and select Script Log

Script Log Selection

The following function will pop up on the screen:

Script Log Popup

In the script log message, simply type %output% and click the Save button.

Script log Message 2

Click Save at the top right corner of the script

Save Script

Completed Task

Complete Task Complete Task 1

Output

  • Script log