Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Setiap modul atau cmdlet Windows PowerShell dapat di kemas sebagai aktivitas Alur Kerja dengan menggunakan metode kelas Microsoft.Powershell.Activities.Activitygenerator. Gunakan Microsoft.Powershell.Activities.Activitygenerator.Generatefrommoduleinfo*, Microsoft.Powershell.Activities.Activitygenerator.Generatefromcommandinfo*, dan metode Microsoft.Powershell.Activities.Activitygenerator.Generatefromname* dari kelas Microsoft.Powershell.Activities.Activitygenerator untuk menghasilkan kode C# yang mewakili aktivitas. Anda kemudian dapat mengkompilasi kode C# yang dihasilkan ke dalam rakitan yang dapat ditambahkan ke proyek sebagai aktivitas.
Anda kemudian dapat mengkompilasi kode C# yang dihasilkan ke dalam rakitan yang dapat ditambahkan ke proyek sebagai aktivitas dengan menggunakan baris perintah dengan formulir berikut.
csc /nologo /out:AssemblyName /target:library /reference:System.Activities.Activity /reference:Microsoft.PowerShell.Activities codefile.cs
Contoh 1
Contoh berikut menunjukkan cara menghasilkan kode C# untuk aktivitas dari modul 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();
}
}
}
Contoh 2
Contoh berikut menunjukkan cara menghasilkan kode C# untuk aktivitas dari 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();
}
}
}