【问题标题】:Setting Windows PowerShell system path permanently when initialising an EC2 instance初始化 EC2 实例时永久设置 Windows PowerShell 系统路径
【发布时间】:2018-06-24 16:49:58
【问题描述】:

我的目标是run powershell commands on EC2 Windows Server 2016 instance at launch,以便永久设置多个系统路径。

<powershell>
######### SET SYSTEM PATHS #########
# We need to restart the compute in order for the system paths to take effect.
# Source: https://www.quora.com/Why-wont-Python-work-in-PowerShell-for-me

### aws ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + “;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages\awscli", "Machine")

### python ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Users\Administrator\AppData\Local\Programs\Python\Python36", "Machine")

### pip ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + “;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts", "Machine")
</powershell>

当我们手动运行脚本并确保重新启动 powershell 控制台时,代码运行良好。请注意,路径不一定需要存在才能设置。

但是,在通过 EC2 初始化实例时系统地运行代码时不起作用。添加Restart-Computer 以尝试重新启动我假设在幕后运行的powershell 控制台不起作用。

其他来源:

【问题讨论】:

    标签: python windows powershell amazon-ec2 environment-variables


    【解决方案1】:

    PowerShell 不是在 Windows 上运行的服务,但它确实具有对 WinRM 的服务依赖性和对 WMI 的组件依赖性。 PowerShell 是一组 .exe,您必须专门启动才能使用它。

    1. powershell_ise.exe 用于通过此 UI 编写脚本(仅限 Windows)
    2. powershell.exe(仅限 Windows)
    3. pwsh.exe(PowerShell 核心、Win、OSX、Linux)

    如果您尝试在启动时执行和 .ps1,请使用启动脚本,无论是否使用 GPO。那是使用 PowerShell 作业或 PowerShell 登录脚本。

    # Use PowerShell to Create Job that Runs at Startup
    # 
    # Beginning with Windows PowerShell 3.0, with Windows PowerShell scheduled jobs, it is possible to natively and easily create a Windows PowerShell startup script (and one that does not rely on Group Policy).
    # 
    # A few steps are required to create a Windows PowerShell script that runs at startup as a Windows PowerShell scheduled job:
    # 1. Open the Windows PowerShell console with admin rights.
    # 2. Create a new job trigger and specify the type as a startup trigger.
    # 3. Specify a short random interval for the startup trigger to prevent race conditions at startup.
    # 4. Create the new scheduled job and specify the job trigger and the full path to the startup script.
    # 
    # 
    # https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/14/use-powershell-to-create-job-that-runs-at-startup
    
    # Use Startup, Shutdown, Logon, and Logoff Scripts
    # You can use Windows PowerShell scripts, or author scripts in any other language supported by the client computer.
    # https://technet.microsoft.com/en-us/library/cc753404(v=ws.11).aspx
    

    【讨论】:

    • 我特指创建EC2实例时的powershell run。方法在第一个链接中说明。
    • 是的,我已经看到了,但是该文档必须与他们的其他指导的技术指导配对,docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/…。所以,你是说,你已经尝试了上面的方向,但它仍然不起作用?注释还显示用户数据脚本由在 SYSTEM 上下文中运行的 Ec2Config 服务执行,因此,这引出了一个问题,您使用的是什么上下文?完全披露,这是我很久以前在课堂上尝试过的。从未投入生产,PoSH DSC 是我的首选,或者根据这种需求预烘焙您的 AMI。
    【解决方案2】:

    powershell.exe .\foo.ps1

    确保您指定脚本的完整路径,并确保您已将执行策略级别设置为至少“RemoteSigned”,以便可以运行未签名的本地脚本。

    【讨论】:

    • 如果您在我的第一个超链接中查看“用户数据和控制台”部分,您会发现我只能提供一个 powershell 脚本。换句话说,没有要提供的脚本路径,并且我没有看到任何执行策略标志 che 创建 EC2 实例。我不想在初始化实例后手动运行任何 powershell 脚本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2012-02-18
    • 2021-08-24
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多