|
| 1 | +function Invoke-ListStandardsCurrentState { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + Tenant.Standards.Read |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $TenantFilter = $Request.Query.tenantFilter |
| 13 | + |
| 14 | + if (-not $TenantFilter) { |
| 15 | + return [HttpResponseContext]@{ |
| 16 | + StatusCode = [HttpStatusCode]::BadRequest |
| 17 | + Body = @{ Results = 'tenantFilter is required' } |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + try { |
| 22 | + $Table = Get-CIPPTable -TableName 'CippStandardsReports' |
| 23 | + $Filter = "PartitionKey eq '$TenantFilter'" |
| 24 | + $Standards = @(Get-CIPPAzDataTableEntity @Table -Filter $Filter) |
| 25 | + |
| 26 | + $Results = foreach ($Standard in $Standards) { |
| 27 | + $CurrentValue = if ($Standard.CurrentValue -and (Test-Json -Json $Standard.CurrentValue -ErrorAction SilentlyContinue)) { |
| 28 | + ConvertFrom-Json -InputObject $Standard.CurrentValue -ErrorAction SilentlyContinue |
| 29 | + } else { |
| 30 | + $Standard.CurrentValue |
| 31 | + } |
| 32 | + |
| 33 | + $ExpectedValue = if ($Standard.ExpectedValue -and (Test-Json -Json $Standard.ExpectedValue -ErrorAction SilentlyContinue)) { |
| 34 | + ConvertFrom-Json -InputObject $Standard.ExpectedValue -ErrorAction SilentlyContinue |
| 35 | + } else { |
| 36 | + $Standard.ExpectedValue |
| 37 | + } |
| 38 | + |
| 39 | + $FieldValue = $Standard.Value |
| 40 | + if ($FieldValue -is [System.Boolean]) { |
| 41 | + $FieldValue = [bool]$FieldValue |
| 42 | + } elseif ($FieldValue -and (Test-Json -Json "$FieldValue" -ErrorAction SilentlyContinue)) { |
| 43 | + $FieldValue = ConvertFrom-Json -InputObject $FieldValue -ErrorAction SilentlyContinue |
| 44 | + } |
| 45 | + |
| 46 | + $IsCompliant = ($FieldValue -eq $true) -or ($Standard.CurrentValue -and $Standard.CurrentValue -eq $Standard.ExpectedValue) |
| 47 | + |
| 48 | + [PSCustomObject]@{ |
| 49 | + StandardName = $Standard.RowKey |
| 50 | + Compliant = $IsCompliant |
| 51 | + CurrentValue = $CurrentValue |
| 52 | + ExpectedValue = $ExpectedValue |
| 53 | + Value = $FieldValue |
| 54 | + LicenseAvailable = $Standard.LicenseAvailable |
| 55 | + LastRefresh = if ($Standard.TimeStamp) { $Standard.TimeStamp.ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') } else { $null } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return [HttpResponseContext]@{ |
| 60 | + StatusCode = [HttpStatusCode]::OK |
| 61 | + Body = @($Results) |
| 62 | + } |
| 63 | + } catch { |
| 64 | + $ErrorMessage = Get-CippException -Exception $_ |
| 65 | + Write-LogMessage -API $APIName -tenant $TenantFilter -message "Failed to get standards current state: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage |
| 66 | + return [HttpResponseContext]@{ |
| 67 | + StatusCode = [HttpStatusCode]::InternalServerError |
| 68 | + Body = @{ Results = "Failed: $($ErrorMessage.NormalizedError)" } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments