Connect-MSGraph | Out-Null $path = "c:\temp\pro" $proerrorcsv= "proremerror.csv" $report_csv= "$path\$proerrorcsv" $graphApiVersion = "Beta" $graphUrl = "https://graph.microsoft.com/$graphApiVersion" $result = Invoke-MSGraphRequest -Url "$graphUrl/deviceManagement/deviceHealthScripts" -HttpMethod GET $Remediation_details = @() $scriptIds = $result.value | Select-Object id foreach($scriptId in $scriptIds){ $urlname = "$graphUrl/deviceManagement/deviceHealthScripts/$($scriptId.id)" $name = Invoke-MSGraphRequest -Url $urlname -HttpMethod GET $displayname = $name.displayname $url = "$graphUrl/deviceManagement/deviceHealthScripts/$($scriptId.id)/deviceRunStates/" + '?$expand=*' $script = Invoke-MSGraphRequest -Url $url -HttpMethod GET $script = $script.value | select-object -first 1 $Remediation_Values = New-Object PSObject $Script_State = $script.detectionState $Script_lastStateUpdateDateTime = $script.lastStateUpdateDateTime $Script_lastSyncDateTime = $script.lastSyncDateTime $Script_DetectionScriptOutput = $script.preRemediationDetectionScriptOutput $Script_DetectionScriptError = $script.preRemediationDetectionScriptError $Script_ScriptError = $script.remediationScriptError $Script_PosteDetectionScriptOutput = $script.postRemediationDetectionScriptOutput $Script_PostDetectionScriptError = $script.postRemediationDetectionScriptError $deviceName = $script.managedDevice.deviceName $osVersion = $script.managedDevice.osVersion $userPrincipalName = $script.managedDevice.userPrincipalName $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Display name" $displayname -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Device name" $deviceName -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "OS version" $osVersion -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "User name" $userPrincipalName -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty State $Script_State -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Last update" $Script_lastStateUpdateDateTime -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Last sync" $Script_lastSyncDateTime -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Pre detetection output" $Script_DetectionScriptOutput -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Pre detection error" $Script_DetectionScriptError -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Error" $Script_ScriptError -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Post detection output" $Script_PosteDetectionScriptOutput -passthru -force $Remediation_Values = $Remediation_Values | Add-Member NoteProperty "Post detection error" $Script_PostDetectionScriptError -passthru -force $Remediation_details += $Remediation_Values } $Remediation_details | Select-Object * | export-csv $report_csv -notype -Delimiter ";"