[英]How can I run a powershell script on a target VM using Terraform's aws_ssm_document
[英]AWS SSM RunCommand - Issue with RunRemoteScript Document to run PowerShell script with parameters
在 AWS SSM 中,我使用 RunRemoteScript 文檔運行 PowerShell 腳本以在 SSM 托管實例上安裝一些軟件。 該腳本托管在可公開訪問的 S3 存儲桶中。
RunCommand 與不帶任何參數的腳本一起正常工作。 軟件已成功部署到托管實例。 但是我的腳本在代碼中嵌入了一個唯一的 CID。 出於安全考慮,我需要將其取出並設置為 PS 腳本的參數。 從那時起,RunCommand 就一直在失敗。
我的腳本如下所示(帶有參數 CID):
param (
[Parameter(Position = 0, Mandatory = 1)]
[string]$CID
)
Start-Transcript -Path "$([System.Environment]::GetEnvironmentVariable('TEMP','Machine'))\app_install.log" -Append
function Install-App {
<#
Installs App
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Position = 0, Mandatory = 1)]
[string]$msiURL,
[Parameter(Position = 2, Mandatory = 1)]
[string]$InstallCheck,
[Parameter(Position = 3, Mandatory = 1)]
[string]$CustomerID
)
if ( -not(Test-Path $installCheck)) {
# Do stuff
...
}
else {
Write-Host ("$installCheck - Already Installed")
Return "Already Installed, Skipped $(($msiURL -split '([^\\/]+$)')[1])"
}
}
Install-App -msiURL "https://s3.amazonaws.com/app.foo.com/Windows/app.exe" -InstallCheck "C:\Program Files\App\app.exe" -CustomerID $CID
Stop-Transcript
按照下面的 AWS SSM 文檔,我運行下面的命令來啟動 RunCommand。 https://docs.aws.amazon.com/systems-manager/latest/userguide/integration-remote-scripts.html
aws ssm send-command --document-name "AWS-RunRemoteScript" --targets "Key=instanceids,Values=mi-abc12345"
--parameters '{"sourceType":["S3"],"sourceInfo":["{\"path\": "https://s3.amazonaws.com/app.foo.com/Windows/app_install.ps1\"}"],"commandLine":["app_install.ps1 abcd123456"]}'
RunCommand 不斷失敗,並出現以下錯誤:
----------ERROR-------
app_install.ps1 : The term 'app_install.ps1' 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 C:\ProgramData\Amazon\SSM\InstanceData\mi-abcd1234\document\orchest
ration\a6811111d-c411-411-a222-bad123456\runPowerShellScript\_script.ps1:4
char:2
+ app_install.ps1 abcd123456
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (app_install.ps1:String)
[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
failed to run commands: exit status 255
我懷疑這與 RunCommand 如何處理 PowerShell 腳本的參數的方式有關。 但是除了我遵循的官方文檔之外,我找不到任何示例。 任何人都可以指出這里的問題是什么?
順便說一句,我已經嘗試將 ps1 放在“。\”之后,但沒有運氣。
我找到了問題的原因。 附加到實例的 IAM 角色沒有足夠的權限來訪問持有腳本的 S3 存儲桶。 因此,SSM 無法將腳本下載到實例,因此出現錯誤“...ps1 無法識別”。
所以它實際上與代碼無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.