From 11ff856a8c09ea8a4ffa9acbe6b949d18fdbac9c Mon Sep 17 00:00:00 2001 From: dals Date: Thu, 12 Jun 2025 12:14:14 +0200 Subject: [PATCH] fixed hostname and reboot functions --- base.ps1 | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/base.ps1 b/base.ps1 index 7a82c9d..82d17cf 100644 --- a/base.ps1 +++ b/base.ps1 @@ -551,13 +551,19 @@ if (Is-McAfeeInstalled) { #endregion McAfee Removal # Set hostname -$hostname = Read-Host "Enter new hostname (leave blank to keep current name)" +$currentName = $env:COMPUTERNAME +$hostname = Read-Host "Enter new hostname (leave blank to keep current name [$currentName])" -if (-not [string]::IsNullOrEmpty($hostname)) { - Rename-Computer -NewName $hostname -Force -PassThru - Write-Host "Hostname changed to $hostname. The system will now restart" +if (-not [string]::IsNullOrWhiteSpace($hostname) -and $hostname -ne $currentName) { + try { + Rename-Computer -NewName $hostname -Force -ErrorAction Stop + Write-Host "Hostname changed to $hostname. It will take effect after reboot." + $global:hostnameChanged = $true + } catch { + Write-Warning "Failed to rename computer: $($_.Exception.Message)" + } } else { - Write-Host "No hostname provided. Keeping the current hostname." + Write-Host "No valid hostname provided or hostname is already set to '$currentName'. Skipping rename." } # Prompt for the Active Directory domain @@ -642,14 +648,18 @@ Write-Host "All installations and updates completed. Restarting the system..." # Restart computer function Confirm-And-Reboot { + if ($global:hostnameChanged -or $domainJoined) { + Write-Host "System changes require a reboot." $response = Read-InputWithTimeout -Prompt "Do you want to reboot the computer now? (Y/N):" -TimeoutSeconds 600 if ($response -match '^(Y|y)$') { - Write-Host "Rebooting now..." - Restart-Computer - } - else { - Write-Host "Reboot cancelled. Please remember to reboot later if required." + Write-Host "Rebooting now..." + Restart-Computer + } else { + Write-Host "Reboot skipped. Please remember to reboot manually for changes to take effect." } + } else { + Write-Host "No reboot required." + } } Confirm-And-Reboot