protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context) { ValidateParameters(); System.Management.Automation.PowerShell invoker = null; HashSet <string> allWorkflowVarNames = new HashSet <string>(StaticPotentialUsingVariableSet, StringComparer.OrdinalIgnoreCase); Dictionary <string, object> defaults = this.ParameterDefaults.Get(context); Dictionary <string, object> activityVariables = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); Dictionary <string, object> activityUsingVariables = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); string[] streams = { "Result", "PSError", "PSWarning", "PSVerbose", "PSDebug", "PSProgress", "PSInformation" }; // First, set the variables from the user's variables foreach (System.ComponentModel.PropertyDescriptor property in context.DataContext.GetProperties()) { if (String.Equals(property.Name, "ParameterDefaults", StringComparison.OrdinalIgnoreCase)) { continue; } // Add all user-defined variables/parameters in the same scope of the InlineScript activity if (!allWorkflowVarNames.Contains(property.Name)) { allWorkflowVarNames.Add(property.Name); } Object value = property.GetValue(context.DataContext); if (value != null) { object tempValue = value; PSDataCollection <PSObject> collectionObject = value as PSDataCollection <PSObject>; if (collectionObject != null && collectionObject.Count == 1) { tempValue = collectionObject[0]; } activityVariables[property.Name] = tempValue; } } // Then, set anything we received from parameters foreach (PSActivityArgumentInfo currentArgument in GetActivityArguments()) { string @default = currentArgument.Name; if (streams.Any(item => string.Equals(item, @default, StringComparison.OrdinalIgnoreCase))) { continue; } object argumentValue = currentArgument.Value.Get(context); if (argumentValue != null && !activityVariables.ContainsKey(currentArgument.Name)) { activityVariables[currentArgument.Name] = argumentValue; } } // Then, set the variables from the host defaults if (defaults != null) { foreach (string hostDefault in defaults.Keys) { string @default = hostDefault; if (streams.Any(item => string.Equals(item, @default, StringComparison.OrdinalIgnoreCase))) { continue; } object propertyValue = defaults[hostDefault]; if (propertyValue != null && !activityVariables.ContainsKey(hostDefault)) { activityVariables[hostDefault] = propertyValue; } } } if (_commandSpecified) { string script = string.IsNullOrEmpty(Command) ? string.Empty : Command; Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Inline Script: '{1}'.", context.ActivityInstanceId, script)); if (IsBlocked(script)) { throw new PSInvalidOperationException(String.Format(CultureInfo.InvariantCulture, ActivityResources.CannotLaunchFormat, script)); } string[] targetNodes = null; if (this.PSComputerName.Expression != null) { targetNodes = this.PSComputerName.Get(context); } else { if (defaults != null && defaults.ContainsKey("PSComputerName")) { targetNodes = this.ParameterDefaults.Get(context)["PSComputerName"] as string[]; } } // See if this command will be run in process. if ((targetNodes == null || targetNodes.Length == 0) && GetRunInProc(context)) { if (_compiledScriptForInProc == null || _ci == null) { lock (Syncroot) { if (_compiledScriptForInProc == null) { if (_scriptWithoutUsing == null) { _scriptWithoutUsing = RemoveUsingPrefix(script, allWorkflowVarNames, out _usingVariables); } _compiledScriptForInProc = ScriptBlock.Create(_scriptWithoutUsing); } // Invoke using the CommandInfo for Invoke-Command directly, rather than going through // the command discovery since this is much faster. if (_ci == null) { _ci = new CmdletInfo("Invoke-Command", typeof(Microsoft.PowerShell.Commands.InvokeCommandCommand)); } } } SetAvailableUsingVariables(activityVariables, activityUsingVariables); Tracer.WriteMessage("PowerShell activity: executing InlineScript locally with ScriptBlock."); invoker = System.Management.Automation.PowerShell.Create(); invoker.AddCommand(_ci).AddParameter("NoNewScope").AddParameter("ScriptBlock", _compiledScriptForInProc); } else { // Try to convert the ScriptBlock to a powershell instance if (_compiledScriptForOutProc == null) { lock (Syncroot) { if (_compiledScriptForOutProc == null) { _compiledScriptForOutProc = ScriptBlock.Create(script); } } } try { // we trust the code inside inlinescript, set isTrusted as True. invoker = _compiledScriptForOutProc.GetPowerShell(activityVariables, out activityUsingVariables, true); Tracer.WriteMessage("PowerShell activity: executing InlineScript with ScriptBlock to powershell conversion."); } catch (Exception) { invoker = null; } if (invoker == null) { // Since scriptblocks aren't serialized with fidelity in the remote case, we need to // use AddScript instead. if (_scriptWithoutUsing == null) { lock (Syncroot) { if (_scriptWithoutUsing == null) { _scriptWithoutUsing = RemoveUsingPrefix(script, allWorkflowVarNames, out _usingVariables); } } } SetAvailableUsingVariables(activityVariables, activityUsingVariables); Tracer.WriteMessage("PowerShell activity: executing InlineScript by using AddScript."); invoker = System.Management.Automation.PowerShell.Create(); invoker.AddScript(_scriptWithoutUsing); } } } else { string commandName = CommandName.Get(context); if (String.IsNullOrEmpty(commandName)) { throw new ArgumentException(ActivityResources.CommandNameRequired); } Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity ID={0}: Invoking command '{1}'.", context.ActivityInstanceId, commandName)); invoker = System.Management.Automation.PowerShell.Create(); invoker.AddCommand(commandName); System.Collections.Hashtable parameters = Parameters.Get(context); if (parameters != null && parameters.Count > 0) { foreach (var key in parameters.Keys) { Tracer.WriteMessage(String.Format(CultureInfo.InvariantCulture, "PowerShell activity: Adding parameter '-{0} {1}'.", key, parameters[key])); } invoker.AddParameters(parameters); } } var implementationContext = new ActivityImplementationContext { PowerShellInstance = invoker, WorkflowContext = activityUsingVariables }; return(implementationContext); }