Issue with report generation - 'Unexpected token '.\ConfigurationData.psd1' in expression or statement.'
Every report that I try to generate currently has 'Error parsing configuration...' and 'Unexpected token '.\ConfigurationData.psd1' in expression or statement.'
I have a series of .ps1 config exports that have been generated, which are all stored in a file path C:\Development\m365-DSC\m-365-dsc-poc\baselines (with folders for each workload within this).
I am trying to generate the json reports from these and add these to file paths C:\Development\m365-DSC\m-365-dsc-poc\reports (again with folders for each workload within this).
The debugging lines I've added to the script below show both the config path and the report path as being correct.
My issue seems to be when it comes to parsing the .ps1 config docs to generate the report, as they all have the following last line within them which is leading to this 'unexpected token' exception message that appears in my log:
<name of resource> -ConfigurationData .\ConfigurationData.psd1
Can anyone help with why this is throwing an error? I have generated reports from similar config files previously and not hit this particular issue. It may be my script but any help debugging would be really appreciated.
` $parentPath = "C:\Development\m365-DSC\m-365-dsc-poc" $baselinesPath = Join-Path -Path $parentPath -ChildPath "baselines" $reportsPath = Join-Path -Path $parentPath -ChildPath "reports"
if (-not (Test-Path -Path $reportsPath)) { New-Item -Path $reportsPath -ItemType Directory | Out-Null Write-Host "Created reports directory" -ForegroundColor Green }
$workloadFolders = Get-ChildItem -Path $baselinesPath -Directory
foreach ($workloadFolder in $workloadFolders) { $workloadName = $workloadFolder.Name Write-Host "Processing workload: $workloadName" -ForegroundColor Cyan
# Create corresponding reports folder
$workloadReportPath = Join-Path -Path $reportsPath -ChildPath $workloadName
if (-not (Test-Path -Path $workloadReportPath)) {
New-Item -Path $workloadReportPath -ItemType Directory | Out-Null
Write-Host " Created report directory for $workloadName" -ForegroundColor Green
}
# Get all PS1 files in this workload folder
$configFiles = Get-ChildItem -Path $workloadFolder.FullName -Filter "*.ps1"
foreach ($configFile in $configFiles) {
$resourceName = [System.IO.Path]::GetFileNameWithoutExtension($configFile.Name)
$configPath = $configFile.FullName
$reportPath = Join-Path -Path $workloadReportPath -ChildPath "$resourceName.json"
# Add debugging to verify paths
Write-Host " Config file path: $configPath" -ForegroundColor Yellow
Write-Host " Report path: $reportPath" -ForegroundColor Yellow
# Verify paths are not empty
if ([string]::IsNullOrEmpty($configPath)) {
Write-Host " ERROR: Config path is empty!" -ForegroundColor Red
continue
}
if ([string]::IsNullOrEmpty($reportPath)) {
Write-Host " ERROR: Report path is empty!" -ForegroundColor Red
continue
}
# Verify config file exists
if (-not (Test-Path -Path $configPath)) {
Write-Host " ERROR: Config file does not exist: $configPath" -ForegroundColor Red
continue
}
Write-Host " Generating report for $resourceName..." -ForegroundColor Yellow
try {
# Generate the report directly
New-M365DSCReportFromConfiguration -Type 'JSON' -ConfigurationPath $configPath -OutputPath $reportPath
Write-Host " Successfully generated report for $resourceName" -ForegroundColor Green
}
catch {
Write-Host " Failed to generate report for ${resourceName}: $($_.Exception.Message)" -ForegroundColor Red
}
}
}
Write-Host "nReport generation complete!" -ForegroundColor Green Write-Host "Reports are available in: $reportsPath" -ForegroundColor Cyan
I've encountered the same issue a couple of times now, and it always seems to be the case if it's UTF-16LE encoded content instead of UTF-8, but it also seems to be related to PowerShell 7 in some ways. I couldn't quite figure out the relation though, but I think it's about the file encoding or something of the content in the file... Unfortunately no idea how to properly address this.
@RosalindHook I just had the issue again today in PowerShell 7. I then ran the command in an elevated Windows PowerShell session (where it worked), and afterwards in a fresh PowerShell 7 session again. It then worked as well. Can you give that a try?
Any updates?
I missed the message from 3 weeks ago so will look at this, although we are working with Powershell 5.1 rather than 7. It's tricky because we are now running scripts in the pipeline - from gitlab to a devbox in powershell. I have given up on the .json reports that we can't get for the time being because I have all the .ps1 files I need as a baseline, and the delta reports (including .json) generate fine - so for our case for now I can get by without the json versions of the export reports.
Gotcha. Then I'll also close this issue (for the time being). If you stumble upon it in any way that necessitates further investigation, feel free to reopen it.