0

I am attempting to run a simple powershell script as an Automation document on my Windows Server 2019 instances. Note, I am using the Tools for Windows PowerShell (AWSPowerShell module) and they are installed by default on all Windows-based Amazon Machine Images (AMIs), which is what I am attempting to use, not the newer AWS.Tools or AWSPowerShell.NetCore which I think depends on a Powershell version that is not 5.1.

The failure details for the Automation Step is this: The specified module 'AWSPowerShell' was not loaded because no valid module file was found in any module directory. It does not make sense to me because I log into the EC2 instance directly and I check if the AWSPowerShell module is installed with Get-Module -ListAvailable and I also confirm the path for the module is in Environment Variables PSModulePath. I test the cmdlets in powershell directly on the instance and it all works, importing the module and running certain aws get cmdlets all work. Anyone know why? I notice that Runtime options are either Powershell Core 6.0 or Powershell 7. My instances are Powershell 5.1. Could this be the problem?

Here is my input for the automation document, you can see that at the very top I attempt to import the module just like I would on the instance itself locally so I can use, for example, the cmdlet Get-CWLLogGroup, which does work when running from a local session on the host.

$input = $env:InputPayload | ConvertFrom-Json

# Install-Module AWSPowerShell -Force
Import-Module AWSPowerShell

class Logger {
    #----------------------------------------------
    [string] hidden  $cwlGroup
    [string] hidden  $cwlStream
    [string] hidden  $sequenceToken
    #----------------------------------------------
    # Log Initialization
    #----------------------------------------------
    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
        }
    }

rest of code removed...........

1 Answers1

0

Got it working by doing the following:

  1. Installing PS 7 on Windows instance with iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
  2. Selecting Powershell 7 for execution runtime function
  3. And added these cmdlets into my script for using AWS.Tools instead of AWSPowerShell.
Install-Module -Name AWS.Tools.Installer -Force
Import-Module AWS.Tools.Installer
Install-AWSToolsModule AWS.Tools.CloudWatchLogs,AWS.Tools.SecretsManager -Force -CleanUp
Import-Module AWS.Tools.CloudWatchLogs
Import-Module AWS.Tools.SecretsManager