// 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 (AsJob.Expression != null) { targetCommand.AddParameter("AsJob", AsJob.Get(context)); } if (DcomAuthentication.Expression != null) { targetCommand.AddParameter("DcomAuthentication", DcomAuthentication.Get(context)); } if (WsmanAuthentication.Expression != null) { targetCommand.AddParameter("WsmanAuthentication", WsmanAuthentication.Get(context)); } if (Protocol.Expression != null) { targetCommand.AddParameter("Protocol", Protocol.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (Impersonation.Expression != null) { targetCommand.AddParameter("Impersonation", Impersonation.Get(context)); } if (ThrottleLimit.Expression != null) { targetCommand.AddParameter("ThrottleLimit", ThrottleLimit.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
/// <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 System.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); // Get the host default values we should use if no explicit parameter was provided. var hostExtension = context.GetExtension <HostParameterDefaults>(); Dictionary <string, object> parameterDefaults = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); if (hostExtension != null) { Dictionary <string, object> incomingArguments = hostExtension.Parameters; foreach (KeyValuePair <string, object> parameterDefault in incomingArguments) { parameterDefaults[parameterDefault.Key] = parameterDefault.Value; } } if (DcomAuthentication.Expression != null) { targetCommand.AddParameter("DcomAuthentication", DcomAuthentication.Get(context)); } if (Impersonation.Expression != null) { targetCommand.AddParameter("Impersonation", Impersonation.Get(context)); } // See if the DCOM protocol is to be used bool usingDcom = false; if (Protocol.Expression != null) { string protocol = Protocol.Get(context); targetCommand.AddParameter("Protocol", protocol); if (string.Equals(protocol, "DCOM", StringComparison.OrdinalIgnoreCase)) { usingDcom = true; } } // Get the WSMan authentication mechanism to use. If no expression was specified, // and DCOM is not being used, get the default from the host. if (WsmanAuthentication.Expression != null) { targetCommand.AddParameter("WsmanAuthentication", WsmanAuthentication.Get(context)); } else { if (!usingDcom) { if (parameterDefaults.ContainsKey("PSAuthentication")) { string authString = parameterDefaults["PSAuthentication"].ToString(); // Note: the underlying cmdlet does support NegotiateWithImplicitCredential so it is expected // that passing this in as-is will result in an (appropriate) error being emitted in that case. targetCommand.AddParameter("WsmanAuthentication", authString); } } } // Map PSComputerName to the underlying cmdlet name only if the computername is not empty string[] computerName = GetPSComputerName(context); if ((computerName != null) && (computerName.Length != 0)) { targetCommand.AddParameter("ComputerName", computerName); } // Map PSCredential to credential. If no expression was provided, then use the default. if (PSCredential.Expression != null) { targetCommand.AddParameter("Credential", PSCredential.Get(context)); } else { if (parameterDefaults.ContainsKey("PSCredential")) { targetCommand.AddParameter("Credential", parameterDefaults["PSCredential"]); } } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (ThrottleLimit.Expression != null) { targetCommand.AddParameter("ThrottleLimit", ThrottleLimit.Get(context)); } // Ignore the -Wait parameter in the self-restart case. if (!IsSelfRestart(context)) { if (Wait.Expression != null) { targetCommand.AddParameter("Wait", Wait.Get(context)); } if (For.Expression != null) { targetCommand.AddParameter("For", For.Get(context)); } if (Delay.Expression != null) { targetCommand.AddParameter("Delay", Delay.Get(context)); } } if (Timeout.Expression != null) { targetCommand.AddParameter("Timeout", Timeout.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
// 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 (AsJob.Expression != null) { targetCommand.AddParameter("AsJob", AsJob.Get(context)); } if (DcomAuthentication.Expression != null) { targetCommand.AddParameter("DcomAuthentication", DcomAuthentication.Get(context)); } if (WsmanAuthentication.Expression != null) { targetCommand.AddParameter("WsmanAuthentication", WsmanAuthentication.Get(context)); } if (Protocol.Expression != null) { targetCommand.AddParameter("Protocol", Protocol.Get(context)); } if (BufferSize.Expression != null) { targetCommand.AddParameter("BufferSize", BufferSize.Get(context)); } if (ComputerName.Expression != null) { targetCommand.AddParameter("ComputerName", ComputerName.Get(context)); } if (Count.Expression != null) { targetCommand.AddParameter("Count", Count.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (Source.Expression != null) { targetCommand.AddParameter("Source", Source.Get(context)); } if (Impersonation.Expression != null) { targetCommand.AddParameter("Impersonation", Impersonation.Get(context)); } if (ThrottleLimit.Expression != null) { targetCommand.AddParameter("ThrottleLimit", ThrottleLimit.Get(context)); } if (TimeToLive.Expression != null) { targetCommand.AddParameter("TimeToLive", TimeToLive.Get(context)); } if (Delay.Expression != null) { targetCommand.AddParameter("Delay", Delay.Get(context)); } if (Quiet.Expression != null) { targetCommand.AddParameter("Quiet", Quiet.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }