Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

How to suppress opening of delta report from New-M365DSCDeltaReport?

Open Kikaider opened this issue 4 years ago • 8 comments

How do I suppress the opening of the generated delta report from the cmdlet New-M365DSCDeltaReport?

I am trying to monitor configuration drift using M365DSC and an Azure Runbook. The Runbook fails on the New-M365DSCDeltaReport cmdlet with the error "The operation was canceled by the user". I believe this error is because the New-M365DSCDeltaReport cmdlet is trying to open the generated delta report, which is what happens when I run the code on my local machine.

The cmdlet I am using is:

New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename *>&1 | Out-Null

I also tried redirecting all the streams straight to $null and other random things with no success:

New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename *> $null $nowhere = New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename $nowhere = New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename *>&1 | Out-Null New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename -OutBuffer 0 *>&1 | Out-Null New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $deltaFilename -OutVariable $nulltemp

Kikaider avatar Aug 19 '22 15:08 Kikaider

In the cmdlet New-M365DSCDeltaRport in Microsoft365DSC/Modules/Microsoft365DSC/Modules/M365DSCReport.psm1 there is an Invoke-Item cmdlet at line 1071. Is this supposed to be there?

if (-not [System.String]::IsNullOrEmpty($OutputPath)) { $reportSB.ToString() | Out-File $OutputPath Invoke-Item $OutputPath } else { return $reportSB.ToString() }

Kikaider avatar Aug 22 '22 14:08 Kikaider

This issue also affects Assert-M365DSCBlueprint, as that cmdlet calls New-M365DSCDeltaRport.

Kikaider avatar Aug 25 '22 12:08 Kikaider

You could leave the OutputPath as $null and call the function with

cmdLetName -OutputPath $null | Out-File <PathToYourFile> 

andikrueger avatar Aug 25 '22 12:08 andikrueger

Well...I was typing out the below suggestion when @andikrueger replied. His suggestion worked like a charm and does not require a code change.

Perhaps add a switch to suppress the opening of the report?

[Parameter()]
[System.String]
$SuppressOpenReport = $false,
if (-not [System.String]::IsNullOrEmpty($SuppressOpenReport))
{
    $reportSB.ToString() | Out-File $OutputPath
    If (-not $SuppressOpenReport) { Invoke-Item $OutputPath }
}
else
{
    return $reportSB.ToString()
}

Kikaider avatar Aug 25 '22 13:08 Kikaider

Broke in v1.22.831.1 with error "Cannot convert the "Loading file 'E:\M365DSC\src\M365x1234-SPOSharingSettings.ps1'" value of type "System.String" to type "System.Collections.Hashtable"." from cmdlet

New-M365DSCDeltaReport -Source $sourceFilename -Destination $exportFilename -OutputPath $null | Out-File -FilePath $deltaFilename

Although the delta html file is produced. As a workaround, I caught the error [System.Management.Automation.ArgumentTransformationMetadataException] and ignored it.

PS - I am running M365DSC in an Azure Runbook.

Kikaider avatar Sep 07 '22 02:09 Kikaider

Could you share the output of $error. This should provide a bit more context.

Would you be able to generate a report of your source and destination files? Do you get the same errors? What happens, if you run the report locally?

andikrueger avatar Sep 07 '22 10:09 andikrueger

New-M365DSCDeltaReport -Source "runbook_dscconfig.ps1" -Destination "runbook_export.ps1" -OutputPath $null | Out-File -FilePath "runbook_2022-Sep-07-0652AM.html"

Cannot convert the "Loading file 'E:\M365DSC\M365x758710\M365DSC\tmp\runbook_export.ps1'" value of type "System.String" to type "System.Collections.Hashtable".
At C:\Program Files\WindowsPowerShell\Modules\Microsoft365DSC\1.22.831.1\modules\M365DSCReport.psm1:598 char:14
+     foreach ($destinationResource in $DestinationObject)
+              ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

Error occurs in Runbook and locally using M365DSC v1.22.831.1.

runbook_dscconfig.txt runbook_export.txt runbook_2022-Sep-07-0652AM.txt

Kikaider avatar Sep 07 '22 11:09 Kikaider

Issue #2260 submitted for this new, ah, issue.

Kikaider avatar Sep 07 '22 22:09 Kikaider