Update samy.ps1
This commit is contained in:
88
samy.ps1
88
samy.ps1
@@ -769,6 +769,22 @@ if (-not [System.Diagnostics.EventLog]::SourceExists('$EventSource')) {
|
||||
|
||||
#endregion Write-Log
|
||||
|
||||
#region Computer rename helpers
|
||||
|
||||
function Test-ComputerName {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Name
|
||||
)
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($Name)) { return $false }
|
||||
if ($Name.Length -gt 15) { return $false }
|
||||
if ($Name -notmatch '^[A-Za-z0-9-]+$') { return $false }
|
||||
return $true
|
||||
}
|
||||
|
||||
#endregion Computer rename helpers
|
||||
|
||||
#region building the Menus
|
||||
|
||||
@@ -1339,6 +1355,72 @@ function Send-JSON {
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-RenameComputer {
|
||||
param($Context)
|
||||
|
||||
try {
|
||||
if ($Context.Request.HttpMethod -ne 'POST') {
|
||||
$Context.Response.StatusCode = 405
|
||||
Send-Text $Context 'Use POST'
|
||||
return
|
||||
}
|
||||
|
||||
# Read raw JSON body
|
||||
$rawBody = (New-Object IO.StreamReader $Context.Request.InputStream).ReadToEnd()
|
||||
if (-not $rawBody) {
|
||||
$Context.Response.StatusCode = 400
|
||||
Send-Text $Context 'Missing request body.'
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
$body = $rawBody | ConvertFrom-Json
|
||||
} catch {
|
||||
$Context.Response.StatusCode = 400
|
||||
Send-Text $Context 'Invalid JSON body.'
|
||||
return
|
||||
}
|
||||
|
||||
$newName = $body.newName
|
||||
|
||||
if (-not (Test-ComputerName -Name $newName)) {
|
||||
Write-LogHybrid "RenameComputer: invalid computer name '$newName'." Error OnBoard -LogToEvent
|
||||
$Context.Response.StatusCode = 400
|
||||
Send-JSON $Context @{
|
||||
Success = $false
|
||||
Error = "Invalid computer name. Must be 1-15 characters and use only letters, numbers, and hyphens."
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Write-LogHybrid "RenameComputer: renaming computer to '$newName'." Info OnBoard -LogToEvent
|
||||
|
||||
try {
|
||||
Rename-Computer -NewName $newName -Force -ErrorAction Stop
|
||||
} catch {
|
||||
Write-LogHybrid "RenameComputer: rename failed: $($_.Exception.Message)" Error OnBoard -LogToEvent
|
||||
$Context.Response.StatusCode = 500
|
||||
Send-JSON $Context @{
|
||||
Success = $false
|
||||
Error = $_.Exception.Message
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Write-LogHybrid "RenameComputer: rename complete, reboot required for new name to apply." Success OnBoard -LogToEvent
|
||||
|
||||
Send-JSON $Context @{
|
||||
Success = $true
|
||||
NewName = $newName
|
||||
Note = "Rename successful. A reboot is required for the new name to take effect."
|
||||
}
|
||||
} catch {
|
||||
Write-LogHybrid "Invoke-RenameComputer fatal error: $($_.Exception.Message)" Error OnBoard -LogToEvent
|
||||
$Context.Response.StatusCode = 500
|
||||
Send-Text $Context "Internal error during computer rename."
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion Onboarding handlers
|
||||
|
||||
@@ -1759,6 +1841,12 @@ function Install-DattoRMM {
|
||||
return
|
||||
}
|
||||
|
||||
# ---- Rename Computer endpoint ----
|
||||
if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'renameComputer') {
|
||||
Invoke-RenameComputer $Context
|
||||
return
|
||||
}
|
||||
|
||||
# ---- Serve UI pages ----
|
||||
if ($path -in @('', 'onboard', 'offboard', 'tweaks', 'SVSApps')) {
|
||||
$page = if ($path -eq '') { 'onboard' } else { $path }
|
||||
|
||||
Reference in New Issue
Block a user