Search code examples
powershellworkflow

PowerShell workflow failed when Install-ADServiceAccount


I have a workflow script that runs on local server and will do Install-ADServiceAccount after reboot. Every function works fine except the Install-ADServiceAccount. The workflow failed when running Install-ADServiceAccount.

Here is my current version of code, which will run Install-ADServiceAccount after reboot local server:

workflow auto_install(){
    Param (
        $domain_account,
        $gmsa_account,
        $gmsa_account_no_domain,
        $log
    )
    FunctionA
    FunctionB
    FunctionC

    #reboot
    Restart-Computer -Wait
    Suspend-Workflow

    FunctionD
    FunctionE

    Install-ADServiceAccount $gmsa_account 
}

I tried to Import-Module ActiveDirectory in scheduled job, but the job still failed with no reason. Scheduled Job as follow:

$AtStartup = New-JobTrigger -AtStartup -RandomDelay 00:02:00
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
$block = {[System.Management.Automation.Remoting.PSSessionConfigurationData]::IsServerManager = $true; Import-Module PSWorkflow; Import-Module ActiveDirectory; Resume-Job -State Suspended | Wait-Job}

Register-ScheduledJob -Name ResumeWorkflow -Trigger $AtStartup -ScheduledJobOption $options -ScriptBlock $block
auto_install $gmsa_account -AsJob -JobName auto_reboot 

From the JobStateInfo I can't get any fail reason and error, too:

PS C:\Windows\system32> (get-job -Name auto_reboot).JobStateInfo | Format-List -Property *
State : Failed
Reason :

PS C:\Windows\system32> (get-job -Name auto_reboot).JobStateInfo.Reason


Solution

  • Case solved with creating scheduled task in WorkFlow and trigger the task after restart to run Install-ADServiceAccount. As far as I know, the user credential (or AD cred.) in WorkFlow might have changed after restart.