简体   繁体   English

Windows AWS EC2实例用户数据PowerShell不会为系统帐户级别加载Powershell模块

[英]Windows AWS EC2 Instance User Data PowerShell do not load Powershell Modules for System Account level

I have created a PowerShell script to run in the user data of an AWS Windows instance. 我创建了一个PowerShell脚本以在AWS Windows实例的用户数据中运行。 If I manually execute the script as Administrator, it runs successfully and does work. 如果我以管理员身份手动执行脚本,则该脚本将成功运行并且可以正常工作。

用户数据脚本

But when I send the script in user data (during instance creation) it throws an exception. 但是,当我在用户数据中发送脚本时(在实例创建过程中),它将引发异常。 This happens during the installation of the module from System Account 这是在通过系统帐户安装模块的过程中发生的

PowerShell Version on EC2 Windows 2012 R2 = v4.0 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

Work Around /Solution : This is more of PsGet Module issue, not EC2 issue 解决方法/解决方案 :这更多是PsGet模块问题,而不是EC2问题

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

At System Account level PsGet Module was setting Path InCorrectly. 在系统帐户级别,PsGet模块正在正确设置路径。 As of now I have Modified PsGet.ps1 to read my custom path $env:PSModulePath, and it works with no issues. 到目前为止,我已经修改了PsGet.ps1来读取我的自定义路径$ env:PSModulePath,它可以正常工作。

PsGet.Ps1 Line 1615 Comment the line #Add-PathToPSModulePath -PathToAdd:$Destination -PersistEnvironment:$PersistEnvironment -Global:$Global PsGet.Ps1行1615注释行#Add-PathToPSModulePath -PathToAdd:$ Destination -PersistEnvironment:$ PersistEnvironment -Global:$ Global

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

It works , at System Account level with no Issues 它可以在系统帐户级别上正常工作

Thanks Alex for help. 感谢Alex的帮助。

I would advice to use specific paths. 我建议使用特定的路径。 The userdata block executes under the windows system account and as such it doesn't have a profile directory nor a temp path. userdata块在Windows系统帐户下执行,因此它没有配置文件目录或临时路径。 For example [System.IO.Path]::GetTempFileName() fails when executing with this user. 例如,使用该用户执行[System.IO.Path]::GetTempFileName()失败。

This is the same when code-deploy packages execute. 执行代码部署程序包时,这是相同的。

If you have to work often with AWS-EC2 then it's best to try to replicate the environment as much as possible locally because an EC2 takes minimum 6 minutes to launch and that can be a major slow down in your troubleshooting efforts. 如果您必须经常使用AWS-EC2,那么最好尝试在本地尽可能多地复制环境,因为EC2至少需要启动6分钟才能启动,这可能会大大拖慢您的故障排除工作。 For this reason, I've decided to setup a vagrant box with the matching operating system and on that box I execute/test what I need. 出于这个原因,我决定使用匹配的操作系统设置一个无所事事的盒子,然后在该盒子上执行/测试所需的东西。 When things get tough and I'm suspicious of the System user's special characteristics, I use psexec to launch my processes. 当事情变得艰难并且我对系统用户的特殊性表示怀疑时,我使用psexec启动进程。 This is a good starting point How to: become the LOCAL SYSTEM account with PsExec 这是一个很好的起点如何:使用PsExec成为本地系统帐户

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM