Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Any Windows PowerShell module or cmdlet can be packaged as a Workflow activity by using the methods of the Microsoft.Powershell.Activities.Activitygenerator class. Use the Microsoft.Powershell.Activities.Activitygenerator.Generatefrommoduleinfo*, Microsoft.Powershell.Activities.Activitygenerator.Generatefromcommandinfo*, and Microsoft.Powershell.Activities.Activitygenerator.Generatefromname* methods of the Microsoft.Powershell.Activities.Activitygenerator class to generate C# code that represents an activity. You can then compile the resulting C# code into an assembly that can be added to a project as an activity.
You can then compile the resulting C# code into an assembly that can be added to a project as an activity by using a command line with the following form.
csc /nologo /out:AssemblyName /target:library /reference:System.Activities.Activity /reference:Microsoft.PowerShell.Activities codefile.cs
Example 1
The following example demonstrates how to generate C# code for an activity from a Windows PowerShell module.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management.Automation;
using System.Collections.ObjectModel;
using Microsoft.PowerShell.Activities;
namespace MakeActivity
{
class Program
{
static void Main(string[] args)
{
StreamWriter CodeFile = new StreamWriter("c:\\\\testmodule\\codefile.cs");
Collection<PSModuleInfo> ModuleInfos = new Collection<PSModuleInfo> { };
PSModuleInfo ModuleInfo;
string ActivityCode = "";
string ModulePath = "";
Console.Write("Type the full path and filename of the module to process:");
ModulePath = Console.ReadLine();
PowerShell ps = PowerShell.Create();
ps.AddCommand("Import-Module").AddArgument(ModulePath);
ps.AddParameter("PassThru");
ModuleInfos = ps.Invoke<PSModuleInfo>();
ModuleInfo = (PSModuleInfo)ModuleInfos[0];
ActivityCode = ActivityGenerator.GenerateFromModuleInfo(ModuleInfo, "MyNamespace").First<String>();
CodeFile.Write(ActivityCode);
Console.ReadLine();
}
}
}
Example 2
The following example demonstrates how to generate C# code for an activity from a Windows PowerShell cmdlet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management.Automation;
using System.Collections.ObjectModel;
using Microsoft.PowerShell.Activities;
namespace MakeActivity
{
class Program
{
static void Main(string[] args)
{
StreamWriter CodeFile = new StreamWriter("c:\\\\testmodule\\codefile.cs");
Collection<PSModuleInfo> ModuleInfos = new Collection<PSModuleInfo> { };
PSModuleInfo ModuleInfo;
Collection<CommandInfo> CommandInfos = new Collection<CommandInfo> { };
CommandInfo MyCommand;
string ActivityCode = "";
string ModulePath = "";
Console.Write("Type the full path and filename of the module to process:");
ModulePath = Console.ReadLine();
PowerShell ps = PowerShell.Create();
ps.AddCommand("Import-Module").AddArgument(ModulePath);
ps.AddParameter("PassThru");
ModuleInfos = ps.Invoke<PSModuleInfo>();
ModuleInfo = (PSModuleInfo)ModuleInfos[0];
ps.AddCommand("Get-Command").AddArgument(ModuleInfo);
CommandInfos = ps.Invoke<CommandInfo>();
MyCommand = CommandInfos[0];
ActivityCode = ActivityGenerator.GenerateFromCommandInfo(MyCommand, "MyNamespace");
CodeFile.Write(ActivityCode);
Console.ReadLine();
}
}
}