博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WF4.0 Beta1之旅(7):WF调用PowerShell
阅读量:7218 次
发布时间:2019-06-29

本文共 4228 字,大约阅读时间需要 14 分钟。

在WF4 beta1中与PowerShell交互提供了两个活动InvokePowerShell和InvokePowerShell<T>活动

InvokePowerShell:调用一个不包含返回值的PowerShell cmdlet。InvokePowerShell可以用来调用简单的cmdlets和脚本。我们还可以向cmdlet传递参数和输入对象。执行之后,此活动会提供一组错误信息(如果发生错误的话)。

InvokePowerShell<T>:此活动调用PowerShell cmdlet,并接收返回的结果。这种类型的活动都要比非泛型版本多一个InitializationAction。InitializationAction用来将cmdlet的执行结果隐射到工作流的变量。

下面就举例说明这个两个活动如何使用,下面的例子中我们完成如下功能,输入一个进程名,然后调用powershell停止该进程,在获取最新的所有进程并显示出来,我们首先来完成几个自定义活动:

1.用于接收输入的活动ReadLine,如下:

namespace CaryPowerShell{    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    using System.Activities;    public class ReadLine : NativeActivity    {        public OutArgument
Result { get; set; } public InArgument
BookmarkName { get; set; } protected override void Execute(ActivityExecutionContext context) { string name = this.BookmarkName.Get(context); if (name == null) { throw new Exception(string.Format("ReadLine {0}: 书签不能为空",this.DisplayName)); } context.CreateNamedBookmark(name, new BookmarkCallback(OnReadComplete)); } void OnReadComplete(ActivityExecutionContext context, Bookmark bookmark, object state) { string input = state as string; context.SetValue(this.Result, input); } }}
2.用于输出进程信息的活动PrintResult,如下:
namespace CaryPowerShell{    using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    using System.Activities;    public class PrintResult
: CodeActivity
{ public InArgument
> Collection { get; set; } protected override void Execute(CodeActivityContext context) { ICollection
underlyingCollection = this.Collection.Get
>(context); if (underlyingCollection.Count == 0) { Console.WriteLine("没有进程"); } else { foreach (T obj in underlyingCollection) { Console.WriteLine(obj.ToString()); } } } }}
3.获取进程的活动GetProcesses,使用的是泛型版本的InvokePowerShell
,如下图:
 
4.停止进程的活动StopProcess,使用的是InvokePowerShell活动,如下图:
 
5.我们设计的工作流如下图:
 
6.宿主程序如下:
namespace CaryPowerShell{    using System;    using System.Linq;    using System.Threading;    using System.Activities;    using System.Activities.Statements;    class Program    {        static void Main(string[] args)        {            bool running = true;            AutoResetEvent syncEvent = new AutoResetEvent(false);            WorkflowInstance myInstance = new WorkflowInstance(new Sequence1());            myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e)            {                running = false;                syncEvent.Set();            };            myInstance.OnIdle = delegate()            {                syncEvent.Set();                return IdleAction.Nothing;            };            myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e)            {                Console.WriteLine(e.UnhandledException.ToString());                return UnhandledExceptionAction.Terminate;            };            myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)            {                Console.WriteLine(e.Reason);                syncEvent.Set();            };            myInstance.Run();            // main loop (manages bookmarks)            while (running)            {                if (!syncEvent.WaitOne(10, false))                {                    if (running)                    {                                             if (myInstance.GetAllBookmarks().Count > 0)                        {                            string bookmarkName = myInstance.GetAllBookmarks()[0].BookmarkName;                            myInstance.ResumeBookmark(bookmarkName, Console.ReadLine());                            syncEvent.WaitOne();                        }                    }                }            }            System.Console.WriteLine("工作流执行结束");            System.Console.ReadKey();        }    }}

7.运行结果如下:

8.相关文章:

本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/07/26/WF4Beta1-InvokePowerShell.html,如需转载请自行联系原作者

你可能感兴趣的文章
linux高编IO-------tmpnam和tmpfile临时文件
查看>>
微信的机器人开发
查看>>
从零开始学Java(二)基础概念——什么是"面向对象编程"?
查看>>
近期面试总结(2016.10)
查看>>
CodeForces 525D Arthur and Walls :只包含点和星的矩阵,需要将部分星变成点使满足点组成矩形 : dfs+思维...
查看>>
积累_前辈的推荐
查看>>
strcpy和memcpy的区别《转载》
查看>>
在windows平台下electron-builder实现前端程序的打包与自动更新
查看>>
DroidPilot V2.1 手写功能特别版
查看>>
COOKIE欺骗
查看>>
js 强转规范解读
查看>>
ACdream - 1735:输油管道
查看>>
golang 获取get参数
查看>>
服务器状态码
查看>>
非小型电子商务系统设计经验分享
查看>>
Video Target Tracking Based on Online Learning—深度学习在目标跟踪中的应用
查看>>
深度学习理论解释基础
查看>>
遗传算法
查看>>
将web网站移动化
查看>>
Application-Session-Cookie
查看>>