GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add Optional PSUseConstrainedLanguageMode rule by joshcorr · Pull Request #2165 · PowerShell/PSScriptAnalyzer

Conversation

@joshcorr

@joshcorr Josh Corrick (joshcorr) commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

PR Summary

This PR adds a new rule PSUseConstrainedLanguageMode that identifies PowerShell patterns incompatible with Constrained Language Mode, helping developers ensure scripts work in restricted environments. This rule is a Warning, but is optional and not enabled by default.

New files:
Rules/UseConstrainedLanguageMode.cs - Implements 14 CLM restriction checks
Tests/Rules/UseConstrainedLanguageMode.tests.ps1 - Adds 46 comprehensive tests
docs/Rules/UseConstrainedLanguageMode.md - Provides complete user documentation

Modified files;

Rules/strings.resx - Adds 16 new diagnostic message strings

Features
Detects CLM Violations:

  • Add-Type usage (code compilation)
  • Disallowed COM objects (only allows Scripting.Dictionary, Scripting.FileSystemObject, VBScript.RegExp)
  • Disallowed .NET types (validates ~70 allowed types including primitives, collections, PowerShell types)
  • PowerShell classes (class keyword)
  • XAML/WPF usage
  • Invoke-Expression usage
  • Dot-sourcing patterns
  • Type constraints, expressions, and casts
  • Member invocations on disallowed types
  • Module manifest wildcards and .ps1 references

Signature Awareness:

  • Detects signature blocks (# SIG # Begin signature block)
  • Applies selective checking to signed scripts (dot-sourcing, parameter types, manifests only)
  • Applies full checking to unsigned scripts

Array Type Support:

  • Handles array notation correctly ([string[]], [int[][]])
  • Strips brackets and validates base types

Configuration:

  • IgnoreSignatures property (default: false) bypasses signature detection and enforces full CLM compliance for all scripts

Testing:
image

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Josh Corrick (joshcorr) added 11 commits March 7, 2026 14:30
@joshcorr
Add UseConstrainedLanguageMode rule and tests
67e239a
@joshcorr
Enhance CLM rule: restrict types, COM objects, add tests
e616116
@joshcorr
Enhance detection of disallowed types in CLM rule
790fe8c
@joshcorr
Detect and flag class definitions in Constrained Language
b4b566c
@joshcorr
Enhance CLM rule to check module manifests for wildcards/.ps1
f8ab608
@joshcorr
Differentiate CLM checks for signed vs unsigned scripts
1be14c8
@joshcorr
Improve dot-sourcing detection and expand CLM rule tests
a23e14f
@joshcorr
Add IgnoreSignatures option to CLM rule and improve type checks
1abc86a
@joshcorr
Add documentation for PSUseConstrainedLanguageMode rule
3526267
@joshcorr
Increase severity of UseConstrainedLanguageMode to Warning for option…
d87628d
…al rule
@joshcorr
Update test to expect 'Warning' severity instead of 'Info'
69b200f
@joshcorr
Josh Corrick (joshcorr) requested review from a team, Christoph Bergmeister (bergmeister), Mikey Lombardi (He/Him) (michaeltlombardi) and Sean Wheeler (sdwheeler) as code owners March 17, 2026 15:54
Copilot AI review requested due to automatic review settings March 17, 2026 15:54
Copilot started reviewing on behalf of Josh Corrick (joshcorr) March 17, 2026 15:55
View session
Copilot AI reviewed Mar 17, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Pull request overview

Adds a new optional ScriptAnalyzer rule (PSUseConstrainedLanguageMode) to detect PowerShell patterns that are incompatible with Constrained Language Mode (CLM), along with tests and documentation so users can opt into CLM-focused linting.

Changes:

  • Added UseConstrainedLanguageMode rule implementation to detect multiple CLM-incompatible patterns, with special handling for signature blocks.
  • Added a comprehensive Pester test suite for the new rule.
  • Added end-user documentation and new localized resource strings for diagnostics.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
Rules/UseConstrainedLanguageMode.cs Implements the new rule logic (CLM checks, signature-block detection, manifest checks).
Tests/Rules/UseConstrainedLanguageMode.tests.ps1 Adds tests covering the new rule’s detections and signature behavior.
docs/Rules/UseConstrainedLanguageMode.md Documents what the rule flags, signed vs unsigned behavior, and configuration.
Rules/Strings.resx Adds diagnostic strings used by the new rule (and updates file header/encoding).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseConstrainedLanguageMode.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConstrainedLanguageMode.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConstrainedLanguageMode.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Tests/Rules/UseConstrainedLanguageMode.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/UseConstrainedLanguageMode.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@liamjpeters

Liam Peters (liamjpeters) commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

👋 Josh Corrick (@joshcorr),

I wonder if UseConstrainedLanguageMode is the best name for this. The rule isn't checking if you're using/enabling constrained language mode, but rather that you're conforming to it's restrictions at design-time.

Perhaps something like ConformToConstrainedLanguageMode could be more on the money? One to consider.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Josh Corrick (joshcorr) added 4 commits March 17, 2026 10:35
@joshcorr
Making copilot suggested edits
17f1a24
@joshcorr
Detect and flag [PSCustomObject]@{} in CLM scripts
a4a4a93
@joshcorr
Add UseConstrainedLanguageMode rule to README.md
5336b8f
@joshcorr
Merge branch 'main' into addCLMRules
aedace7
@joshcorr

Josh Corrick (joshcorr) commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

Hi Liam Peters (@liamjpeters) thanks for the feedback. I named the rule to conform to the existing naming structure Use.. over some of the other leading verbs I found in the project (Avoid, Possible, etc). I do see this rule trying to check for implementation of language specific features (i.e. will this script use constrained language mode correctly), so the name doesn't feel that odd to me, but I see your point. Will wait to see if Andy or others have a strong opinion on the naming.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bergmeister
Christoph Bergmeister (bergmeister) reviewed Mar 18, 2026
View reviewed changes
Comment thread Rules/Strings.resx

@bergmeister Christoph Bergmeister (bergmeister) Mar 18, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

can you please tidy away the whitespace diff please? might be easiest to just copy paste the first 58 lines from main over this

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@joshcorr Josh Corrick (joshcorr) Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fixed in the latest commit.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bergmeister

Christoph Bergmeister (bergmeister) commented Mar 18, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Wow, what a great idea and implementation Josh Corrick (@joshcorr) Build seems to be currently broken, if you could fix that would be great (maybe it's simply due to that whitespace diff in the xml file, msbuild can be picky at times.
Please bear with us as it's a big change to review but very happy to have it :-)

joshcorr reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bergmeister
Christoph Bergmeister (bergmeister) requested a review from Copilot March 18, 2026 19:45
bergmeister
Christoph Bergmeister (bergmeister) reviewed Mar 18, 2026
View reviewed changes
Comment thread Rules/UseConstrainedLanguageMode.cs
}

// Only flag if COM object name was found AND it's not in the allowed list
if (!string.IsNullOrWhiteSpace(comObjectValue) && !AllowedComObjects.Contains(comObjectValue))

@bergmeister Christoph Bergmeister (bergmeister) Mar 18, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

minor but wouldn't it reader better as !comObjectValue.IsNullOrWhiteSpace() since comObjectValue is already typed as string? similar a bit below

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@joshcorr Josh Corrick (joshcorr) Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I looked into this and since the IsNullOrWhiteSpace() is a public static method on the string class, it can't be called from a string object., but rather from the class.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bergmeister reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@bergmeister Christoph Bergmeister (bergmeister) Mar 19, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

ah, it's been clearly some time ago that I actively wrote a lot of C#, would have thought hoped that .NET would've improved on those things but seems not

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Copilot AI reviewed Mar 18, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Pull request overview

Adds a new optional ScriptAnalyzer rule to warn about PowerShell constructs that are incompatible with Constrained Language Mode (CLM), along with accompanying tests and documentation.

Changes:

  • Introduces PSUseConstrainedLanguageMode rule implementation (signature-aware + CLM pattern checks).
  • Adds a comprehensive Pester test suite for the rule.
  • Adds rule documentation and localized diagnostic strings.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
Rules/UseConstrainedLanguageMode.cs Implements the new CLM compatibility rule (AST checks + signature-aware behavior + caching).
Tests/Rules/UseConstrainedLanguageMode.tests.ps1 Adds Pester coverage for the new rule, including signed/unsigned scenarios and performance-related validation.
docs/Rules/UseConstrainedLanguageMode.md Documents rule purpose, configuration, and remediation guidance.
docs/Rules/README.md Adds the new rule to the rules index table.
Rules/Strings.resx Adds localized strings for rule name/description and diagnostic messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Tests/Rules/UseConstrainedLanguageMode.tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/Rules/UseConstrainedLanguageMode.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/Strings.resx Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConstrainedLanguageMode.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConstrainedLanguageMode.cs
Comment on lines +726 to +727
// Find all assignment statements in this scope
var assignments = scope.FindAll(testAst => testAst is AssignmentStatementAst, true);
bergmeister
Christoph Bergmeister (bergmeister) reviewed Mar 18, 2026
View reviewed changes
Comment thread docs/Rules/UseConstrainedLanguageMode.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

@joshcorr
Update CLM rule: tighten types, docs, and add error string
cba70f3
@bergmeister

Christoph Bergmeister (bergmeister) commented Mar 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Josh Corrick (@joshcorr) We merged a few other PRs that now caused a merge conflict here, can you pull latest of main into your branch and resolve conflict please?
I also kicked off build, which is working now but one of your new tests is failing, please have a look at it as well please: https://github.com/PowerShell/PSScriptAnalyzer/actions/runs/23281623145/job/67789974715?pr=2165#step:7:93
image

Thanks :-)

