繁体   English   中英

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

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

我创建了一个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”)

它可以在系统帐户级别上正常工作

感谢Alex的帮助。

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

执行代码部署程序包时,这是相同的。

如果您必须经常使用AWS-EC2,那么最好尝试在本地尽可能多地复制环境,因为EC2至少需要启动6分钟才能启动,这可能会大大拖慢您的故障排除工作。 出于这个原因,我决定使用匹配的操作系统设置一个无所事事的盒子,然后在该盒子上执行/测试所需的东西。 当事情变得艰难并且我对系统用户的特殊性表示怀疑时,我使用psexec启动进程。 这是一个很好的起点如何:使用PsExec成为本地系统帐户

暂无
暂无

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

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