简体   繁体   English

如何在userdata中重新启动多个PowerShell命令?

[英]How can I run multiple PowerShell commands with restarts in userdata?

I am looking to spin up a Domain Controller in AWS using Terraform. 我希望使用Terraform在AWS中启动域控制器。 I've tried passing all commands through userdata, but I just can't find a way to get the commands run after a reboot. 我已经尝试通过userdata传递所有命令,但我找不到在重启后运行命令的方法。

Here is my userdata: 这是我的用户数据:

    workflow Rename-And-Continue {
    Rename-Computer -NewName "HACKDC" -Force -Passthru
    Restart-Computer -Wait

    Install-WindowsFeature AD-Domain-Services, rsat-adds -IncludeAllSubFeature
    Install-ADDSForest -DomainName hackdc -SafeModeAdministratorPassword (ConvertTo-SecureString "SOMEPASSWORD" -AsPlainText -Force) -DomainMode Win2012R2 -DomainNetbiosName HACKDC -ForestMode Win2012R2 -Confirm:$false -Force
    Restart-Service NetLogon -EA 0
    Get-Service -Name ADWS; while($s.Status -ne "Running") {Start-Service ADWS; Start-Sleep 3};

}

$AtStartup = New-JobTrigger -AtStartup
Register-ScheduledJob -Name testWorkflow -Trigger $AtStartup -ScriptBlock {Import-Module PSWorkflow; Get-Job testWorkflow -State Suspended | Resume-Job};
Rename-And-Continue -AsJob -JobName testWorkflow

The main question here is what can be done to ensure that the workflow properly runs, so that I can set up the environment even if multiple restarts are required (yes I know the above isn't every step for properly configuring a DC, just a snippet) 这里的主要问题是如何确保工作流正常运行,以便即使需要多次重启也可以设置环境(是的我知道以上并不是正确配置DC的每一步,只是一个片段)

Your current script only waits for the computer to restart. 您当前的脚本只等待计算机重新启动。

-Wait <SwitchParameter>
        Indicates that this cmdlet suppresses the Windows PowerShell prompt and blocks the pipeline 
        until all of the computers have restarted. You can use this parameter in a script to restart
        computers and then continue to process when the restart is finished.

In order to continue running the script, you may want to use the following. 要继续运行脚本,您可能需要使用以下内容。

# This will restart the computer. Then delay 2 seconds. 
# Then wait for PowerShell to become available again. 
# It will also timeout after 300 seconds (5 mins).
Restart-Computer -Wait -For PowerShell -Timeout 300 -Delay 2

For more information just use the Get-Help cmdlet with the -Examples flag. 有关更多信息,请使用带有-Examples标志的Get-Help cmdlet。 The specific example would be the one below. 具体的例子如下。

PS C:\>Restart-Computer -ComputerName "Server01" -Wait -For PowerShell -Timeout 300 -Delay 2

    This command restarts the Server01 remote computer and then waits up to 5 minutes (300 seconds) 
    for Windows PowerShell to be available on the restarted computer before it continues.

暂无
暂无

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

相关问题 如何使宏依次运行一些PowerShell命令? - How can I make a macro to run a few PowerShell commands one after the other? 如何在用户数据中运行 Tmux? - How to run Tmux inside userdata? 如何获取 RDS 端点以在 Userdata 中使用 - How can I get the RDS endpoint for use in Userdata AWS lambda:如何在 lambda 中运行 aws cli 命令 - AWS lambda: how can I run aws cli commands in lambda Terraform:我如何告诉 Windows EC2 实例使用整个磁盘? (用户数据中的 PowerShell) - Terraform: how do I tell a Windows EC2 instance to use the entire disk? (PowerShell in userdata) 当可能有多个实例时,如何确保 crontab 命令在 AWS 上运行一次? - How do I ensure crontab commands run once on AWS when there may be multiple instances? 当出站流量权限被锁定时,为什么Boto命令不能在UserData中运行? - Why won't Boto commands run in UserData when outbound traffic permissions are locked down? 如果用户包含bash脚本,如何防止AWS cloudinit执行用户数据 - How can I prevent AWS cloudinit to execute the userdata if they contain a bash script 如何将 AWS lambda 环境变量传递给用户数据脚本(脚本将在 ec2 启动期间触发) - How can I pass AWS lambda environement variable into userdata script ( script will fire during ec2 spinup) 如何从本地计算机上的单个ssh命令在aws计算机上运行tmux命令? - How can I run tmux commands on an aws machine from a single ssh command on my local machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM