fixed hostname and reboot functions
This commit is contained in:
30
base.ps1
30
base.ps1
@@ -551,13 +551,19 @@ if (Is-McAfeeInstalled) {
|
|||||||
#endregion McAfee Removal
|
#endregion McAfee Removal
|
||||||
|
|
||||||
# Set hostname
|
# 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)) {
|
if (-not [string]::IsNullOrWhiteSpace($hostname) -and $hostname -ne $currentName) {
|
||||||
Rename-Computer -NewName $hostname -Force -PassThru
|
try {
|
||||||
Write-Host "Hostname changed to $hostname. The system will now restart"
|
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 {
|
} 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
|
# Prompt for the Active Directory domain
|
||||||
@@ -642,14 +648,18 @@ Write-Host "All installations and updates completed. Restarting the system..."
|
|||||||
# Restart computer
|
# Restart computer
|
||||||
|
|
||||||
function Confirm-And-Reboot {
|
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
|
$response = Read-InputWithTimeout -Prompt "Do you want to reboot the computer now? (Y/N):" -TimeoutSeconds 600
|
||||||
if ($response -match '^(Y|y)$') {
|
if ($response -match '^(Y|y)$') {
|
||||||
Write-Host "Rebooting now..."
|
Write-Host "Rebooting now..."
|
||||||
Restart-Computer
|
Restart-Computer
|
||||||
}
|
} else {
|
||||||
else {
|
Write-Host "Reboot skipped. Please remember to reboot manually for changes to take effect."
|
||||||
Write-Host "Reboot cancelled. Please remember to reboot later if required."
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Write-Host "No reboot required."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Confirm-And-Reboot
|
Confirm-And-Reboot
|
||||||
|
|||||||
Reference in New Issue
Block a user