简体   繁体   English

EC2 Windows 用户数据:Powershell 未按预期运行

[英]EC2 Windows User Data: Powershell does not run as expected

I am trying to bootstrap EC2 Windows instances via Powershell commands inside user data.我正在尝试通过用户数据中的 Powershell 命令引导 EC2 Windows 实例。 The steps I am trying to execute via user data are:我试图通过用户数据执行的步骤是:

  • Install chocolatey安装巧克力
  • Use chocolatey to install Python使用 Chocolatey 安装 Python
  • Use chocolatey to install AWS CLI使用 Chocolatey 安装 AWS CLI
  • Use AWS CLI to download a Powershell script from S3使用 AWS CLI 从 S3 下载 Powershell 脚本
  • Run that Powershell script运行该 Powershell 脚本

The user data is pretty straightforward:用户数据非常简单:

<powershell>
Set-ExecutionPolicy Bypass -Force;
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'));
choco install python3 -y;
choco install awscli -y

refreshenv

$BootstrapScript = (Join-Path $env:TEMP "NewBootstrap.ps1")
& aws s3api get-object --bucket my-bucket-name --key bootstrap/WindowsBootstrap.ps1 "$BootstrapScript"

iex "$BootstrapScript"
</powershell>

The bootstrap script is never downloaded or executed.引导脚本永远不会被下载或执行。 If I logon to the instance and view the logs, the output suggests something strange is happening:如果我登录到实例并查看日志,输出表明发生了一些奇怪的事情:

  • At the top of the logs there are errors complaining that 'aws' is not recognized as the name of a cmdlet .在日志的顶部有错误抱怨'aws' is not recognized as the name of a cmdlet Why should this error before it's even tried to install the aws cli?为什么在尝试安装 aws cli 之前会出现此错误?
  • After that the term 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\NewBootstrap.ps1' is not recognized as the name of a cmdlet, function, script file .之后, the term 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\NewBootstrap.ps1' is not recognized as the name of a cmdlet, function, script file Again - why is this erroring before we've tried to get that file?再次 - 为什么在我们尝试获取该文件之前会出现此错误?
  • Then the logs show that choco, python and awscli actually were installed correctly.然后,日志显示,巧克力,Python和awscli实际上正确安装。

I don't understand what is happening with the order of execution.我不明白执行顺序发生了什么。 If I login to the box and execute the exact same user data script that is contained in C:\\Windows\\Temp it runs completely as expected.如果我登录到该框并执行包含在C:\\Windows\\Temp的完全相同的用户数据脚本,它会完全按预期运行。

Any help on understanding or debugging this would be most appreciated.任何有关理解或调试的帮助将不胜感激。

Output from C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Log\\UserdataExecution.log with some choco output elided for brevityC:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Log\\UserdataExecution.log输出,为简洁起见省略了一些 choco 输出

