Nanocloud/nanocloud

Change Windows hostname without rebooting

Closed this issue · 1 comments

=== Version ===

  • v2.1.1

=== New Enhancement ===

  • When booting a custom AMI on AWS with Nanocloud, it would be good to change the hostname.
  • It can be done with the following script :

_/Set allowed ASCII character codes to Uppercase letters (65..90),
$charcodes = 65..90

// Convert allowed character codes to characters
$allowedChars = $charcodes | ForEach-Object { [char][byte]$_ }

$LengthOfName = 10
// Generate computer name
$pw = ($allowedChars | Get-Random -Count $LengthOfName) -join ""
$prefix = "CDW12"
$name = $prefix+$pw

$ComputerName = $name

Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname"
Remove-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname"

New-PSDrive -name HKU -PSProvider "Registry" -Root "HKEY_USERS"

Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\Computername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\Computername\ActiveComputername" -name "Computername" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "Hostname" -value $ComputerName
Set-ItemProperty -path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" -name "NV Hostname" -value $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "AltDefaultDomainName" -value $ComputerName
Set-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -name "DefaultDomainName" -value $ComputerName
#Set-ItemProperty -path "HKU:.Default\Software\Microsoft\Windows Media\WMSDK\General" -name "Computername" -value $ComputerName_

Add a new entry in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command " & 'C:\Users\Administrator\Desktop\test2.ps1' "

This is the command line I used to call the script