Skip to content

Commit a14f999

Browse files
committed
Feat: Add Invoke-ExecRenameAPDevice function for AP device renaming
1 parent d5e2edc commit a14f999

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ExecRenameAPDevice {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Endpoint.Autopilot.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
$APIName = $Request.Params.CIPPEndpoint
13+
$Headers = $Request.Headers
14+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
15+
$TenantFilter = $Request.Body.tenantFilter
16+
17+
18+
try {
19+
$DeviceId = $Request.Body.deviceId
20+
$SerialNumber = $Request.Body.serialNumber
21+
$DisplayName = $Request.Body.displayName
22+
23+
# Validation
24+
if ($DisplayName.Length -gt 15) {
25+
$ValidationError = 'Display name cannot exceed 15 characters.'
26+
} elseif ($DisplayName -notmatch '^[a-zA-Z0-9-]+$') {
27+
# This regex also implicitly checks for spaces
28+
$ValidationError = 'Display name can only contain letters (a-z, A-Z), numbers (0-9), and hyphens (-).'
29+
} elseif ($DisplayName -match '^\d+$') {
30+
$ValidationError = 'Display name cannot consist solely of numbers.'
31+
}
32+
33+
if ($null -ne $ValidationError) {
34+
$Result = "Validation failed: $ValidationError"
35+
$StatusCode = [HttpStatusCode]::BadRequest
36+
} else {
37+
# Validation passed, proceed with Graph API call
38+
$body = @{
39+
displayName = $DisplayName
40+
} | ConvertTo-Json
41+
42+
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities/$($DeviceId)/UpdateDeviceProperties" -tenantid $TenantFilter -body $body -method POST | Out-Null
43+
$Result = "Successfully renamed device '$($DeviceId)' with serial number '$($SerialNumber)' to '$($DisplayName)'"
44+
Write-LogMessage -Headers $User -API $APINAME -message $Result -Sev Info
45+
$StatusCode = [HttpStatusCode]::OK
46+
}
47+
} catch {
48+
$ErrorMessage = Get-CippException -Exception $_
49+
$Result = "Could not rename device '$($DeviceId)' with serial number '$($SerialNumber)' to '$($DisplayName)'. Error: $($ErrorMessage.NormalizedError)"
50+
Write-LogMessage -Headers $User -API $APINAME -message $Result -Sev Error -LogData $ErrorMessage
51+
$StatusCode = [HttpStatusCode]::BadRequest
52+
}
53+
54+
55+
# Associate values to output bindings by calling 'Push-OutputBinding'.
56+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
57+
StatusCode = $StatusCode
58+
Body = @{ Results = $Result }
59+
})
60+
61+
}

0 commit comments

Comments
 (0)