fixed hostname function

This commit is contained in:
2025-06-12 13:04:09 +02:00
parent 11ff856a8c
commit 0444d658be

View File

@@ -550,20 +550,24 @@ if (Is-McAfeeInstalled) {
#endregion McAfee Removal #endregion McAfee Removal
# Set hostname # Current computer name
$currentName = $env:COMPUTERNAME $currentName = $env:COMPUTERNAME
$hostname = Read-Host "Enter new hostname (leave blank to keep current name [$currentName])"
# Prompt for new hostname
Write-Host "Current hostname is '$currentName'."
$hostname = Read-Host "Enter new hostname (leave blank to keep current name)"
# Check if the input is non-empty and different
if (-not [string]::IsNullOrWhiteSpace($hostname) -and $hostname -ne $currentName) { if (-not [string]::IsNullOrWhiteSpace($hostname) -and $hostname -ne $currentName) {
try { try {
Rename-Computer -NewName $hostname -Force -ErrorAction Stop Rename-Computer -NewName $hostname -Force -ErrorAction Stop
Write-Host "Hostname changed to $hostname. It will take effect after reboot." Write-Host "Hostname changed to '$hostname'. It will take effect after reboot."
$global:hostnameChanged = $true $global:hostnameChanged = $true
} catch { } catch {
Write-Warning "Failed to rename computer: $($_.Exception.Message)" Write-Warning "Failed to rename computer: $($_.Exception.Message)"
} }
} else { } else {
Write-Host "No valid hostname provided or hostname is already set to '$currentName'. Skipping rename." Write-Host " No hostname change requested or hostname already set to '$currentName'. Skipping rename."
} }
# Prompt for the Active Directory domain # Prompt for the Active Directory domain