Nintex Workflow – PowerShell Find All Workflows and Export Part 2

This is Part 2 from the previous post on PowerShell Find All Workflows.

We currently have the ability to find all our workflows and parse the information.  Next, what I would like to do, is to be able to export each of these workflow to file. 

This would be useful as a backup process.  Since it’s a PowerShell script, I’m sure it’s possible to schedule it to run once a week or something like that.

Changes to the PowerShell Script

The way to export a workflow is via a web service call to the site where the workflow lives.  The Nintex Workflow web service url looks like this :

http://[siteurl]/_vti_bin/NintexWorkflow/Workflow.asmx

You can find some more information about this web service in the Nintex Workflow 2010 SDK.

Since the web service lives in SharePoint, to call it, you’ll need to provide login/password credentials.  We’ll make these parameters you pass to the PowerShell script, so as not to hardcode them into it.

Here is what the script looks like now.

[IO.Directory]::SetCurrentDirectory((Convert-Path (Get-Location -PSProvider FileSystem)))

# build the credential object
$secpasswd = ConvertTo-SecureString $args[1] -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($args[0],$secpasswd);

# check if we are in the same location as the nwadmin.exe
if(Test-Path(“.\nwadmin.exe”))
{
  # find all the workflows and store them in a variable
  $foundworkflows = .\nwadmin -o FindWorkflows

  # read the contents of the NWAdmin -o FindWorkflows
  # $foundworkflows = get-content “C:\Program Files\Nintex\Nintex Workflow 2010\workflows.txt”

  foreach($line in $foundworkflows)
  {
    if($line.StartsWith(“Active at “))
    {
      # get the site url
      $site = $line.Replace(“Active at “,””);
    }
    if($line.StartsWith(“– “))
    {
      # get the list name
      $list = $line.Replace(“– “,””);
    }
    if($line.StartsWith(“—- “))
    {
      # get the workflow name
      $workflowname = $line.Replace(“—- “,””);

      $webserviceurl = $site + “/_vti_bin/NintexWorkflow/Workflow.asmx”

      # get web service proxy
      $page = New-WebServiceProxy -Uri $webserviceurl -Credential $credential;

      $page.Url = $webserviceurl;

      # ($page | Get-Member -Name ExportWorkflow).definition;

      # export workflow
      try
      {
        if($list -eq “Site Workflow”)
        {$exportedworkflow = $page.ExportWorkflow($workflowname,$list,’Site’);}
        elseif($list -eq “Reusable workflow template”)
        {$exportedworkflow = $page.ExportWorkflow($workflowname,$list,’Reusable’);}
        elseif($list -eq “Site collection reusable workflow template”)
        {$exportedworkflow = $page.ExportWorkflow($workflowname,$list,’GloballyReusable’);}
        else
        {$exportedworkflow = $page.ExportWorkflow($workflowname,$list,’List’);}
      }
      catch
      {
        echo $error;
        $exportedworkflow = “”;
      }

      $localfile = “{0}\{1}.nwf” -f [IO.Directory]::GetCurrentDirectory(),$workflowname;
      echo “Exporting workflow to – ” $localfile;
      $stream = [System.IO.StreamWriter] $localfile;
      $stream.WriteLine($exportedworkflow);
      $stream.close();

      # only do this for the first workflow
      # break;
    }
  }
}
else
{
  echo “NWAdmin doesn’t exist.  Change directory to where NWAdmin.exe lives.”;
}

What this script does, is finds each workflow from the NWAdmin command, then connects to the web service running on that site and makes a call to the ExportWorkflow web method.  The result is then written to a file in the current directory, using the workflow name and the .NWF extension.

How to run this PowerShell Script

Put the script file in to : c:\Program Files\Nintex\Nintex Workflow 2010

To run this script, open the SharePoint 2010 Management Shell. 

Change directories to c:\Program Files\Nintex\Nintex Workflow 2010

Run the script :

   .\AllWorkflowsV2.ps1 domain\username password

Conclusion

You should now be able to run this and export all your workflows to NWF files for backup.

Although each environment is different, a test on my environment with 130 workflows, took a little over a minute to export.

Downloads

Nintex Workflow 2010 : v2.3.5.0

Download the AllWorkflowsV2 PowerShell script

Leave a Reply

Your email address will not be published. Required fields are marked *