/// <summary> /// Adds the PSActivity variable to the active runspace, which is of type InlineScriptContext. /// </summary> /// <param name="implementationContext">The ActivityImplementationContext returned by the call to GetCommand.</param> protected override void PrepareSession(ActivityImplementationContext implementationContext) { if (implementationContext.PSActivityEnvironment == null) { implementationContext.PSActivityEnvironment = new PSActivityEnvironment(); } // Update the preference variables UpdatePreferenceVariables(implementationContext); System.Management.Automation.PowerShell session = implementationContext.PowerShellInstance; implementationContext.PSActivityEnvironment.Variables["UserName"] = System.Environment.UserName; string computerName = null; if (implementationContext.ConnectionInfo != null) { computerName = implementationContext.ConnectionInfo.ComputerName; } if (string.IsNullOrEmpty(computerName)) { computerName = "localhost"; } implementationContext.PSActivityEnvironment.Variables["ComputerName"] = computerName; implementationContext.PSActivityEnvironment.Variables["PSComputerName"] = computerName; string workflowCommandName = null; Dictionary <string, object> activityVariables = (Dictionary <string, object>)implementationContext.WorkflowContext; if (activityVariables != null && activityVariables.ContainsKey("ParameterDefaults")) { HostParameterDefaults defaults = activityVariables["ParameterDefaults"] as HostParameterDefaults; if (defaults != null) { workflowCommandName = defaults.Parameters["WorkflowCommandName"] as string; } } if (string.IsNullOrEmpty(workflowCommandName)) { workflowCommandName = "unknown"; } implementationContext.PSActivityEnvironment.Variables["CommandName"] = workflowCommandName; // Populate the default variables InlineScriptContext inlineScriptContext = new InlineScriptContext(this); // Populate the activity variables foreach (KeyValuePair <string, object> entry in activityVariables) { if (String.Equals(entry.Key, "ParameterDefaults", StringComparison.OrdinalIgnoreCase)) { System.Diagnostics.Debug.Assert(entry.Value is HostParameterDefaults, "ParameterDefaults does not contain a HostParameterDefaults object"); inlineScriptContext.Variables[entry.Key] = ((HostParameterDefaults)entry.Value).Parameters; continue; } inlineScriptContext.Variables[entry.Key] = entry.Value; } // Set the PowerShell session variables... foreach (KeyValuePair <string, object> entry in activityVariables) { var value = entry.Value; if (String.Equals(entry.Key, "ParameterDefaults", StringComparison.OrdinalIgnoreCase)) { continue; } implementationContext.PSActivityEnvironment.Variables[entry.Key] = value; } }
/// <summary> /// Adds the PSActivity variable to the active runspace, which is of type InlineScriptContext. /// </summary> /// <param name="implementationContext">The ActivityImplementationContext returned by the call to GetCommand.</param> protected override void PrepareSession(ActivityImplementationContext implementationContext) { if (implementationContext.PSActivityEnvironment == null) { implementationContext.PSActivityEnvironment = new PSActivityEnvironment(); } // Update the preference variables UpdatePreferenceVariables(implementationContext); System.Management.Automation.PowerShell session = implementationContext.PowerShellInstance; implementationContext.PSActivityEnvironment.Variables["UserName"] = System.Environment.UserName; string computerName = null; if (implementationContext.ConnectionInfo != null) { computerName = implementationContext.ConnectionInfo.ComputerName; } if (string.IsNullOrEmpty(computerName)) { computerName = "localhost"; } implementationContext.PSActivityEnvironment.Variables["ComputerName"] = computerName; implementationContext.PSActivityEnvironment.Variables["PSComputerName"] = computerName; string workflowCommandName = null; Dictionary<string, object> activityVariables = (Dictionary<string, object>)implementationContext.WorkflowContext; if (activityVariables != null && activityVariables.ContainsKey("ParameterDefaults")) { HostParameterDefaults defaults = activityVariables["ParameterDefaults"] as HostParameterDefaults; if (defaults != null) { workflowCommandName = defaults.Parameters["WorkflowCommandName"] as string; } } if (string.IsNullOrEmpty(workflowCommandName)) { workflowCommandName = "unknown"; } implementationContext.PSActivityEnvironment.Variables["CommandName"] = workflowCommandName; // Populate the default variables InlineScriptContext inlineScriptContext = new InlineScriptContext(this); // Populate the activity variables foreach (KeyValuePair<string, object> entry in activityVariables) { if (String.Equals(entry.Key, "ParameterDefaults", StringComparison.OrdinalIgnoreCase)) { System.Diagnostics.Debug.Assert(entry.Value is HostParameterDefaults, "ParameterDefaults does not contain a HostParameterDefaults object"); inlineScriptContext.Variables[entry.Key] = ((HostParameterDefaults)entry.Value).Parameters; continue; } inlineScriptContext.Variables[entry.Key] = entry.Value; } // Set the PowerShell session variables... foreach (KeyValuePair<string, object> entry in activityVariables) { var value = entry.Value; if (String.Equals(entry.Key, "ParameterDefaults", StringComparison.OrdinalIgnoreCase)) continue; implementationContext.PSActivityEnvironment.Variables[entry.Key] = value; } }