public static Endpoint GenerateCallback(this ScriptBlock endpoint, string id, PSCmdlet cmdlet, System.Management.Automation.SessionState sessionState, object[] argumentList = null) { if (endpoint == null) { return(null); } var logger = LogManager.GetLogger("CallbackCmdlet"); var callback = new Endpoint(); callback.Name = id; callback.ScriptBlock = endpoint; callback.Variables = new Dictionary <string, object>(); callback.ArgumentList = argumentList; try { var variables = endpoint.Ast.FindAll(x => x is VariableExpressionAst, true).Cast <VariableExpressionAst>().Select(m => m.VariablePath.ToString()); foreach (var variableName in variables) { var variable = sessionState.PSVariable.Get(variableName); if (variable != null && !variable.Options.HasFlag(ScopedItemOptions.Constant) && !variable.Options.HasFlag(ScopedItemOptions.ReadOnly)) { if (!callback.Variables.ContainsKey(variable.Name)) { callback.Variables.Add(variable.Name, sessionState.PSVariable.GetValue(variable.Name)); } } } } catch (Exception ex) { logger.Error(ex, "Failed to look up variables."); } callback.SessionId = sessionState.PSVariable.Get(Constants.SessionId)?.Value as string; callback.Page = sessionState.PSVariable.Get(Constants.UDPage)?.Value as Page; var hostState = cmdlet.GetHostState(); hostState.EndpointService.Register(callback); return(callback); }
public void Register(string id, PSCmdlet cmdlet) { if (!string.IsNullOrEmpty(Name)) { // already registerd return; } Name = id; Variables = new Dictionary <string, object>(); try { var variables = ScriptBlock.Ast.FindAll(x => x is VariableExpressionAst, true).Cast <VariableExpressionAst>().Select(m => m.VariablePath.ToString()); foreach (var variableName in variables) { var variable = cmdlet.SessionState.PSVariable.Get(variableName); if (variable != null && !variable.Options.HasFlag(ScopedItemOptions.Constant) && !variable.Options.HasFlag(ScopedItemOptions.ReadOnly)) { if (!Variables.ContainsKey(variable.Name)) { Variables.Add(variable.Name, cmdlet.SessionState.PSVariable.GetValue(variable.Name)); } } } } catch (Exception ex) { cmdlet.WriteWarning(ex.Message); } SessionId = cmdlet.SessionState.PSVariable.Get(Constants.SessionId)?.Value as string; Page = cmdlet.SessionState.PSVariable.Get(Constants.UDPage)?.Value as Page; var state = cmdlet.GetHostState(); state.EndpointService.Register(this); }