如何使用cmdlet生成的Windows工作流活动?

时间:2014-03-14 13:08:18

标签: powershell workflow cmdlet

我使用PowerShell 3.0从cmdlet生成了WWF活动。

 /// <summary>
    /// Activity to invoke the Microsoft.Ceres.HostController.Cmdlets\Connect-Host command in a Workflow.
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCode("Microsoft.PowerShell.Activities.ActivityGenerator.GenerateFromName", "3.0")]
    public sealed class ConnectHost : PSRemotingActivity
    {
        /// <summary>
        /// Gets the display name of the command invoked by this activity.
        /// </summary>
        public ConnectHost()
        {
            this.DisplayName = "Connect-Host";
        }

        /// <summary>
        /// Gets the fully qualified name of the command invoked by this activity.
        /// </summary>
        public override string PSCommandName
        {
            get { return "Microsoft.Ceres.HostController.Cmdlets\\Connect-Host"; }
        }

        // Arguments

        /// <summary>
        /// Provides access to the Host parameter.
        /// </summary>
        [ParameterSpecificCategory]
        [DefaultValue(null)]
        public InArgument<System.String> Host { get; set; }

        /// <summary>
        /// Provides access to the Port parameter.
        /// </summary>
        [ParameterSpecificCategory]
        [DefaultValue(null)]
        public InArgument<System.String> Port { get; set; }

        /// <summary>
        /// Provides access to the NoConnectionSecurity parameter.
        /// </summary>
        [ParameterSpecificCategory]
        [DefaultValue(null)]
        public InArgument<System.Management.Automation.SwitchParameter> NoConnectionSecurity { get; set; }

        /// <summary>
        /// Provides access to the ServiceIdentity parameter.
        /// </summary>
        [ParameterSpecificCategory]
        [DefaultValue(null)]
        public InArgument<System.String> ServiceIdentity { get; set; }


        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of Sytem.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (Host.Expression != null)
            {
                targetCommand.AddParameter("Host", Host.Get(context));
            }

            if (Port.Expression != null)
            {
                targetCommand.AddParameter("Port", Port.Get(context));
            }

            if (NoConnectionSecurity.Expression != null)
            {
                targetCommand.AddParameter("NoConnectionSecurity", NoConnectionSecurity.Get(context));
            }

            if (ServiceIdentity.Expression != null)
            {
                targetCommand.AddParameter("ServiceIdentity", ServiceIdentity.Get(context));
            }

            var cont = new ActivityImplementationContext() {PowerShellInstance = invoker};
            return cont;
        }
    }

创建activity1.xaml后,输入“Connect-Host”活动并编写开始代码:

var workflow1 = new Activity1();
    WorkflowInvoker.Invoke(workflow1);

但是我收到了消息的异常: “'Microsoft.PowerShell.Workflow.ActivityHostProcess'的类型初始化程序引发了异常。” “无法找到路径'C:\ Windows \ system32 \ windowspowershell \ v1.0 \ modules \ psworkflow \ PSWorkflow.types.ps1xml'的一部分。”

但是这个文件存在于我的系统中。

我如何使用这项技术?

1 个答案:

答案 0 :(得分:0)

这可能是Windows Powershell的一个问题。

解决方案是复制文件夹:&#34; PSWorkflow&#34;来自"C:\Windows\System32\WindowsPowerShell\v1.0\Modules\"

"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules"

然后一切正常。