Interaction with powershell in wf4 beta1 provides two activities: invokepowershell and invokepowershell <t>.
Invokepowershell: Call A powershell cmdlet that does not contain the returned value. Invokepowershell can be used to call simple lelets and scripts. We can also pass parameters and input objects to the cmdlet. After execution, this activity provides a set of error messages (if an error occurs ).
Invokepowershell <t>: This activity calls the powershell cmdlet and receives the returned results. This type of activity requires one more initializationaction than the non-generic version. Initializationaction is used to hide the execution result of the cmdlet to the workflow variable.
The following example shows how to use these two activities. In the following example, we complete the following functions, enter a process name, and then call powershell to stop the process, after obtaining and displaying the latest processes, we will first complete several custom activities:
1. The active Readline used to receive input is as follows:
Namespace Carypowershell { Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Activities; Public class Readline : Nativeactivity { Public Outargument < String > Result { Get ; Set ;} Public Inargument < String > 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}: the bookmarks cannot be blank" , 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. Activities used to output process informationPrintresult, as follows:
Namespace Carypowershell { Using System; Using System. Collections. Generic; Using System. LINQ;Using System. text; Using System. Activities; Public class Printresult <T>: codeactivity <t> { Public Inargument < Icollection <T> collection { Get ; Set ;} Protected override void Execute (codeactivitycontext context ){ Icollection <T> underlyingcollection = This . Collection. Get < Icollection <T> (context ); If (Underlyingcollection. Count = 0 ){ Console . Writeline ( "No process" );} Else { Foreach (T OBJ In Underlyingcollection ){ Console . Writeline (obj. tostring ());}}}}}
3. Get the getprocesses activity of the process, using the generic version of invokepowershell <t>, such:
4. Stop the process activity stopprocess. The invokepowershell activity is used, for example:
5. The workflow we designed is as follows:
6. HostProgramAs follows:
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 ( "Workflow execution ended" ); System. Console . Readkey ();}}}
7. The running result is as follows:
8. RelatedArticle:
Wf4.0 beta1 Tour (1): Basic Introduction
Wf4.0 beta1 Tour (2): Exception Handling
Wf4.0 beta1 Tour (3): brand new flowchart
Wf4.0 beta1 Tour (4): Use of bookmark
Wf4.0 beta1 Tour (5): Rule Engine changes
Wf4.0 beta1 Tour (6): Custom Activity