You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Systems Manager自动化执行PowerShell脚本时AWSPowerShell模块加载失败

问题分析与解决方案

核心原因

你的怀疑完全正确,问题出在AWS Systems Manager自动化文档的运行环境和实例预装模块不兼容:

  • 实例Windows AMI预装的AWSPowerShell模块是专门针对Windows PowerShell 5.1(桌面版)开发的,只能在PS5.1环境里加载;
  • 但SSM自动化文档的PowerShell运行选项默认用的是PowerShell Core(6.0/7.x),这是跨平台版本,识别不了专为PS5.1编译的AWSPowerShell模块,所以才会报“找不到有效模块文件”的错误。

解决办法

两种方案任选其一:

方案1:强制SSM用Windows PowerShell 5.1执行脚本

在SSM自动化文档的步骤配置里,把运行环境指定为WindowsPowerShell(不是默认的PowerShell),这样脚本就会在实例的PS5.1环境里跑,就能正常加载预装的AWSPowerShell模块。

自动化文档步骤配置示例:

mainSteps:
- name: RunPowerShellScript
  action: 'aws:runPowerShellScript'
  inputs:
    runtime: WindowsPowerShell
    runCommand:
      - |
        $input = $env:InputPayload | ConvertFrom-Json
        Import-Module AWSPowerShell
        # 你的后续脚本逻辑...

方案2:在PowerShell Core环境安装兼容模块

如果必须用PowerShell Core运行脚本,就在脚本开头安装针对PowerShell Core的AWS模块AWSPowerShell.NetCore,替换原来的AWSPowerShell:

修改后的脚本片段:

$input = $env:InputPayload | ConvertFrom-Json

# 安装兼容PowerShell Core的AWS模块
Install-Module AWSPowerShell.NetCore -Force -AllowClobber -Scope CurrentUser
Import-Module AWSPowerShell.NetCore

class Logger {
    [string] hidden  $cwlGroup
    [string] hidden  $cwlStream
    [string] hidden  $sequenceToken

    Logger([string] $Action, [string] $HostName) {
        $this.cwlGroup = "/ps/boot/configuration/"
        $this.cwlStream = "{0}/{1}/{2}" -f $HostName, $Action,
        (Get-Date -UFormat "%Y-%m-%d_%H.%M.%S")
        $this.sequenceToken = ""

        if ( !(Get-CWLLogGroup -LogGroupNamePrefix $this.cwlGroup) ) {
            New-CWLLogGroup -LogGroupName $this.cwlGroup
            Write-CWLRetentionPolicy -LogGroupName $this.cwlGroup -RetentionInDays 3
        }
        if ( !(Get-CWLLogStream -LogGroupName $this.cwlGroup -LogStreamNamePrefix $this.cwlStream) ) {
            New-CWLLogStream -LogGroupName $this.cwlGroup -LogStreamName $this.cwlStream
        }
    }

# 剩余代码...

注意:首次运行要确保实例有互联网权限访问PowerShell Gallery,不然安装会失败。

验证方式

  • 方案1:执行自动化后,查看步骤日志,确认脚本是在Windows PowerShell 5.1环境运行,模块导入成功;
  • 方案2:脚本执行后,用Get-Module AWSPowerShell.NetCore命令确认模块已加载,后续AWS cmdlet能正常执行。

内容的提问来源于stack exchange,提问作者humbleStrength

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:37:45