【问题标题】:How do I troubleshoot Powershell user data scripts on AWS EC2?如何对 AWS EC2 上的 Powershell 用户数据脚本进行故障排除?
【发布时间】:2016-04-26 12:13:30
【问题描述】:

从自定义 AMI 创建 ec2 实例时,我尝试从用户数据框运行 powershell 脚本。在创建 ami 之前,我已经在配置上启用了用户数据执行。

这是我放入用户数据的内容

<powershell>
c:\scripts\github-download.ps1 someuser somepassword
</powershell>

它调用的脚本如下所示。

Param($gituser, $gitpass)
C:\Users\Administrator\AppData\Local\GitHub\shell.ps1 
git clone https://"$gituser":"$gitpass"@github.com/somegitrepo |out-null

我不知道为什么这不起作用。我在这里做错了吗?非常感谢任何帮助。

【问题讨论】:

  • 没有足够的细节来真正解决这个问题。您如何将用户数据传递到实例中(控制台/CLI?它是 base 64 编码的吗?所有细节在这里都会有所帮助。)?实例是否配置为与 Internet 通信?您是否尝试在此脚本中将 ExecutionPolicy 设置为 RemoteSigned 以确保它可以执行其他脚本?您是否检查了C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt 的 ec2 配置日志以获取任何详细信息?您是否尝试过登录脚本本身?
  • 也许不要|out-null这个命令,这样你就可以看到发生了什么?你有一套工作的 git 二进制文件吗?
  • 您有机会尝试我的答案吗?
  • @RodrigoM 如果您没有从该 OP 中获得解决方案,我建议您将出色的答案移至一个新的规范问题,以解决 EC2 上的 powershell 用户数据问题。 OP 没有给我们足够的细节,这不是一件微不足道/无关的事情,我讨厌你的好答案永远不被接受。
  • 好建议!会感谢安东尼

标签: powershell amazon-web-services amazon-ec2


【解决方案1】:

不要使用&lt;powsershell&gt; 标记调用用户数据,而是使用&lt;script&gt; 标记调用PowerShell 本身。您可以获得对其调用的命令行控制,并且可以直接控制执行策略和其他命令行设置:

<script>
    PowerShell -ExecutionPolicy Bypass -NoProfile -File c:\scripts\github-download.ps1 -user USER -password PASSWORD
</script>

在您的脚本中,如下设置脚本的开头和结尾部分:

# Server script called from userdata in this format
# <script>
# PowerShell -ExecutionPolicy Bypass -NoProfile -File c:\scripts\github-download.ps1  -user USER -password PASSWORD
# </script>

param (
    [string]$user = $(throw "-user is required."),
    [string]$password = $(throw "-password is required."),
)
Start-Transcript -Path C:\userscriptlog.txt
Import-Module WebAdministration
if ([System.Diagnostics.EventLog]::SourceExists("Userdata") -eq $False) {
    New-Eventlog -Logname Application -Source 'Userdata' 
}
Write-Eventlog -Logname Application -Source 'Userdata' -EventId 1 -EntryType Information -Message 'Begining post-deployment configuration script'

-- YOUR MAIN SCRIPT HERE --

Write-Eventlog -Logname Application -Source 'Userdata' -EventId 1 -EntryType Information -Message 'Post-deployment configuration script complete'
Stop-Transcript

对于脚本中的错误处理,您需要为每个命令使用强大的异常处理和日志记录,再次简化故障排除和调试。此块只是获取当前实例 ID,但请注意内置的异常处理和日志记录:

# get instance-id
try { 
    $InstanceId = (Invoke-WebRequest http://169.254.169.254/latest/meta-data/instance-id).content
} catch { 
    $_.Exception.message | out-file c:\InstanceId_error.log 
    Write-Host "FATAL: InstanceId exception"
    Exit    
}

if (!$InstanceId) { 
    Write-Host "FATAL: InstanceId is null"
    Exit    
} else {
    $InstanceId | out-file C:\InstanceId.txt
    Write-Host "InstanceId: $InstanceId"    
}

对您需要实现的任何命令或 shell 调用尝试这种方法。

此用户数据脚本的 powershell 脚本“包装器”允许可选的命令行参数、生成执行记录并将事件记录到 Windows 事件日志中,以确认脚本的基本执行。

它将为任何基于 Powershell 的用户数据脚本提供灵活的框架,便于调试和测试。

【讨论】:

    【解决方案2】:

    | out-null 将 git clone 可能发生的任何错误静音,因此您不会知道出了什么问题,除非您将错误通过管道传递到其他地方或不使用 | out-null

    在您尝试使用用户数据自动执行任何操作之前,我会在没有 | out-null 的情况下手动在 EC2 实例上运行命令。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多