2017/11/06 12:11:49Z: Userdata execution begins
2017/11/06 12:11:49Z: Zero or more than one <persist> tag was not provided
2017/11/06 12:11:49Z: Unregistering the persist scheduled task
2017/11/06 12:11:54Z: Zero or more than one <runAsLocalSystem> tag was not provided
2017/11/06 12:11:54Z: Zero or more than one <script> tag was not provided
2017/11/06 12:11:54Z: Zero or more than one <powershellArguments> tag was not provided
2017/11/06 12:11:54Z: <powershell> tag was provided.. running powershell content
2017/11/06 15:13:42Z: Userdata execution begins
2017/11/06 15:13:42Z: Zero or more than one <persist> tag was not provided
2017/11/06 15:13:42Z: Unregistering the persist scheduled task
2017/11/06 15:13:54Z: Zero or more than one <runAsLocalSystem> tag was not provided
2017/11/06 15:13:54Z: Zero or more than one <script> tag was not provided
2017/11/06 15:13:54Z: Zero or more than one <powershellArguments> tag was not provided
2017/11/06 15:13:55Z: <powershell> tag was provided.. running powershell content
2017/11/06 15:16:11Z: Userdata:  is currently executing. To end it kill the process with id: 2828
2017/11/06 15:17:40Z: Message: The errors from user scripts: & : The term 'aws' 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:\Windows\TEMP\UserScript.ps1:15 char:3
+ & aws s3api get-object --bucket my-bucket-name --key bootstra ...
+   ~~~
    + CategoryInfo          : ObjectNotFound: (aws:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\Users\Administrator\AppData\Local\Temp\NewBootstrap.ps1 : The term 
'C:\Users\Administrator\AppData\Local\Temp\NewBootstrap.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 line:1 char:1
+ C:\Users\Administrator\AppData\Local\Temp\NewBootstrap.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\Admini...ewBootstrap.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


2017/11/06 15:17:40Z: Message: The output from user scripts: This is the new bootstrap

Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        11/6/2017   3:14 PM                chocInstall                                                           
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.8.

... Chocolatey Install output ...

Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
Installing awscli
Chocolatey v0.10.8
Installing the following packages:
python3


 .. Python Download / Install output ...

Download of python-3.6.3-amd64.exe (30.16 MB) completed.
Hashes match.
Installing python3...
python3 has been installed.
Installed to 'C:\Python36'
  python3 can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of python3 was successful.
  Software installed as 'EXE', install location is likely default.

Chocolatey installed 1/1 packages. 
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Chocolatey v0.10.8
Installing the following packages:
awscli

 .. AWS CLI Download / Install output ...

 The install of awscli was successful.
  Software installed as 'msi', install location is likely default.

Chocolatey installed 1/1 packages. 
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

2017/11/06 15:17:40Z: Userdata execution done

The issue was that when installed as part of the cloud init process, the Powershell profile fails to import Chocolatey.问题是作为云初始化过程的一部分安装时,Powershell 配置文件无法导入 Chocolatey。 This means that packages will install fine via choco install , but are not made available in the environment, even if you call refreshenv (hence my call to aws failed even though it installed successfully.)这意味着软件包将通过choco install安装得很好,但在环境中不可用,即使您调用refreshenv (因此,即使安装成功,我对aws调用也失败了。)

To fix this problem you can manually force Powershell to import the Chocolatey module by editing your Powershell profile.要解决此问题,您可以通过编辑 Powershell 配置文件手动强制 Powershell 导入 Chocolatey 模块。

# Updated profile content to explicitly import Choco
$ChocoProfileValue = @'
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}
'@

# Write it to the $profile location
Set-Content -Path "$profile" -Value $ChocoProfileValue -Force

# Source it
. $profile

You can read more on this in the chocolatey troubleshooting guide您可以在巧克力故障排除指南中阅读更多相关信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将PowerShell脚本发送到用户数据中的Windows ec2 - sending powershell script to Windows ec2 in user data Windows AWS EC2实例用户数据PowerShell不会为系统帐户级别加载Powershell模块 - Windows AWS EC2 Instance User Data PowerShell do not load Powershell Modules for System Account level 在Windows上的EC2实例上记录用户数据脚本输出 - Logging user-data Script Output on EC2 Instances on windows AWS EC2 Windows 实例用户数据和任务计划程序 - AWS EC2 Windows Instance User Data and Task Scheduler Ec2在启动时将用户数据传递到Windows实例 - Ec2 Passing User Data to Windows Instance at Launch EC2 - 可以在EC2 Linux实例上运行Windows吗? - EC2 - possible to run Windows on an EC2 Linux instance? 如何在Windows EC2实例上运行Minikube? - How to run Minikube on Windows EC2 Instance? 使用 Z2F2D399F0EA88448339FE5514B304 为 Windows EC2 实例配置 static IP 地址 - Configuring static IP address for Windows EC2 instance using powershell 在 Terraform 中的远程 Windows EC2 实例上执行 Powershell 脚本 - Executing Powershell script on remote Windows EC2 instance in Terraform 在Windows ec2实例上运行批处理文件,然后在关机实例上运行? - Run batch file on windows ec2 instance then shutdown instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM