Skip to content

Fix Windows PowerShell 5.1 import failure from non-ASCII module source (#1603) - #1611

Open
Eduard Arbona (earbona23) wants to merge 3 commits into
microsoftgraph:mainfrom
earbona23:fix/1603-source-non-ascii-ps51-import
Open

Eduard Arbona (earbona23) wants to merge 3 commits into
microsoftgraph:mainfrom
earbona23:fix/1603-source-non-ascii-ps51-import

Conversation

@earbona23

Copy link
Copy Markdown

Description

Import-Module Microsoft.Entra.Applications (and Microsoft.Entra.Beta.Applications) throws parser errors on Windows PowerShell 5.1 in 1.3.0, as reported in #1603.

Root cause

The shipped .psm1 is assembled by concatenating the per-cmdlet .ps1 source files and is written without a BOM. Several of the new AgentId / Blueprint / MCPServer source files contain non-ASCII characters (→, em/en dashes, ✓) in comments and in Write-Host / Write-Verbose strings.

Windows PowerShell 5.1 decodes a BOM-less file with the system ANSI code page rather than UTF-8, so each multi-byte character becomes a stray curly-quote that unbalances the surrounding string. The parser then fails with Missing closing ')' in expression and The string is missing the terminator, and the import aborts. PowerShell 7 reads BOM-less files as UTF-8, so CI and PowerShell 7 users never hit this.

Fix

  • Replace the non-ASCII characters in the affected source files with ASCII equivalents (→→->, dashes→-, ✓→[OK]). ASCII parses identically under every encoding.
  • Two Users cmdlet files carried the same non-ASCII-without-BOM hazard (a curly apostrophe in a HelpMessage); they are cleaned as well so the source tree is fully ASCII. Those two do not break the import, only garble help text, but they trip the same PSScriptAnalyzer rule.
  • Add test/Entra/SourceFileEncoding.Tests.ps1, a Pester regression test that fails if any module/**/*.ps1 reintroduces non-ASCII bytes.

Testing

  • Simulated the PS 5.1 read (decode source bytes as Windows-1252, then parse via the PowerShell AST parser): the original files produce the exact parser errors from the issue; the fixed files parse with zero errors.
  • Invoke-ScriptAnalyzer no longer reports PSUseBOMForUnicodeEncodedFile on any of the changed files (16 findings removed, no new findings).
  • New Pester test passes; verified it fails when a non-ASCII character is reintroduced.

Fixes #1603

microsoftgraph#1603)

The per-cmdlet source files under the Applications and Beta.Applications
modules contained non-ASCII characters (the arrow "->", em/en dashes, and
check marks) inside comments and Write-Host/Write-Verbose strings. These
files are concatenated without a BOM into the shipped .psm1.

Windows PowerShell 5.1 decodes a BOM-less file using the system ANSI code
page instead of UTF-8, so each multi-byte character turns into stray quote
characters that unbalance the surrounding strings. The parser then fails
with "Missing closing ')' in expression" and "The string is missing the
terminator", and Import-Module aborts. PowerShell 7 (Core) reads BOM-less
files as UTF-8, which is why CI and PowerShell 7 users never saw the break.

Replace the non-ASCII characters with ASCII equivalents so the generated
module parses identically under every encoding. Two Users cmdlet files
carried the same non-ASCII-without-BOM hazard (a curly apostrophe in a
HelpMessage) and are cleaned as well. Add a Pester regression test that
fails if any module source file reintroduces non-ASCII bytes.

Fixes microsoftgraph#1603
@earbona23
Eduard Arbona (earbona23) requested a review from a team as a code owner September 3, 2026 12:28
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@learn-build-service-prod

Copy link
Copy Markdown

Learn Build status updates of commit 0b53622:

❌ Validation status: errors

Please follow instructions here which may help to resolve issue.

File Status Preview URL Details
❌Error Details

  • Line 0, Column 0: [Error: PSMD2Yaml_FileLoadFailed] Failed to load file: C:/LocalRun/W/qy1s-s/module/mapping/monikerMapping.json. PackageRoot, ReferenceTocUrl, and ConceptualTocUrl are required for every moniker. PackageRoot should be a valid relative path to docset root.

For more details, please refer to the build report.

Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them.

@earbona23

Copy link
Copy Markdown
Author

Flagging that the red OpenPublishing.Build check is not caused by this PR.

The failure is PSMD2Yaml_FileLoadFailed on module/mapping/monikerMapping.json ("PackageRoot, ReferenceTocUrl, and ConceptualTocUrl are required for every moniker"). That file is not touched here — this PR only changes source encoding in 16 cmdlet files plus one test.

The same status is red on every recent open PR in this repo (#1601, #1572, #1529), so it looks like a docset configuration issue rather than per-PR validation. Happy to rebase if a fix lands.

@SamErde Sam Erde (SamErde) left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested action: Approve — reviewed commit 0b5362241f804f094e179b6b067d3d0d9c603b1f.1

The source changes address #1603: the affected non-ASCII characters are replaced with ASCII without changing the cmdlets' control flow or Graph requests.

  • Independently scanned all 639 module PowerShell source files at this commit: no non-ASCII bytes remain.
  • Decoded the 16 changed production files using Windows-1252 and parsed them: zero parser errors.
  • Ran the new Pester regression file under PowerShell 7.6.7/Pester 5.9.1: 641 passed.
  • No new security-sensitive behavior or material production regression found in this diff.

Nonblocking test-portability note: Join-Path $PSScriptRoot '..' '..' 'module' in the new test uses the PowerShell 6+ overload. Windows PowerShell 5.1 rejects that invocation; use nested joins if the guard itself must run there. This does not affect the shipped cmdlet fix.

Limitations: I did not build and import every packaged module on Windows PowerShell 5.1. OpenPublishing remains red with a moniker-mapping configuration error; that file is unchanged here, but the overall checks are not all green.


Footnotes

  1. AI-assisted review by Codex (1 agent). Model: gpt-6-astra. Configured reasoning effort: high (verified from the review session metadata). Review effort: detailed source review with the validation described above. Skill: engineering:code-review. Tools: GitHub API/CLI, Git, PowerShell source analysis and isolated runtime probes. ↩

The regression guard added for microsoftgraph#1603 resolved the module root with
`Join-Path $PSScriptRoot '..' '..' 'module'`. The third positional
argument binds to -AdditionalChildPath, introduced in PowerShell 6.
Windows PowerShell 5.1 only accepts -Path and -ChildPath, so it rejects
that invocation with a parameter binding error.

That left the guard unable to run on the one platform the fix targets:
validating microsoftgraph#1603 under Windows PowerShell 5.1 would fail before a single
source file was checked.

Replaced with nested Join-Path calls, which resolve identically on both
editions. What the guard asserts is unchanged: 641 tests pass.
@earbona23

Eduard Arbona (earbona23) commented Oct 18, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review, Sam Erde (@SamErde). Boss I applied the test-portability note, and it turned out to be more than cosmetic.

The guard resolved the module root with Join-Path $PSScriptRoot '..' '..' 'module'. The third positional argument binds to -AdditionalChildPath, which arrived in PowerShell 6. The 5.1 reference lists only [-Path] <String[]> [-ChildPath] <String> [-Resolve] [-Credential] [-UseTransaction], so Windows PowerShell 5.1 rejects that invocation with a parameter binding error.

That means the regression guard for #1603 could not run on the one edition the fix targets: anyone validating this under Windows PowerShell 5.1 would hit the binding error before a single source file was read. A guard that cannot execute where the bug lives reports nothing.

Fixed in 2751155 with nested Join-Path calls, which resolve identically on both editions. Verified:

  • Both forms produce the same path (confirmed in PowerShell 7.6.6).
  • The file parses with zero errors.
  • The suite still reports 641 passed, 0 failed (Pester 6.2.0) - the same count you measured with Pester 5.9.1, so the change is behaviour-neutral.

Only the test file changed; the 16 cmdlet source files are untouched. Sorry for the extra review round on an approved commit - the change is three lines and does not alter what the guard asserts.

On OpenPublishing.Build: still red with the same monikerMapping.json configuration error, unrelated to this branch. 1es-EntraPowerShell-PR is still expected but never reported; it may need /azp run from someone authorized. The branch is also behind base - I deliberately did not rebase, to avoid rewriting history under a review. Happy to do it whenever a maintainer prefers. Thanks

@learn-build-service-prod

Copy link
Copy Markdown

Learn Build status updates of commit 2751155:

❌ Validation status: errors

Please follow instructions here which may help to resolve issue.

File Status Preview URL Details
❌Error Details

  • Line 0, Column 0: [Error: PSMD2Yaml_FileLoadFailed] Failed to load file: C:/LocalRun/W/erze-s/module/mapping/monikerMapping.json. PackageRoot, ReferenceTocUrl, and ConceptualTocUrl are required for every moniker. PackageRoot should be a valid relative path to docset root.

For more details, please refer to the build report.

Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them.

@learn-build-service-prod

Copy link
Copy Markdown

Learn Build status updates of commit 81a7287:

❌ Validation status: errors

Please follow instructions here which may help to resolve issue.

File Status Preview URL Details
❌Error Details

  • Line 0, Column 0: [Error: PSMD2Yaml_FileLoadFailed] Failed to load file: C:/LocalRun/W/r04z-s/module/mapping/monikerMapping.json. PackageRoot, ReferenceTocUrl, and ConceptualTocUrl are required for every moniker. PackageRoot should be a valid relative path to docset root.

For more details, please refer to the build report.

Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them.

@earbona23 Eduard Arbona (earbona23) left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status, for a maintainer with write access:

Approved by Sam Erde (@SamErde) and green on everything in its control: CLA and
GitOps/AdvancedSecurity both pass. The only red is OpenPublishing.Build, and it
is not from this branch. It fails with the same PSMD2Yaml_FileLoadFailed on
module/mapping/monikerMapping.json on every recent PR in this repo (root cause
and fix in #1616). This PR changes only source-file encoding in 16 cmdlet files
plus one regression test; it touches no docs or mapping file.

Two items are outside my control as the author:

  • 1es-EntraPowerShell-PR is marked "expected" but has never reported. It likely
    needs /azp run from an authorized pipeline user.
  • Merging is blocked on a code-owner review from
    microsoftgraph/msgraph-ps-compatibility-azuread-write.

Happy to rebase onto the latest main whenever a maintainer prefers; I held off to
avoid rewriting history under an active review.

Thanks for taking a look broo

@earbona23

Copy link
Copy Markdown
Author

Status, for a maintainer with write access:

This is approved by Sam Erde (@SamErde) and green on everything in its control: CLA and GitOps/AdvancedSecurity both pass. The only red is OpenPublishing.Build, and it is not from this branch. It fails with the same PSMD2Yaml_FileLoadFailed on module/mapping/monikerMapping.json on every recent PR in this repo (root cause and proposed fix in #1616). This PR changes only source-file encoding in 16 cmdlet files plus one regression test; it touches no docs or mapping file.

Two items are outside my control as the author:

  • 1es-EntraPowerShell-PR is marked "expected" but has never reported. It likely needs /azp run from an authorized pipeline user.
  • Merging is blocked on a code-owner review from microsoftgraph/msgraph-ps-compatibility-azuread-write.

Happy to rebase onto the latest main whenever a maintainer prefers; I held off to avoid rewriting history under an active review. Thanks for taking a look.

@earbona23

Copy link
Copy Markdown
Author

Ran the Windows PowerShell 5.1 checks that were still missing, on Windows (PS 5.1.26100 and PS 7.6.6, Pester 5.7.1):

I didn't run the full module build on 5.1, since it pins Microsoft.Graph 2.25.0 and I don't have that side by side.

@SamErde

Copy link
Copy Markdown
Contributor

The module's pin to Microsoft.Graph.Authentication 2.25.0 should be removed (or bumped to a newer version) as there are some known issues with a few interim releases. This warrants a new issue/request, but to be honest, I'm not sure if Microsoft has a team dedicated to this module anymore.

@earbona23

Copy link
Copy Markdown
Author

Thanks Sam Erde (@SamErde). I measured the shipped artifact before answering, because "the pin" turns out to be two separate knobs that pull in opposite directions.

The published manifest is a floor, not an exact pin.

Microsoft.Entra.Authentication 1.3.0 (current latest on the Gallery, published 2026-05-18), line 54 of the .psd1 inside the package:

RequiredModules = @(@{ModuleName = 'Microsoft.Graph.Authentication'; ModuleVersion = '2.25.0'; })

ModuleVersion inside a RequiredModules entry is a minimum, and the nuspec carries <dependency id="Microsoft.Graph.Authentication" version="2.25.0" />, which is NuGet for "2.25.0 or later". Verified end to end (PowerShell 7.6.6, Linux):

  • Save-Module Microsoft.Entra.Authentication -RequiredVersion 1.3.0 resolves Graph.Authentication to 2.41.2, the current latest, not to 2.25.0.
  • With 2.41.2 as the only Graph.Authentication on PSModulePath, Import-Module Microsoft.Entra.Authentication imports clean: 9 cmdlets exported, Connect-Entra present, and Get-Module confirms Graph.Authentication 2.41.2 is the loaded one.

So consumers are not held at 2.25.0 today. Removing that entry would not unblock a newer release, it would only let versions below 2.25.0 satisfy the requirement.

The exact pin lives in the build toolchain, and that one does bite.

module/Entra/config/ModuleSettings.json:17 and its EntraBeta twin carry "destinationModuleVersion": "2.25.0", and it is consumed with -RequiredVersion:

  • build/Install-Dependencies.ps1:42 installs Graph.Authentication at exactly that version, and :46 does the same for every destinationModuleName.
  • build/Publish-LocalCompatModule.ps1:38 and :43.
  • src/CompatibilityAdapterBuilder.ps1:188: Import-Module $moduleName -RequiredVersion $content.destinationModuleVersion -Force.

That is what forces a contributor's machine and the pipeline onto 2.25.0, published 2024-11-21, with 23 stable releases shipped since (2.26.0 through 2.41.2, the latest published 2026-10-14). It is also the reason I could not run the full module build on 5.1 for this PR.

One trap for whoever takes it on. The two 2.25.0 values are different keys in different files, so bumping one and not the other silently splits the build environment from the shipped floor:

file key effect
module/*/config/ModuleSettings.json destinationModuleVersion exact version installed and imported at build time
module/*/config/ModuleMetadata.json requiredModulesVersion minimum written into the shipped RequiredModules (via src/EntraModuleBuilder.ps1:536 and src/CompatibilityAdapterBuilder.ps1:540)

And a manifest has no maximum version field, so a floor cannot exclude a bad interim release that sits above it. Raising the floor skips the bad ones below the new value, and nothing more.

Happy to open the issue with these measurements, and to send the bump as its own PR so this one stays scoped to the encoding fix. Which target would you prefer, latest 2.41.2 or a specific known good version?

@SamErde

Copy link
Copy Markdown
Contributor

Eduard Arbona (@earbona23), that assessment is (obviously) 😀 correct. However, there are some potentially unexpected behaviors that should be known, depending on whether the user runs Install-Module or Install-PSResource, and which version of Install-PSResource is run.

The breakdown

Command and implementation Automatic dependency selection
Install-Module (PowerShellGet 2.x) Generally selects the newest matching dependency when it needs to install one. A suitable version already installed can satisfy the dependency.
Install-PSResource (PSResourceGet 1.2.0) Can select the lowest matching version. Technically, it keeps the last matching candidate after processing the repository response, so it does not reliably guarantee either the lowest or newest version.
Install-PSResource (PSResourceGet 1.3 prereleases) The rewritten resolver uses a latest-version lookup for dependencies that specify a minimum without an upper bound.

It's not necessarily a big problem, but can be annoying if users don't know to expect this.

@earbona23

Copy link
Copy Markdown
Author

Good point, thanks. I hadn't looked at it from the PSResourceGet side.

I checked the one case I could reproduce here. Against PSGallery, with PowerShell 7.6.6 on Linux, saving Microsoft.Entra.Authentication 1.3.0 into an empty folder:

  • Save-PSResource (PSResourceGet 1.2.0) pulled Microsoft.Graph.Authentication 2.41.2
  • Save-Module (PowerShellGet 2.2.5) pulled 2.41.2 as well

So on the Gallery today both land on latest. That fits your description, though. If 1.2.0 keeps whichever candidate comes last in the feed response, PSGallery just happens to order them in a way that works. A private feed (Artifacts, a NuGet v3 mirror) could order them differently and land someone on 2.25.0 exactly. That makes the floor matter more than I said above, and it's a good argument for raising it to a known good version rather than just dropping it.

I'll open a separate issue for the build pin and the floor, with both keys listed so they move together, and keep this PR scoped to the 5.1 encoding fix.

@earbona23

Copy link
Copy Markdown
Author

Checking in on this one, with no change on my side since 21 October.

Current state, for anyone with write access who picks this up:

  • Approved by Sam Erde (@SamErde).
  • CLA and GitOps/AdvancedSecurity pass.
  • The only red is OpenPublishing.Build, and it is not produced by this branch: it reports on the docs build for the repository, and the two failures above are from the Learn build service on commits that only touch test/ and the module source.
  • The 5.1 verification Sam Erde (@SamErde) asked for is in the thread: SourceFileEncoding.Tests.ps1 641/641 on Windows PowerShell 5.1 and 641/641 on 7.6.6, with Pester 5.7.1.

On the dependency question we closed out: the published manifest is a floor rather than an exact pin, and the Install-Module versus Install-PSResource difference Sam Erde (@SamErde) raised is real but sits outside this change. I am happy to open a separate issue for it if that is the preferred shape — I did not want to widen this PR to cover it.

If anything here needs rework rather than a merge, say so and I will take it. I would rather it be rejected with a reason than sit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🪲 Import-Module Microsoft.Entra.Applications / Microsoft.Entra..Beta.Applications throws an error 1.3.0

2 participants