PoshCode Logo PowerShell Code Repository

New-Activity by Adam Driscoll 5 weeks ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3164"></script>download | new post

Wraps a pre-existing PowerShell cmdlet in a binary Workflow Activity and outputs a DLL that can be used in Visual Studio.

  1. #
  2. # By Adam Driscoll
  3. # This is very much a test function to see if this was possible. There was no polishing done. Please visit
  4. # http://csharpening.net/?p=867 for more information.
  5. #
  6.  
  7. function global:Test-IsCommonParameter
  8. {
  9.     param
  10.     (  
  11.         $Parameter
  12.     )
  13.    
  14.     'WhatIf','Confirm','UseTransaction','Verbose','Debug','OutBuffer','OutVariable','WarningVariable','ErrorVariable','ErrorAction','WarningAction' -contains $Parameter.Name
  15. }
  16.  
  17. function global:New-Activity
  18. {
  19.     param(
  20.     [string]
  21.     $CommandName,
  22.     [String]
  23.     $OutputAssembly
  24.     )
  25.    
  26.     $Command = Get-Command -Name $CommandName
  27.    
  28.     $ParameterString = ""
  29.    
  30.     foreach($parameter in $Command.Parameters.Values )
  31.     {
  32.         if (Test-IsCommonParameter -Parameter $Parameter)
  33.         {
  34.             continue
  35.         }
  36.         $ParameterType = $parameter.ParameterType
  37.         if ($ParameterType -match "switch")
  38.         {
  39.             $ParameterType = "SwitchParameter"
  40.         }
  41.         if ($ParameterType -match "scriptblock")
  42.         {
  43.             $ParameterType = "ScriptBlock"
  44.         }
  45.        
  46.         $ParameterString += "
  47.            [ParameterSpecificCategory]
  48.            public InArgument<$ParameterType> $($Parameter.Name) {get;set;}
  49.            
  50.        "
  51.     }
  52.    
  53.     $MethodString = ""
  54.    
  55.     foreach($parameter in $command.Parameters.Values)
  56.     {
  57.         if (Test-IsCommonParameter -Parameter $Parameter)
  58.         {
  59.             continue
  60.         }
  61.         $ParameterName = $parameter.Name
  62.         $MethodString += "
  63.            if (this.$ParameterName.Expression != null)
  64.            {
  65.                shell2.AddParameter(`"$ParameterName`", this.$ParameterName.Get(context));
  66.            }
  67.        "
  68.     }
  69.    
  70.     $ClassName = $Command.Name.Replace("-", "")
  71.     $FullName = $command.ModuleName + "\" + $command.Name
  72.    
  73.     Add-Type -OutputType Library -OutputAssembly $OutputAssembly -ReferencedAssemblies "System.Activities","C:\windows\assembly\GAC_MSIL\Microsoft.PowerShell.Workflow.ServiceCore\3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Workflow.ServiceCore.dll"  -TypeDefinition "
  74.  
  75.   using System.Activities;
  76.   using System.Management.Automation;
  77.   using Microsoft.PowerShell.Activities;
  78.   using System;
  79.   namespace CustomActitivies
  80.   {
  81.    public sealed class $ClassName : PSRemotingActivity
  82.    {
  83.        public $ClassName()
  84.        {
  85.            base.DisplayName = `"$ClassName`";
  86.        }
  87.    
  88.    $ParameterString
  89.        
  90.        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
  91.        {
  92.            PowerShell shell = PowerShell.Create();
  93.            PowerShell shell2 = shell.AddCommand(this.PSCommandName);
  94.            $MethodString
  95.            ActivityImplementationContext context2 = new ActivityImplementationContext();
  96.            context2.PowerShellInstance = shell;
  97.            return context2;
  98.        }
  99.        
  100.        public override string PSCommandName
  101.        {
  102.            get
  103.            {
  104.                return @`"$FullName`";
  105.            }
  106.        }
  107.    }
  108.    }
  109.    "
  110. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me