Nintex

Nintex Connect
Metalogix
Metalogix
BlueStripe
BlueStripe

Training - Combined Knowledge

Training - TekDog Inc

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

  • RSS
  • Add To My MSN
  • Add To Windows Live
  • Add To My Yahoo
  • Add To Google

Statistics

  • Entries (119)
  • Comments (312)
Posted by Vadim Tabakman Wednesday, August 15, 2012 2:01:00 PM Categories: Nintex Nintex Workflow PowerShell

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

Copyright Vadim Tabakman

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Tuesday, March 12, 2013 8:45:46 PM Kevin

Just one more note on the change to the ExportWorkflow line I pasted in my previous comment. The ExportWorkflow method in 2007 only takes in two parameters, WorkflowName and ListName so the WorkflowType must be removed from the method call.

Kev

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Tuesday, March 12, 2013 8:44:11 PM Kevin

Have just used this once again after some modifications to it so it can be used for MOSS 2007! Again, it has saved me so much time.

A couple of changes....

Add the following two lines to the top of the script

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

As there is only one type of workflow (list) the Site and Reusable export lines can be removed, so now the try block looks like

try
{
$exportedworkflow = $page.ExportWorkflow($workflowname,$list);
switch($operation) etc etc...

Thats about it.

Thanks Vadim!

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Thursday, March 07, 2013 7:04:17 AM

Hi Dan,

not with this script, since it only takes one set of site url/credentials.  You'd need to tweak it to export from one site and import into another farm.

cheers,

Vadim

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Thursday, March 07, 2013 3:54:03 AM Dan

can this export and import into a different farm ?

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Thursday, August 16, 2012 4:00:56 PM

Ok.  Script updated, so that should fix the export errors :)

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Thursday, August 16, 2012 3:07:13 PM

Thanks Kevin,

It looks like even though there's an error, the workflow still gets exported.

Here is the error and I'll be looking into it when I can:

Exception calling "ExportWorkflow" with "3" argument(s): "Server was unable to process request. ---> Workflow not found.
"
At C:\Program Files\Nintex\Nintex Workflow 2010\FindAllWorkflows.ps1:44 char:49
+         $exportedworkflow = $page.ExportWorkflow <<<< ($workflowname,$list,'List');
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

 

re: Nintex Workflow - PowerShell Find All Workflows and Export or Republish Part 3

Thursday, August 16, 2012 2:53:04 PM Kevin

Wow Vadim, this is awesome. Thanks so much for posting this up, it is a huge timesaver for me and some of the guys I work with. Seriously amazing....

I'm getting an error and have output the error text. I'll send you an email with the errors, easier than posting it up here.

Thanks again.