|
| 1 | +function Sync-IntuneDeviceHealthScript { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Synchronizes one Intune device health script from a declarative definition. |
| 5 | + .DESCRIPTION |
| 6 | + Every definition requires DisplayName, Type, Path, SourceMarker, and |
| 7 | + OwnershipMetadata. Present definitions additionally require |
| 8 | + FingerprintMetadataKey, Fingerprint, Status, and BuildBody. |
| 9 | + BuildBody receives IncludeCreateOnlyProperties and remains workload-specific. |
| 10 | + #> |
| 11 | + [CmdletBinding(SupportsShouldProcess)] |
| 12 | + [OutputType([PSCustomObject[]])] |
| 13 | + param( |
| 14 | + [Parameter(Mandatory)] |
| 15 | + [hashtable]$Definition, |
| 16 | + |
| 17 | + [Parameter()] |
| 18 | + [ValidateSet('Present', 'Remove')] |
| 19 | + [string]$DesiredState = 'Present', |
| 20 | + |
| 21 | + [Parameter()] |
| 22 | + [bool]$WhatIfEnabled = $false |
| 23 | + ) |
| 24 | + |
| 25 | + $requiredKeys = @('DisplayName', 'Type', 'Path', 'SourceMarker', 'OwnershipMetadata') |
| 26 | + if ($DesiredState -eq 'Present') { |
| 27 | + $requiredKeys += 'FingerprintMetadataKey', 'Fingerprint', 'Status', 'BuildBody' |
| 28 | + } |
| 29 | + $missingKeys = @($requiredKeys | Where-Object { -not $Definition.ContainsKey($_) }) |
| 30 | + if ($missingKeys.Count -gt 0) { |
| 31 | + throw "Device health script definition is missing required key(s): $($missingKeys -join ', ')" |
| 32 | + } |
| 33 | + if ($Definition.OwnershipMetadata -isnot [hashtable] -or $Definition.OwnershipMetadata.Count -eq 0) { |
| 34 | + throw 'Device health script definition requires non-empty ownership metadata.' |
| 35 | + } |
| 36 | + |
| 37 | + $escapedDisplayName = $Definition.DisplayName.Replace("'", "''") |
| 38 | + $filter = [uri]::EscapeDataString("displayName eq '$escapedDisplayName'") |
| 39 | + $response = Invoke-HydrationGraphRequest -Method GET -Uri "beta/deviceManagement/deviceHealthScripts?`$filter=$filter" |
| 40 | + $existingScripts = @($response.value | Where-Object { $null -ne $_ }) |
| 41 | + $ownedScripts = [System.Collections.Generic.List[object]]::new() |
| 42 | + |
| 43 | + foreach ($existingScript in $existingScripts) { |
| 44 | + $description = [string]$existingScript.description |
| 45 | + if (-not (Test-HydrationKitObject -Description $description)) { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + $descriptionLines = $description -split "`r?`n" | ForEach-Object { $_.Trim() } |
| 50 | + if ($descriptionLines -notcontains $Definition.SourceMarker) { |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + $metadata = ConvertFrom-HydrationDeviceHealthScriptDescription -Description $description |
| 55 | + $isOwned = $true |
| 56 | + foreach ($key in $Definition.OwnershipMetadata.Keys) { |
| 57 | + if (-not $metadata.ContainsKey($key) -or $metadata[$key] -cne [string]$Definition.OwnershipMetadata[$key]) { |
| 58 | + $isOwned = $false |
| 59 | + break |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if ($isOwned) { |
| 64 | + $ownedScripts.Add([pscustomobject]@{ |
| 65 | + Script = $existingScript |
| 66 | + Metadata = $metadata |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if ($DesiredState -eq 'Remove') { |
| 72 | + $results = [System.Collections.Generic.List[object]]::new() |
| 73 | + foreach ($ownedScript in $ownedScripts) { |
| 74 | + if ($WhatIfEnabled) { |
| 75 | + $results.Add((Add-HydrationDryRunResult -Action 'WouldDelete' -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type)) |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + if (-not $PSCmdlet.ShouldProcess($Definition.DisplayName, 'Delete remediation')) { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + Invoke-HydrationGraphRequest -Method DELETE -Uri "beta/deviceManagement/deviceHealthScripts/$($ownedScript.Script.id)" | Out-Null |
| 85 | + Write-HydrationLog -Message " Deleted: $($Definition.DisplayName)" -Level Info |
| 86 | + $results.Add((New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Deleted' -Status 'Removed')) |
| 87 | + } catch { |
| 88 | + $errorMessage = Get-GraphErrorMessage -ErrorRecord $_ |
| 89 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - $errorMessage" -Level Warning |
| 90 | + $results.Add((New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status $errorMessage)) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return @($results) |
| 95 | + } |
| 96 | + |
| 97 | + if ($ownedScripts.Count -gt 1) { |
| 98 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - Multiple matching hydration-owned remediations exist; remove them explicitly before importing." -Level Warning |
| 99 | + return @(New-HydrationResult -Name $Definition.DisplayName -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status 'Multiple owned remediations') |
| 100 | + } |
| 101 | + |
| 102 | + $ownedScript = $ownedScripts | Select-Object -First 1 |
| 103 | + if ($existingScripts.Count -gt 0 -and -not $ownedScript) { |
| 104 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - A remediation with this name already exists but is not owned by Intune Hydration Kit." -Level Warning |
| 105 | + return @(New-HydrationResult -Name $Definition.DisplayName -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status 'Name collision') |
| 106 | + } |
| 107 | + |
| 108 | + if ($ownedScript -and $ownedScript.Metadata[$Definition.FingerprintMetadataKey] -ceq $Definition.Fingerprint) { |
| 109 | + Write-HydrationLog -Message " Skipped: $($Definition.DisplayName)" -Level Info |
| 110 | + return @(New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Skipped' -Status 'Already current') |
| 111 | + } |
| 112 | + |
| 113 | + if ($ownedScript -and $Definition.ContainsKey('RequireUnassigned') -and $Definition.RequireUnassigned) { |
| 114 | + try { |
| 115 | + $assignmentResponse = Invoke-HydrationGraphRequest -Method GET -Uri "beta/deviceManagement/deviceHealthScripts/$($ownedScript.Script.id)/assignments?`$top=1" |
| 116 | + $assignments = @($assignmentResponse.value | Where-Object { $null -ne $_ }) |
| 117 | + } catch { |
| 118 | + $errorMessage = Get-GraphErrorMessage -ErrorRecord $_ |
| 119 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - Could not verify assignments: $errorMessage" -Level Warning |
| 120 | + return @(New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status "Assignment check failed: $errorMessage") |
| 121 | + } |
| 122 | + |
| 123 | + if ($assignments.Count -gt 0) { |
| 124 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - The remediation has assignments and will not be updated." -Level Warning |
| 125 | + return @(New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status 'Assigned') |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if ($WhatIfEnabled) { |
| 130 | + $action = if ($ownedScript) { 'WouldUpdate' } else { 'WouldCreate' } |
| 131 | + return @(Add-HydrationDryRunResult -Action $action -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type) |
| 132 | + } |
| 133 | + |
| 134 | + $operation = if ($ownedScript) { 'Update remediation' } else { 'Create remediation' } |
| 135 | + if (-not $PSCmdlet.ShouldProcess($Definition.DisplayName, $operation)) { |
| 136 | + return @() |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + $body = & $Definition.BuildBody (-not $ownedScript) |
| 141 | + if ($ownedScript) { |
| 142 | + Invoke-HydrationGraphRequest -Method PATCH -Uri "beta/deviceManagement/deviceHealthScripts/$($ownedScript.Script.id)" -Body $body | Out-Null |
| 143 | + Write-HydrationLog -Message " Updated: $($Definition.DisplayName)" -Level Info |
| 144 | + return @(New-HydrationResult -Name $Definition.DisplayName -Id $ownedScript.Script.id -Path $Definition.Path -Type $Definition.Type -Action 'Updated' -Status $Definition.Status) |
| 145 | + } |
| 146 | + |
| 147 | + $createdScript = Invoke-HydrationGraphRequest -Method POST -Uri 'beta/deviceManagement/deviceHealthScripts' -Body $body |
| 148 | + Write-HydrationLog -Message " Created: $($Definition.DisplayName)" -Level Info |
| 149 | + return @(New-HydrationResult -Name $Definition.DisplayName -Id $createdScript.id -Path $Definition.Path -Type $Definition.Type -Action 'Created' -Status $Definition.Status) |
| 150 | + } catch { |
| 151 | + $errorMessage = Get-GraphErrorMessage -ErrorRecord $_ |
| 152 | + Write-HydrationLog -Message " Failed: $($Definition.DisplayName) - $errorMessage" -Level Warning |
| 153 | + return @(New-HydrationResult -Name $Definition.DisplayName -Path $Definition.Path -Type $Definition.Type -Action 'Failed' -Status $errorMessage) |
| 154 | + } |
| 155 | +} |
0 commit comments