Share via


Creating a Workflow by Using a Windows PowerShell Script

You can create a workflow by writing a Windows PowerShell script. To create a workflow, use the workflow keyword followed by a name for the workflow before the body of the script. For example:


workflow Invoke-HelloWorld {"Hello World from workflow"}

You find the workflow in the same way you would any other Windows PowerShell command.

Implementing Parallel and Sequence

Windows Workflow Foundation supports execution of activities in parallel. To implement this capability in a Windows PowerShell script, use the parallel keyword in front of a script block. You can also use the foreach -parallel construction to iterate through a collection of objects in parallel. To execute a group of activities in sequential order within a parallel block, enclose that group of activities in a script block and precede the block with the sequence keyword.

Joining Computers to a Domain

The following script creates a workflow that checks the domain status of a group of user-specified computers, joins them to a domain if they are not already joined, and then checks the status again. This is a script version of the XAML workflow explained in Creating a Workflow with Windows PowerShell Activities.

workflow Join-Domain
{
    param([string[]] $ComputerName, [PSCredential] $DomainCred, [PsCredential] $MachineCred)

    foreach -parallel($Computer in $ComputerName)
    {
        sequence {
        Get-WmiObject -PSComputerName $Computer -PSCredential $MachineCred
        Add-Computer -PSComputerName $Computer -PSCredential $DomainCred
        Restart-Computer -ComputerName $Computer -Credential $MachineCred -For PowerShell -Force -Wait -PSComputerName ""
        Get-WmiObject -PSComputerName $Computer -PSCredential $MachineCred
        }
    }
}