[英]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.