Skip to content

Cannot import PSScriptAnalyzer 1.24.0 with PowerShell 7.4.5 or earlier on Linux #2106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
belcher-rok opened this issue Jun 3, 2025 · 2 comments

Comments

@belcher-rok
Copy link

belcher-rok commented Jun 3, 2025

Cannot import PSScriptAnalyzer 1.24.0 with PowerShell 7.4.5 or earlier on Linux*. Attempting to do so fails with the following error:

Import-Module: Could not load file or assembly 'System.Management.Automation, Version=7.4.6.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

* Note: I did not attempt to reproduce this on Windows, so it is possible that the problem also exists there.

  • PSScriptAnalyzer 1.23.0 works as expected.
  • PowerShell 7.4.6+ works as expected.
    • Reproduced with 7.4.1, 7.4.2, 7.4.5.
    • Did not try with earlier versions.
  • Did not try on Windows, although it does work okay with PS 7.5.1.

Steps to reproduce

Easiest way to repro is probably via a linux container with PowerShell installed:

$cmds = @(
  'Install-Module PSScriptAnalyzer -RequiredVersion 1.24.0 -Force'
  'Import-Module PSScriptAnalyzer -PassThru'
  '$Error[0] | select *'
) -join '; '
docker run -it --rm demisto/powershell-ubuntu:7.4.5.112371 pwsh -c $cmds

In this example I am using demisto/powershell-ubuntu:7.4.5.112371 but I was also able to repeat using an older .NET SDK container (mcr.microsoft.com/dotnet/sdk:8.0.302-bookworm-slim-arm64v8) containing PowerShell v7.4.2.

Expected behavior

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.24.0                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAnalyzer}

Actual behavior

Import-Module: Could not load file or assembly 'System.Management.Automation, Version=7.4.6.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
specified.

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.24.0                PSScriptAnalyzer

PSMessageDetails      :
Exception             : System.IO.FileNotFoundException: Could not load file or assembly 'System.Management.Automation, Version=7.4.6.500, Culture=neutral,
                        PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

                        File name: 'System.Management.Automation, Version=7.4.6.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
                           at System.Reflection.RuntimeAssembly.GetExportedTypes()
                           at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
                           at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo
                        psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers)
                           at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo,
                        PSModuleInfo moduleInfo, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers, String& helpFile)
                           at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)
                           at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly
                        assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix,
                        Boolean& found, String shortModuleName, Boolean disableFormatUpdates)
                           at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix,
                        SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean&
                        moduleFileFound)
                           at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)
                           at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String
                        name)
                           at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, /root/.local/share/powershell/Modules/PSScriptAnalyzer/1.24.0/PSScriptAnalyzer.psm1: line 34
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.4.5
PSEdition                      Core
GitCommitId                    7.4.5
OS                             Ubuntu 22.04.5 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
InvalidOperation: You cannot call a method on a null-valued expression.
@bergmeister
Copy link
Collaborator

Minimum PowerShell 7 version is 7.4.7 via the SMA reference here:

<PackageVersion Include="System.Management.Automation" Version="7.4.7" />

PowerShell support policy is to update to latest patch version within one month of release of a new patch, therefore no need to support versions like 7.4.5
Therefore suggest to update to latest patch 7.4.10
What we could've done better is to update the version check here so that you'd have gotten a better error message
https://github.com/PowerShell/PSScriptAnalyzer/blob/4b4a136d3b669a1fc127f182e7360160e4919acb/Engine/PSScriptAnalyzer.psm1#L12C12-L12C41

@belcher-rok
Copy link
Author

belcher-rok commented Jun 5, 2025

Therefore suggest to update to latest patch 7.4.10
What we could've done better is to update the version check here so that you'd have gotten a better error message
https://github.com/PowerShell/PSScriptAnalyzer/blob/4b4a136d3b669a1fc127f182e7360160e4919acb/Engine/PSScriptAnalyzer.psm1#L12C12-L12C41

Thanks @bergmeister, that will help explain the issue and is a valid suggestion, but sometimes getting an updated version isn't that easy. In my case, I'm using self-hosted runners managed by a separate group in my company. For whatever reason, they are still using 7.4.1. It sucks that a patched PS version breaks PSSA compatibility.

In my case, I pinned the PSSA version to 1.23, but that is not ideal since I know I am unlikely to remember to unpin it when the runners are updated to PS 7.4.7+ (whenever that is planned).

MSLearn documentation also needs to be updated:
Image

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

No branches or pull requests

2 participants