如何在用户数据重新启动时运行多个PowerShell命令?

app*_*guy 5 powershell active-directory amazon-web-services terraform

我希望使用Terraform在AWS中启动域控制器。我尝试过通过用户数据传递所有命令,但是我找不到重新启动后运行命令的方法。

这是我的用户数据:

    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
Run Code Online (Sandbox Code Playgroud)

这里的主要问题是如何确保工作流程正常运行,以便即使需要多次重新启动也可以设置环境(是的,我知道以上并不是正确配置DC的每个步骤,片段)

Pat*_*vay 0

您当前的脚本仅等待计算机重新启动。

-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.
Run Code Online (Sandbox Code Playgroud)

为了继续运行脚本,您可能需要使用以下命令。

# 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
Run Code Online (Sandbox Code Playgroud)

有关更多信息,只需使用带有 -Examples 标志的 Get-Help cmdlet。具体的例子如下。

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.
Run Code Online (Sandbox Code Playgroud)