Partilhar via


Creating a Workflow Activity from a Windows PowerShell Cmdlet (Criar uma Atividade de Fluxo de Trabalho a partir de um Cmdlet do Windows PowerShell)

Qualquer módulo de Windows PowerShell ou cmdlet pode ser embalado como uma atividade de Workflow utilizando os métodos da classe Microsoft.Powershell.Activities.Activitygenerator. Utilize o Microsoft.Powershell.Activities.Activitygenerator.Generatefrommoduleinfo*, Microsoft.Powershell.Activities.Activitygenerator.Generatefromcommandinfo*, e Microsoft.Powershell.Activities.Activitygenerator.Generatefromname* métodos da classe Microsoft.Powershell.Activities.Activitygenerator para gerar código C# que representa uma atividade. Em seguida, pode compilar o código C# resultante numa montagem que pode ser adicionada a um projeto como uma atividade.

Em seguida, pode compilar o código C# resultante num conjunto que pode ser adicionado a um projeto como uma atividade utilizando uma linha de comando com o seguinte formulário.

csc /nologo /out:AssemblyName /target:library /reference:System.Activities.Activity /reference:Microsoft.PowerShell.Activities codefile.cs

Exemplo 1

O exemplo a seguir demonstra como gerar código C# para uma atividade a partir de um módulo Windows PowerShell.

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();

        }
    }
}

Exemplo 2

O exemplo a seguir demonstra como gerar código C# para uma atividade a partir de um cmdlet Windows PowerShell.

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();

        }
    }
}