4

I have a script that works perfectly to upload to an S3 bucket with Windows PowerShell, but it doesn't work with PowerShell Core. According to Amazon, most of the cmdlets that work in one should work in the other.

This is the command I'm using:

Write-S3Object -BucketName $bucketName -Folder $localDir -KeyPrefix $targetFolder -AccessKey $accessKey -SecretKey $secretKey -Recurse

Again, when I try to run the command directly PowerShell it works as expected, but in PowerShell Core I get this error:

Write-S3Object : The term 'Write-S3Object' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Write-S3Object -BucketName "cloud-storage-poc" -Folder "C:\Users\Admi ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Write-S3Object:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Kehaar
  • 119
  • 2
  • 8
  • 1
    Do any other AWS cmdlets work in your powershell core configuration? Try `Get-AWSPowerShellVersion` and let us know what returns. If it fails with the same error, the module may simply not be loaded correctly. – Anthony Neace Dec 06 '18 at 00:56
  • 1
    You may need to Install-Module and/or Import-Module wherever the Write-S3Object cmdlet is. On a working powershell prompt enter Get-Command Write-s3Object to see where it's housed. – No Refunds No Returns Dec 06 '18 at 02:42
  • Removing and reinstalling then re-importing it worked locally, thanks you two. Adding the import line fixed it on aws lambda too- I would have thought the #Requires line would be enough – Kehaar Dec 06 '18 at 12:19

1 Answers1

7

For PowerShell Core you will need to explicitly import the AWSPowerShell.NetCore module before your script or command runs. Due to the large number of cmdlets (over 5000 currently) in the module we cannot at present list the exported cmdlet names in the module manifest and are exploring other alternatives (such as in future creating per-service modules, but no ETA yet).

Assuming a 'clean' machine, then

Install-Module AWSPowerShell.NetCore
Import-Module AWSPowerShell.NetCore
Write-S3Object ...

Should then work for you. Of course if you already have the correct module installed then you can skip the first command. I set up my PowerShell profiles for both Windows and Core to always do an import.

Why this works on Windows, for some, is that up until mid last year we did list the exported cmdlets in the manifest. Right when we passed the 4000 cmdlets count in the module however, publishing to the PowerShell Gallery blocked due to a hidden limit and forced us to stop listing them. With the exported cmdlets listed in a manifest, PowerShell doesn't need an explicit import statement.

Steve Roberts
  • 714
  • 4
  • 8