【问题标题】:Windows AWS EC2 Instance User Data PowerShell do not load Powershell Modules for System Account levelWindows AWS EC2 实例用户数据 PowerShell 不加载系统账户级别的 Powershell 模块
【发布时间】:2017-04-10 19:06:43
【问题描述】:

我创建了一个 PowerShell 脚本以在 AWS Windows 实例的用户数据中运行。如果我以管理员身份手动执行脚本,它会成功运行并且确实有效。

但是当我在用户数据中发送脚本时(在实例创建期间),它会引发异常。这发生在从系统帐户安装模块期间

EC2 Windows 2012 R2 上的 PowerShell 版本 = v4.0

 Cannot bind argument to parameter 'Path' because it is an empty string.
 

2017-04-10T18:24:25.004Z: Ec2HandleUserData: Message: Executing C:Windowssystem32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy unrestricted . 'C:\Program Files\Amazon\Ec2ConfigService\Scripts\UserScript.ps1' from System account
2017-04-10T18:24:25.006Z: Ec2HandleUserData: Message: Executing User Data with PID: 1084
2017-04-10T18:24:34.639Z: Ec2HandleUserData: Message: ExitCode of User Data with PID: 1084 is 1
2017-04-10T18:24:34.642Z: Ec2HandleUserData: Message: The errors from user scripts: iex : Cannot bind argument to parameter 'Path' because it is an empty string.
At C: Program Files Amazon Ec2ConfigService Scripts UserScript.ps1:5 char:79
+  (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") 
| ie ...
+                                                                              
 ~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterB 
   indingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl 
   lowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
 

2017-04-10T18:24:34.642Z: Ec2HandleUserData: Message: The output from user scripts: 
2017-04-10T18:24:34.642Z: Background plugin complete: Ec2HandleUserData

解决/解决方案:这更多是 PsGet 模块问题,而不是 EC2 问题

https://github.com/psget/psget/issues/208

在系统帐户级别 PsGet 模块设置路径不正确。 截至目前,我已修改 PsGet.ps1 以读取我的自定义路径 $env:PSModulePath,并且它可以正常工作。

PsGet.Ps1 第 1615 行 注释 #Add-PathToPSModulePath -PathToAdd:$Destination -PersistEnvironment:$PersistEnvironment -Global:$Global

添加:[Environment]::SetEnvironmentVariable("PSModulePath", ";C:{DirectoryIwantModulesToBe}\Modules", "Machine")

它在系统帐户级别有效,没有问题

感谢亚历克斯的帮助。

【问题讨论】:

标签: windows powershell amazon-ec2


【解决方案1】:

我建议使用特定路径。 userdata 块在 Windows 系统帐户下执行,因此它没有配置文件目录,也没有临时路径。 例如,[System.IO.Path]::GetTempFileName() 在使用此用户执行时失败。

代码部署包执行时也是如此。

如果您必须经常使用 AWS-EC2,那么最好尝试在本地尽可能多地复制环境,因为 EC2 至少需要 6 分钟才能启动,这可能会大大减慢您的故障排除工作。出于这个原因,我决定用匹配的操作系统设置一个流浪盒,并在那个盒子上执行/测试我需要的东西。当事情变得艰难并且我怀疑系统用户的特殊特征时,我使用 psexec 来启动我的进程。这是一个很好的起点How to: become the LOCAL SYSTEM account with PsExec

【讨论】:

  • 感谢 Alex,这有助于调试问题。我确实使用 aws.amazon.com/ec2/run-command 在同一个实例上进行复制。
  • 您提到使用特定路径。尝试设置新模块路径和新目录 [Environment]::SetEnvironmentVariable("PSModulePath",";C:\Modules", "Machine") 仍然出现异常,当我尝试安装模块时
  • 我的意思是将文件下载到特定文件夹。 Invoke-WebRequest -Uri $uri -OutFile $outFilePath 其中$outFilePath 例如在C:` but not on the temp folder. Then execute the downloaded artifact on $outFilePath`上。
  • 另外,请注意$env:PSModulePath 在用户配置文件中包含一个条目。这似乎奇怪地截断为\WindowsPowerShell\Modules
  • 我没有尝试从用户数据安装 PowerShellGet。我实际上是使用 packer 构建 amis,并使用配置脚本安装在 Windows Server 2012 R2 上。在这个packer file 中注意& C:/Provision/ISHBootstrap/Source/Server/PackageManagement/Install-PackageManagement.ps1 行。它是在服务器上安装 PackageManagement 的。
猜你喜欢
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 2023-03-30
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多