Nintex Workflow – PowerShell Find All Workflows and Export or Republish Part 3

This is Part 3 from the previous post on PowerShell Find All Workflows and Export.

We now have the ability to find all our workflows, parse the information and export the workflows to file.  Next, what I would like to do, is have the abililty to republish each of these workflows.  This is a request I’ve seen a few times, although I haven’t really found a need for it myself.

Changes to the PowerShell Script

The way to export and publish 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.

Here is what the script looks like now.

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

$operation = $args[0]

# build the credential object
$secpasswd = ConvertTo-SecureString $args[2] -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($args[1],$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’);}

        switch($operation)
        {
          “RepublishAll”
          {
            # republish this workflow
            try
            {
              $publishresult = $page.PublishFromNWFXml($exportedworkflow,$list,$workflowname,$True);
            }
            catch
            {
              echo $error;
            }
            if($publishresult -eq “”)
            {
               echo “Publish Result is empty”;
            }
          }
          “ExportAll”
          {
            $localfile = “{0}\{1}.nwf” -f [IO.Directory]::GetCurrentDirectory(),$workflowname;
            echo “Exporting workflow to – ” $localfile;
            $stream = [System.IO.StreamWriter] $localfile;
            $stream.WriteLine($exportedworkflow);
            $stream.close();
          }
          default
          {
            echo “No operation”;
          }
        }

      }
      catch [system.exception]
      {
        # $error[0];
        $_.Exception.Message;
        $exportedworkflow = “”;
      }

      # 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 it 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 followed by a PublishFromNWFXml method call. 

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 to Export:

   .\AllWorkflowsV3.ps1 ExportAll domain\username password

Run the script to Republish:

   .\AllWorkflowsV3.ps1 RepublishAll domain\username password

Conclusion

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

Don’t be afraid to tweak this to add more features that you may need.

Update

Script now supports List workflows, Site workflows, Reusable Workflow Templates and Site Collection Resuable Workflow Templates.

Downloads

Nintex Workflow 2010 : v2.3.5.0

Download the AllWorkflowsV3 PowerShell script

Leave a Reply

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