joshcorr reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Josh Corrick (joshcorr) added 4 commits March 19, 2026 18:56
@joshcorr
Add platform checks to CLM tests for cross-platform support
02598ac
@joshcorr
Merge branch 'main' into addCLMRules
9c91f63
@joshcorr
Add missing end tag after merge conflict from main
650d050
@joshcorr
Add CLM rule ScriptsToProcess; Fix wildcard and dotsource bug
3d3afc8
andyleejordan
Andy Jordan (andyleejordan) approved these changes Mar 20, 2026
View reviewed changes

@andyleejordan Andy Jordan (andyleejordan) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks Josh Corrick (@joshcorr), hope the session goes well. I'll get this out!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

joshcorr reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@andyleejordan
Andy Jordan (andyleejordan) merged commit 6cfc260 into PowerShell:main Mar 20, 2026
4 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@joshcorr
Josh Corrick (joshcorr) deleted the addCLMRules branch March 21, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@bergmeister Christoph Bergmeister (bergmeister) bergmeister left review comments
Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@sdwheeler Sean Wheeler (sdwheeler) Awaiting requested review from sdwheeler
@michaeltlombardi Mikey Lombardi (He/Him) (michaeltlombardi) Awaiting requested review from michaeltlombardi michaeltlombardi is a code owner
+1 more reviewer
@andyleejordan Andy Jordan (andyleejordan) andyleejordan approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@joshcorr @liamjpeters @bergmeister @andyleejordan
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.