diff --git a/BestPracticeAnalyser_All/run.ps1 b/BestPracticeAnalyser_All/run.ps1 index 9185c07e02d6..dffb59e46639 100644 --- a/BestPracticeAnalyser_All/run.ps1 +++ b/BestPracticeAnalyser_All/run.ps1 @@ -1,8 +1,8 @@ param($tenant) $TenantName = Get-Tenants | Where-Object -Property defaultDomainName -EQ $tenant -Set-Location (Get-Item $PSScriptRoot).Parent.FullName -$TemplatesLoc = Get-ChildItem "Config\*.BPATemplate.json" +$CippRoot = (Get-Item $PSScriptRoot).Parent.FullName +$TemplatesLoc = Get-ChildItem "$CippRoot\Config\*.BPATemplate.json" $Templates = $TemplatesLoc | ForEach-Object { $Template = $(Get-Content $_) | ConvertFrom-Json [PSCustomObject]@{ @@ -26,89 +26,85 @@ $AddRow = foreach ($Template in $templates) { if ($Field.Where) { $filterscript = [scriptblock]::Create($Field.Where) } else { $filterscript = { $true } } try { switch ($field.API) { - "Graph" { + 'Graph' { $paramsField = @{ uri = $field.URL tenantid = $TenantName.defaultDomainName } - if ($Field.parameters) { + if ($Field.parameters.psobject.properties.name) { $field.Parameters | ForEach-Object { Write-Host "Doing: $($_.psobject.properties.name) with value $($_.psobject.properties.value)" - $paramsField.Add($_.psobject.properties.name, $_.psobject.properties.value) + $paramsField[$_.psobject.properties.name] = $_.psobject.properties.value } } $FieldInfo = New-GraphGetRequest @paramsField | Where-Object $filterscript | Select-Object $field.ExtractFields } - "Exchange" { - if ($field.Command -notlike "get-*") { - Write-LogMessage -API "BPA" -tenant $tenant -message "The BPA only supports get- exchange commands. A set or update command was used." -sev Error + 'Exchange' { + if ($field.Command -notlike 'get-*') { + Write-LogMessage -API 'BPA' -tenant $tenant -message 'The BPA only supports get- exchange commands. A set or update command was used.' -sev Error break - } - else { + } else { $paramsField = @{ tenantid = $TenantName.defaultDomainName cmdlet = $field.Command } - if ($Field.Parameters) { $paramsfield.add('cmdparams', $field.parameters) } - $FieldInfo = New-ExoRequest @paramsField | Where-Object $filterscript | Select-Object $field.ExtractFields + if ($Field.Parameters) { $paramsfield.'cmdparams' = $field.parameters } + $FieldInfo = New-ExoRequest @paramsField | Where-Object $filterscript | Select-Object $field.ExtractFields } } - "CIPPFunction" { - if ($field.Command -notlike "get-CIPP*") { - Write-LogMessage -API "BPA" -tenant $tenant -message "The BPA only supports get-CIPP commands. A set or update command was used, or a command that is not allowed." -sev Error + 'CIPPFunction' { + if ($field.Command -notlike 'get-CIPP*') { + Write-LogMessage -API 'BPA' -tenant $tenant -message 'The BPA only supports get-CIPP commands. A set or update command was used, or a command that is not allowed.' -sev Error break } $paramsField = @{ TenantFilter = $TenantName.defaultDomainName } - if ($field.parameters) { + if ($field.parameters.psobject.properties.name) { $field.Parameters | ForEach-Object { - $paramsField.Add($_.psobject.properties.name, $_.psobject.properties.value) + $paramsField[$_.psobject.properties.name] = $_.psobject.properties.value } } - $FieldInfo = & $field.Command @paramsField | Where-Object $filterscript | Select-Object $field.ExtractFields + $FieldInfo = & $field.Command @paramsField | Where-Object $filterscript | Select-Object $field.ExtractFields } } - } - catch { + } catch { Write-Host "Error getting $($field.Name) in $($field.api) for $($TenantName.displayName) with GUID $($TenantName.customerId). Error: $($_.Exception.Message)" - Write-LogMessage -API "BPA" -tenant $tenant -message "Error getting $($field.Name) for $($TenantName.displayName) with GUID $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error - $fieldinfo = "FAILED" - $field.StoreAs = "string" - } + Write-LogMessage -API 'BPA' -tenant $tenant -message "Error getting $($field.Name) for $($TenantName.displayName) with GUID $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error + $fieldinfo = 'FAILED' + $field.StoreAs = 'string' + } try { switch -Wildcard ($field.StoreAs) { - "*bool" { + '*bool' { if ($field.ExtractFields.Count -gt 1) { - Write-LogMessage -API "BPA" -tenant $tenant -message "The BPA only supports 1 field for a bool. $($field.ExtractFields.Count) fields were specified." -sev Error + Write-LogMessage -API 'BPA' -tenant $tenant -message "The BPA only supports 1 field for a bool. $($field.ExtractFields.Count) fields were specified." -sev Error break } if ($null -eq $FieldInfo.$($field.ExtractFields)) { $FieldInfo = $false } $Result.Add($field.Name, [bool]$FieldInfo.$($field.ExtractFields)) } - "JSON" { - if ($FieldInfo -eq $null) { $JsonString = '{}' } else { $JsonString = (ConvertTo-Json -Depth 15 -InputObject $FieldInfo) } + 'JSON' { + if ($FieldInfo -eq $null) { $JsonString = '{}' } else { $JsonString = (ConvertTo-Json -Depth 15 -InputObject $FieldInfo -Compress) } $Result.Add($field.Name, $JSONString) } - "string" { + 'string' { $Result.Add($field.Name, [string]$FieldInfo) } } - } - catch { - Write-LogMessage -API "BPA" -tenant $tenant -message "Error storing $($field.Name) for $($TenantName.displayName) with GUID $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error - $Result.Add($field.Name, "FAILED") + } catch { + Write-LogMessage -API 'BPA' -tenant $tenant -message "Error storing $($field.Name) for $($TenantName.displayName) with GUID $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error + $Result.Add($field.Name, 'FAILED') } } - + if ($Result) { try { Add-AzDataTableEntity @Table -Entity $Result -Force - } - catch { - Write-LogMessage -API "BPA" -tenant $tenant -message "Error getting saving data for $($template.Name) - $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error + } catch { + Write-LogMessage -API 'BPA' -tenant $tenant -message "Error getting saving data for $($template.Name) - $($TenantName.customerId). Error: $($_.Exception.Message)" -sev Error } } diff --git a/ListBPA/run.ps1 b/ListBPA/run.ps1 index cd6b5b450bb0..0fae94c1b8a8 100644 --- a/ListBPA/run.ps1 +++ b/ListBPA/run.ps1 @@ -8,12 +8,13 @@ $APIName = $TriggerMetadata.FunctionName $Table = get-cipptable 'cachebpav2' $name = $Request.query.Report -if ($name -eq $null) { $name = "CIPP Best Practices v1.0 - Table view" } +if ($name -eq $null) { $name = 'CIPP Best Practices v1.0 - Table view' } # Get all possible JSON files for reports, find the correct one, select the Columns $JSONFields = @() $Columns = $null -(Get-ChildItem -Path "Config\*.BPATemplate.json" -Recurse | Select-Object -ExpandProperty FullName | ForEach-Object { +$CippRoot = (Get-Item $PSScriptRoot).Parent.FullName +(Get-ChildItem -Path "$CippRoot\Config\*.BPATemplate.json" -Recurse | Select-Object -ExpandProperty FullName | ForEach-Object { $Template = $(Get-Content $_) | ConvertFrom-Json if ($Template.Name -eq $NAME) { $JSONFields = $Template.Fields | Where-Object { $_.StoreAs -eq 'JSON' } | ForEach-Object { $_.name } @@ -23,14 +24,14 @@ $Columns = $null }) -if ($Request.query.tenantFilter -ne "AllTenants" -and $Style -eq "Tenant") { +if ($Request.query.tenantFilter -ne 'AllTenants' -and $Style -eq 'Tenant') { $mergedObject = New-Object pscustomobject $Data = (Get-AzDataTableEntity @Table -Filter "PartitionKey eq '$($Request.query.tenantFilter)'") | ForEach-Object { $row = $_ - $JSONFields | ForEach-Object { + $JSONFields | ForEach-Object { $jsonContent = $row.$_ - if ($jsonContent -ne $null -and $jsonContent -ne "FAILED") { + if ($jsonContent -ne $null -and $jsonContent -ne 'FAILED') { $row.$_ = $jsonContent | ConvertFrom-Json -Depth 15 } } @@ -40,13 +41,12 @@ if ($Request.query.tenantFilter -ne "AllTenants" -and $Style -eq "Tenant") { } $Data = $mergedObject -} -else { +} else { $Data = (Get-AzDataTableEntity @Table -Filter "RowKey eq '$NAME'") | ForEach-Object { $row = $_ $JSONFields | ForEach-Object { $jsonContent = $row.$_ - if ($jsonContent -ne $null -and $jsonContent -ne "FAILED") { + if ($jsonContent -ne $null -and $jsonContent -ne 'FAILED') { $row.$_ = $jsonContent | ConvertFrom-Json -Depth 15 } } @@ -64,8 +64,8 @@ $Results = [PSCustomObject]@{ if (!$Results) { $Results = @{ - Columns = @( value = "Results"; name = "Results") - Data = @(@{ Results = "The BPA has not yet run." }) + Columns = @( value = 'Results'; name = 'Results') + Data = @(@{ Results = 'The BPA has not yet run.' }) } }