// 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 (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (EventName.Expression != null) { targetCommand.AddParameter("EventName", EventName.Get(context)); } if (SourceIdentifier.Expression != null) { targetCommand.AddParameter("SourceIdentifier", SourceIdentifier.Get(context)); } if (Action.Expression != null) { targetCommand.AddParameter("Action", Action.Get(context)); } if (MessageData.Expression != null) { targetCommand.AddParameter("MessageData", MessageData.Get(context)); } if (SupportEvent.Expression != null) { targetCommand.AddParameter("SupportEvent", SupportEvent.Get(context)); } if (Forward.Expression != null) { targetCommand.AddParameter("Forward", Forward.Get(context)); } if (MaxTriggerCount.Expression != null) { targetCommand.AddParameter("MaxTriggerCount", MaxTriggerCount.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 (LogName.Expression != null) { targetCommand.AddParameter("LogName", LogName.Get(context)); } if (Source.Expression != null) { targetCommand.AddParameter("Source", Source.Get(context)); } if (EntryType.Expression != null) { targetCommand.AddParameter("EntryType", EntryType.Get(context)); } if (Category.Expression != null) { targetCommand.AddParameter("Category", Category.Get(context)); } if (EventId.Expression != null) { targetCommand.AddParameter("EventId", EventId.Get(context)); } if (Message.Expression != null) { targetCommand.AddParameter("Message", Message.Get(context)); } if (RawData.Expression != null) { targetCommand.AddParameter("RawData", RawData.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.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 (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (NoWait.Expression != null) { targetCommand.AddParameter("NoWait", NoWait.Get(context)); } if (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (PassThru.Expression != null) { targetCommand.AddParameter("PassThru", PassThru.Get(context)); } if (ServiceDisplayName.Expression != null) { targetCommand.AddParameter("DisplayName", ServiceDisplayName.Get(context)); } if (Include.Expression != null) { targetCommand.AddParameter("Include", Include.Get(context)); } if (Exclude.Expression != null) { targetCommand.AddParameter("Exclude", Exclude.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 (DestinationPath.Expression != null) { targetCommand.AddParameter("DestinationPath", DestinationPath.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (Module.Expression != null) { targetCommand.AddParameter("Module", Module.Get(context)); } if (FullyQualifiedModule.Expression != null) { targetCommand.AddParameter("FullyQualifiedModule", FullyQualifiedModule.Get(context)); } if (UICulture.Expression != null) { targetCommand.AddParameter("UICulture", UICulture.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (UseDefaultCredentials.Expression != null) { targetCommand.AddParameter("UseDefaultCredentials", UseDefaultCredentials.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.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 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); // Initialize the arguments if (Path.Expression != null) { targetCommand.AddParameter("Path", Path.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (Filter.Expression != null) { targetCommand.AddParameter("Filter", Filter.Get(context)); } if (Include.Expression != null) { targetCommand.AddParameter("Include", Include.Get(context)); } if (Exclude.Expression != null) { targetCommand.AddParameter("Exclude", Exclude.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (Stream.Expression != null) { targetCommand.AddParameter("Stream", Stream.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 (Path.Expression != null) { targetCommand.AddParameter("Path", Path.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (Audit.Expression != null) { targetCommand.AddParameter("Audit", Audit.Get(context)); } if (AllCentralAccessPolicies.Expression != null) { targetCommand.AddParameter("AllCentralAccessPolicies", AllCentralAccessPolicies.Get(context)); } if (Filter.Expression != null) { targetCommand.AddParameter("Filter", Filter.Get(context)); } if (Include.Expression != null) { targetCommand.AddParameter("Include", Include.Get(context)); } if (Exclude.Expression != null) { targetCommand.AddParameter("Exclude", Exclude.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 (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (DependentServices.Expression != null) { targetCommand.AddParameter("DependentServices", DependentServices.Get(context)); } if (RequiredServices.Expression != null) { targetCommand.AddParameter("RequiredServices", RequiredServices.Get(context)); } if (ServiceDisplayName.Expression != null) { targetCommand.AddParameter("DisplayName", ServiceDisplayName.Get(context)); } if (Include.Expression != null) { targetCommand.AddParameter("Include", Include.Get(context)); } if (Exclude.Expression != null) { targetCommand.AddParameter("Exclude", Exclude.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.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 (Job.Expression != null) { targetCommand.AddParameter("Job", Job.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (InstanceId.Expression != null) { targetCommand.AddParameter("InstanceId", InstanceId.Get(context)); } if (JobId.Expression != null) { targetCommand.AddParameter("Id", JobId.Get(context)); } if (State.Expression != null) { targetCommand.AddParameter("State", State.Get(context)); } if (Command.Expression != null) { targetCommand.AddParameter("Command", Command.Get(context)); } if (Filter.Expression != null) { targetCommand.AddParameter("Filter", Filter.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
public override Collection <PSObject> Run(bool debug) { Collection <PSObject> result = null; runspace.Open(); for (int i = 0; i < cmdlets.Count; i++) { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { powershell.Runspace = runspace; powershell.AddCommand(cmdlets[i].name); if (cmdlets[i].parameters.Count > 0) { var paramDictionary = new Dictionary <string, object>(); foreach (CmdletParam cmdletparam in cmdlets[i].parameters) { paramDictionary.Add(cmdletparam.name, cmdletparam.value); } powershell.AddParameters(paramDictionary); } if (debug) { powershell.AddParameter("Debug"); } PrintPSCommand(powershell); result = powershell.Invoke(); if (debug) { Console.WriteLine(string.Join("", powershell.Streams.Debug)); } if (powershell.Streams.Error.Count > 0) { runspace.Close(); var exceptions = new List <Exception>(); foreach (ErrorRecord error in powershell.Streams.Error) { exceptions.Add(new Exception(error.Exception.Message)); } throw new AggregateException(exceptions); } } } runspace.Close(); return(result); }
// 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 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); // Initialize the arguments if ((ComputerName.Expression != null) && (PSRemotingBehavior.Get(context) != RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", ComputerName.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.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 (SetSeed.Expression != null) { targetCommand.AddParameter("SetSeed", SetSeed.Get(context)); } if (Maximum.Expression != null) { targetCommand.AddParameter("Maximum", Maximum.Get(context)); } if (Minimum.Expression != null) { targetCommand.AddParameter("Minimum", Minimum.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (Count.Expression != null) { targetCommand.AddParameter("Count", Count.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 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); // Initialize the arguments if (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (SecurityDescriptorSddl.Expression != null) { targetCommand.AddParameter("SecurityDescriptorSddl", SecurityDescriptorSddl.Get(context)); } if (SkipNetworkProfileCheck.Expression != null) { targetCommand.AddParameter("SkipNetworkProfileCheck", SkipNetworkProfileCheck.Get(context)); } if (NoServiceRestart.Expression != null) { targetCommand.AddParameter("NoServiceRestart", NoServiceRestart.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 (Uri.Expression != null) { targetCommand.AddParameter("Uri", Uri.Get(context)); } if (Class.Expression != null) { targetCommand.AddParameter("Class", Class.Get(context)); } if (Namespace.Expression != null) { targetCommand.AddParameter("Namespace", Namespace.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (UseDefaultCredential.Expression != null) { targetCommand.AddParameter("UseDefaultCredential", UseDefaultCredential.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 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); // Initialize the arguments if (String.Expression != null) { targetCommand.AddParameter("String", String.Get(context)); } if (AsPlainText.Expression != null) { targetCommand.AddParameter("AsPlainText", AsPlainText.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (SecureKey.Expression != null) { targetCommand.AddParameter("SecureKey", SecureKey.Get(context)); } if (Key.Expression != null) { targetCommand.AddParameter("Key", Key.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 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); // Initialize the arguments if (Path.Expression != null) { targetCommand.AddParameter("Path", Path.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (IncludeTotalCount.Expression != null) { targetCommand.AddParameter("IncludeTotalCount", IncludeTotalCount.Get(context)); } if (Skip.Expression != null) { targetCommand.AddParameter("Skip", Skip.Get(context)); } if (First.Expression != null) { targetCommand.AddParameter("First", First.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 (To.Expression != null) { targetCommand.AddParameter("To", To.Get(context)); } if (Content.Expression != null) { targetCommand.AddParameter("Content", Content.Get(context)); } if (Path.Expression != null) { targetCommand.AddParameter("Path", Path.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (OutFile.Expression != null) { targetCommand.AddParameter("OutFile", OutFile.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 (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (FilePath.Expression != null) { targetCommand.AddParameter("FilePath", FilePath.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (Append.Expression != null) { targetCommand.AddParameter("Append", Append.Get(context)); } if (Variable.Expression != null) { targetCommand.AddParameter("Variable", Variable.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 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); // Initialize the arguments if (Add.Expression != null) { targetCommand.AddParameter("Add", Add.Get(context)); } if (Remove.Expression != null) { targetCommand.AddParameter("Remove", Remove.Get(context)); } if (Replace.Expression != null) { targetCommand.AddParameter("Replace", Replace.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (Property.Expression != null) { targetCommand.AddParameter("Property", Property.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 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); // Initialize the arguments if (Seconds.Expression != null) { targetCommand.AddParameter("Seconds", Seconds.Get(context)); } if (Milliseconds.Expression != null) { targetCommand.AddParameter("Milliseconds", Milliseconds.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 (Description.Expression != null) { targetCommand.AddParameter("Description", Description.Get(context)); } if (RestorePointType.Expression != null) { targetCommand.AddParameter("RestorePointType", RestorePointType.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
private Job StartJobCommand(System.Management.Automation.PowerShell powerShell) { Job job = null; // Use PowerShell Start-Job cmdlet to run job. powerShell.AddCommand("Start-Job"); powerShell.AddParameter("Name", _jobDefinition.Name); // Add job parameters from the JobInvocationInfo object. CommandParameterCollection parameters = _jobDefinition.InvocationInfo.Parameters[0]; foreach (CommandParameter parameter in parameters) { switch (parameter.Name) { case "ScriptBlock": powerShell.AddParameter("ScriptBlock", parameter.Value as ScriptBlock); break; case "FilePath": powerShell.AddParameter("FilePath", parameter.Value as string); break; case "RunAs32": powerShell.AddParameter("RunAs32", (bool)parameter.Value); break; case "Authentication": powerShell.AddParameter("Authentication", (AuthenticationMechanism)parameter.Value); break; case "InitializationScript": powerShell.AddParameter("InitializationScript", parameter.Value as ScriptBlock); break; case "ArgumentList": powerShell.AddParameter("ArgumentList", parameter.Value as object[]); break; } } // Start the job. Collection <PSObject> rtn = powerShell.Invoke(); if (rtn != null && rtn.Count == 1) { job = rtn[0].BaseObject as Job; } return(job); }
/// <summary> /// Sample execution scenario 2: Asynchronous. /// </summary> /// <param name="script">Script command to be invoked.</param> /// <param name="parameters">Parameters for the command.</param> /// <remarks> /// Executes a PowerShell script asynchronously with script output and event handling. /// </remarks> public void ExecuteAsynchronously(string script, params PowerShellScriptParameter[] parameters) { using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddScript(script); if (parameters != null) { foreach (var parameter in parameters) { powerShell.AddParameter(parameter.Name, parameter.Value); } } // prepare a new collection to store output stream objects PSDataCollection <PSObject> outputCollection = new PSDataCollection <PSObject>(); outputCollection.DataAdded += this.OutputCollection_DataAdded; // the streams (Error, Debug, Progress, etc) are available on the PowerShell instance. // we can review them during or after execution. // we can also be notified when a new item is written to the stream (like this): powerShell.Streams.Error.DataAdded += this.Error_DataAdded; // begin invoke execution on the pipeline // use this overload to specify an output stream buffer IAsyncResult result = powerShell.BeginInvoke <PSObject, PSObject>(null, outputCollection); // do something else until execution has completed. // this could be sleep/wait, or perhaps some other work while (!result.IsCompleted) { Thread.Sleep(1000); } this.logger.LogDebug("Execution has stopped. The pipeline state: " + powerShell.InvocationStateInfo.State); foreach (var outputItem in outputCollection) { if (outputItem != null) { this.logger.LogDebug(outputItem.BaseObject.GetType().FullName); this.logger.LogDebug(outputItem.BaseObject.ToString() + "\n"); } } this.ProcessPowerShellStreams(powerShell); outputCollection.Dispose(); } }
public override Collection <PSObject> Run(bool debug) { Collection <PSObject> result = null; runspace.Open(); for (int i = 0; i < cmdlets.Count; i++) { using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create()) { powershell.Runspace = runspace; if (!String.IsNullOrWhiteSpace(cmdlets[i])) { powershell.AddScript(cmdlets[i]); } if (debug) { powershell.AddParameter("Debug"); } PrintPSCommand(powershell); result = powershell.Invoke(); if (debug) { Console.WriteLine(string.Join("", powershell.Streams.Debug)); } if (powershell.Streams.Error.Count > 0) { runspace.Close(); var exceptions = new List <Exception>(); foreach (ErrorRecord error in powershell.Streams.Error) { exceptions.Add(new Exception(error.Exception.Message)); } throw new AggregateException(exceptions); } } } runspace.Close(); return(result); }
// 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 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); // Initialize the arguments if (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (BinaryPathName.Expression != null) { targetCommand.AddParameter("BinaryPathName", BinaryPathName.Get(context)); } if (ServiceDisplayName.Expression != null) { targetCommand.AddParameter("DisplayName", ServiceDisplayName.Get(context)); } if (Description.Expression != null) { targetCommand.AddParameter("Description", Description.Get(context)); } if (StartupType.Expression != null) { targetCommand.AddParameter("StartupType", StartupType.Get(context)); } if (Credential.Expression != null) { targetCommand.AddParameter("Credential", Credential.Get(context)); } if (DependsOn.Expression != null) { targetCommand.AddParameter("DependsOn", DependsOn.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 (Depth.Expression != null) { targetCommand.AddParameter("Depth", Depth.Get(context)); } if (Path.Expression != null) { targetCommand.AddParameter("Path", Path.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (NoClobber.Expression != null) { targetCommand.AddParameter("NoClobber", NoClobber.Get(context)); } if (Encoding.Expression != null) { targetCommand.AddParameter("Encoding", Encoding.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 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); // Initialize the arguments if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (Begin.Expression != null) { targetCommand.AddParameter("Begin", Begin.Get(context)); } if (Process.Expression != null) { targetCommand.AddParameter("Process", Process.Get(context)); } if (End.Expression != null) { targetCommand.AddParameter("End", End.Get(context)); } if (RemainingScripts.Expression != null) { targetCommand.AddParameter("RemainingScripts", RemainingScripts.Get(context)); } if (MemberName.Expression != null) { targetCommand.AddParameter("MemberName", MemberName.Get(context)); } if (ArgumentList.Expression != null) { targetCommand.AddParameter("ArgumentList", ArgumentList.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 (Certificate.Expression != null) { targetCommand.AddParameter("Certificate", Certificate.Get(context)); } if (IncludeChain.Expression != null) { targetCommand.AddParameter("IncludeChain", IncludeChain.Get(context)); } if (TimestampServer.Expression != null) { targetCommand.AddParameter("TimestampServer", TimestampServer.Get(context)); } if (HashAlgorithm.Expression != null) { targetCommand.AddParameter("HashAlgorithm", HashAlgorithm.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (FilePath.Expression != null) { targetCommand.AddParameter("FilePath", FilePath.Get(context)); } if (LiteralPath.Expression != null) { targetCommand.AddParameter("LiteralPath", LiteralPath.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 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); // Initialize the arguments if (Delimiter.Expression != null) { targetCommand.AddParameter("Delimiter", Delimiter.Get(context)); } if (PropertyNames.Expression != null) { targetCommand.AddParameter("PropertyNames", PropertyNames.Get(context)); } if (TemplateFile.Expression != null) { targetCommand.AddParameter("TemplateFile", TemplateFile.Get(context)); } if (TemplateContent.Expression != null) { targetCommand.AddParameter("TemplateContent", TemplateContent.Get(context)); } if (IncludeExtent.Expression != null) { targetCommand.AddParameter("IncludeExtent", IncludeExtent.Get(context)); } if (UpdateTemplate.Expression != null) { targetCommand.AddParameter("UpdateTemplate", UpdateTemplate.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.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 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); // Initialize the arguments if (UnjoinDomainCredential.Expression != null) { targetCommand.AddParameter("UnjoinDomainCredential", UnjoinDomainCredential.Get(context)); } if (LocalCredential.Expression != null) { targetCommand.AddParameter("LocalCredential", LocalCredential.Get(context)); } if (Restart.Expression != null) { targetCommand.AddParameter("Restart", Restart.Get(context)); } if (Force.Expression != null) { targetCommand.AddParameter("Force", Force.Get(context)); } if (PassThru.Expression != null) { targetCommand.AddParameter("PassThru", PassThru.Get(context)); } if (WorkgroupName.Expression != null) { targetCommand.AddParameter("WorkgroupName", WorkgroupName.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.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 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); // Initialize the arguments if (Name.Expression != null) { targetCommand.AddParameter("Name", Name.Get(context)); } if (ProcessId.Expression != null) { targetCommand.AddParameter("Id", ProcessId.Get(context)); } if (InputObject.Expression != null) { targetCommand.AddParameter("InputObject", InputObject.Get(context)); } if (IncludeUserName.Expression != null) { targetCommand.AddParameter("IncludeUserName", IncludeUserName.Get(context)); } if (Module.Expression != null) { targetCommand.AddParameter("Module", Module.Get(context)); } if (FileVersionInfo.Expression != null) { targetCommand.AddParameter("FileVersionInfo", FileVersionInfo.Get(context)); } if (GetIsComputerNameSpecified(context) && (PSRemotingBehavior.Get(context) == RemotingBehavior.Custom)) { targetCommand.AddParameter("ComputerName", PSComputerName.Get(context)); } return(new ActivityImplementationContext() { PowerShellInstance = invoker }); }
/// <summary> /// Get the PowerShell instance for the PSv3 (or later) remote end /// Generate the PowerShell instance by using the text of the scriptblock /// </summary> /// <remarks> /// In PSv3 and PSv4, if the remote server is PSv3 or later, we generate an object array that contains the value of each using expression in /// the parsing order, and then pass the array to the remote end as a special argument. On the remote end, the using expressions will be indexed /// in the same parsing order during the variable analysis process, and the index is used to get the value of the corresponding using expression /// from the special array. There is a limitation in that approach -- $using cannot be used in different scopes with Invoke-Command/Start-Job /// (see WinBlue#475223), because the variable analysis process can only index using expressions within the same scope (this is by design), and a /// using expression from a different scope may be assigned with an index that conflicts with other using expressions. /// /// To fix the limitation described above, we changed to pass a dictionary with key/value pairs for the using expressions on the client side. The key /// is an unique base64 encoded string generated based on the text of the using expression. On the remote end, it can always get the unique key of a /// using expression because the text passed to the server side is the same, and thus the value of the using expression can be retrieved from the special /// dictionary. With this approach, $using in different scopes can be supported for Invoke-Command/Start-Job. /// /// This fix involved changes on the server side, so the fix will work only if the remote end is PSv5 or later. In order to avoid possible breaking /// change in 'PSv5 client - PSv3 server' and 'PSv5 client - PSv4 server' scenarios, we should keep sending the array-form using values if the remote /// end is PSv3 or PSv4 as long as no UsingExpression is in a different scope. If the remote end is PSv3 or PSv4 and we do have UsingExpressions /// in different scopes, then we will revert back to the approach we use to handle UsingExpression for PSv2 remote server. /// </remarks> /// <returns></returns> private System.Management.Automation.PowerShell GetPowerShellForPSv3OrLater(string serverPsVersion) { if (_powershellV3 != null) { return _powershellV3; } // Try to convert the scriptblock to powershell commands. _powershellV3 = ConvertToPowerShell(); if (_powershellV3 != null) { return _powershellV3; } // Using expressions can be a variable, as well as property and / or array references. E.g. // // icm { echo $using:a } // icm { echo $using:a[3] } // icm { echo $using:a.Length } // // Semantic checks on the using statement have already validated that there are no arbitrary expressions, // so we'll allow these expressions in everything but NoLanguage mode. bool allowUsingExpressions = (Context.SessionState.LanguageMode != PSLanguageMode.NoLanguage); object[] usingValuesInArray = null; IDictionary usingValuesInDict = null; // Value of 'serverPsVersion' should be either 'PSv3Orv4' or 'PSv5OrLater' if (serverPsVersion == PSv3Orv4) { usingValuesInArray = ScriptBlockToPowerShellConverter.GetUsingValuesAsArray(_scriptBlock, allowUsingExpressions, Context, null); if (usingValuesInArray == null) { // 'usingValuesInArray' will be null only if there are UsingExpressions used in different scopes. // PSv3 and PSv4 remote server cannot handle this, so we revert back to the approach we use for PSv2 remote end. return GetPowerShellForPSv2(); } } else { // Remote server is PSv5 or later version usingValuesInDict = ScriptBlockToPowerShellConverter.GetUsingValuesAsDictionary(_scriptBlock, allowUsingExpressions, Context, null); } string textOfScriptBlock = this.MyInvocation.ExpectingInput ? _scriptBlock.GetWithInputHandlingForInvokeCommand() : _scriptBlock.ToString(); _powershellV3 = System.Management.Automation.PowerShell.Create().AddScript(textOfScriptBlock); if (_args != null) { foreach (object arg in _args) { _powershellV3.AddArgument(arg); } } if (usingValuesInDict != null && usingValuesInDict.Count > 0) { _powershellV3.AddParameter(Parser.VERBATIM_ARGUMENT, usingValuesInDict); } else if (usingValuesInArray != null && usingValuesInArray.Length > 0) { _powershellV3.AddParameter(Parser.VERBATIM_ARGUMENT, usingValuesInArray); } return _powershellV3; }
/// <summary> /// Get the PowerShell instance for the PSv2 remote end /// Generate the PowerShell instance by using the text of the scriptblock /// </summary> /// <remarks> /// PSv2 doesn't understand the '$using' prefix. To make UsingExpression work on PSv2 remote end, we will have to /// alter the script, and send the altered script to the remote end. Since the script is altered, when there is an /// error, the error message will show the altered script, and that could be confusing to the user. So if the remote /// server is PSv3 or later version, we will use a different approach to handle UsingExpression so that we can keep /// the script unchanged. /// /// However, on PSv3 and PSv4 remote server, it's not well supported if UsingExpressions are used in different scopes (fixed in PSv5). /// If the remote end is PSv3 or PSv4, and there are UsingExpressions in different scopes, then we have to revert back to the approach /// used for PSv2 remote server. /// </remarks> /// <returns></returns> private System.Management.Automation.PowerShell GetPowerShellForPSv2() { if (_powershellV2 != null) { return _powershellV2; } // Try to convert the scriptblock to powershell commands. _powershellV2 = ConvertToPowerShell(); if (_powershellV2 != null) { // Look for EndOfStatement tokens. foreach (var command in _powershellV2.Commands.Commands) { if (command.IsEndOfStatement) { // PSv2 cannot process this. Revert to sending script. _powershellV2 = null; break; } } if (_powershellV2 != null) { return _powershellV2; } } List<string> newParameterNames; List<object> newParameterValues; string scriptTextAdaptedForPSv2 = GetConvertedScript(out newParameterNames, out newParameterValues); _powershellV2 = System.Management.Automation.PowerShell.Create().AddScript(scriptTextAdaptedForPSv2); if (_args != null) { foreach (object arg in _args) { _powershellV2.AddArgument(arg); } } if (newParameterNames != null) { Dbg.Assert(newParameterValues != null && newParameterNames.Count == newParameterValues.Count, "We should get the value for each using variable"); for (int i = 0; i < newParameterNames.Count; i++) { _powershellV2.AddParameter(newParameterNames[i], newParameterValues[i]); } } return _powershellV2; }