fixed hostname and reboot functions

This commit is contained in:
2025-06-12 12:14:14 +02:00
parent 3a50b6be8d
commit 11ff856a8c

View File

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