public System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection <object> errors) { errors = new System.Collections.ObjectModel.Collection <object>(); System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results; using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline()) { System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(paramSet.Cmdlet); if (paramSet.Parameters != null) { foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters) { if (parameter.Value != null && parameter.Value.IsSet) { cmd.Parameters.Add(parameter.ParameterSwitch, parameter.Value.ValueObject); } } pipeline.Commands.Add(cmd); } results = pipeline.Invoke(); errors = pipeline.Error.ReadToEnd(); //try //{ // results = pipeline.Invoke(); // return results; //} //catch (Exception Ex) //{ // errors = pipeline.Error.ReadToEnd(); //} //return null; } return(results); }
protected IEnumerable <PSObject> ExecuteCmdlet(string cmdletName, Dictionary <string, object> parameters, string moduleName = "AWSPowerShell") { using (var pipeline = System.Management.Automation.Runspaces.Runspace.DefaultRunspace.CreateNestedPipeline()) { System.Management.Automation.Runspaces.Command command = new System.Management.Automation.Runspaces.Command($"{moduleName}\\{cmdletName}"); if (parameters != null) { foreach (var parameter in parameters) { command.Parameters.Add(parameter.Key, parameter.Value); } } pipeline.Commands.Add(command); return(pipeline.Invoke()); } }
private Collection <PSModuleInfo> GetPSModuleInfo(string modulePath) { PSModuleInfo moduleInfo = null; lock (_lockObject) { _moduleInfoCache.TryGetValue(modulePath, out moduleInfo); } if (moduleInfo != null) { var returnValue = new Collection <PSModuleInfo>(); returnValue.Add(moduleInfo); return(returnValue); } CommandInfo commandInfo = new CmdletInfo("Get-Module", typeof(Microsoft.PowerShell.Commands.GetModuleCommand), null, null, _context); System.Management.Automation.Runspaces.Command getModuleCommand = new System.Management.Automation.Runspaces.Command(commandInfo); string moduleName = Path.GetFileNameWithoutExtension(modulePath); var modules = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace) .AddCommand(getModuleCommand) .AddParameter("List", true) .AddParameter("Name", moduleName) .AddParameter("ErrorAction", ActionPreference.Ignore) .AddParameter("WarningAction", ActionPreference.Ignore) .AddParameter("InformationAction", ActionPreference.Ignore) .AddParameter("Verbose", false) .AddParameter("Debug", false) .Invoke <PSModuleInfo>(); lock (_lockObject) { foreach (var module in modules) { _moduleInfoCache.Add(module.Path, module); } } return(modules); }
public System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection<object> errors) { errors = new System.Collections.ObjectModel.Collection<object>(); System.Collections.ObjectModel.Collection<System.Management.Automation.PSObject> results; using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline()) { System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(paramSet.Cmdlet); if (paramSet.Parameters != null) { foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters) { if (parameter.Value != null && parameter.Value.IsSet) { cmd.Parameters.Add(parameter.ParameterSwitch, parameter.Value.ValueObject); } } pipeline.Commands.Add(cmd); } results = pipeline.Invoke(); errors = pipeline.Error.ReadToEnd(); //try //{ // results = pipeline.Invoke(); // return results; //} //catch (Exception Ex) //{ // errors = pipeline.Error.ReadToEnd(); //} //return null; } return results; }
public static void AddParameter(this System.Management.Automation.Runspaces.Command cmd, string name, object value) { var parameter = new System.Management.Automation.Runspaces.CommandParameter(name, value); cmd.Parameters.Add(parameter); }
public void RunCustomDesignRules(Database db, Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox) { UIHierarchy solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer; if (((System.Array)solExplorer.SelectedItems).Length != 1) { return; } //UIHierarchyItem hierItem = ((UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0)); // TODO - get database properties //ProjectItem pi = (ProjectItem)hierItem.Object; //Window w = pi.Open(BIDSViewKinds.Designer); //opens the designer //w.Activate(); try { //ClearTaskList(); ClearErrorList(); ApplicationObject.StatusBar.Text = "Starting Powershell Design Rules Engine..."; var ps = PowerShell.Create(); string modelType; if (db == null) { modelType = "Tabular"; ps.Runspace.SessionStateProxy.SetVariable("WorkspaceConnection", sandbox.AdomdConnection); ps.Runspace.SessionStateProxy.SetVariable("CurrentCube", sandbox.Cube); } else { modelType = "MultiDim"; ps.Runspace.SessionStateProxy.SetVariable("CurrentDB", db); } ps.Runspace.SessionStateProxy.SetVariable("ModelType", modelType); ps.Runspace.SessionStateProxy.SetVariable("VerbosePreference", "Continue"); ps.Streams.Debug.DataAdded += new EventHandler <DataAddedEventArgs>(Debug_DataAdded); ps.Streams.Warning.DataAdded += new EventHandler <DataAddedEventArgs>(Warning_DataAdded); ps.Streams.Error.DataAdded += new EventHandler <DataAddedEventArgs>(Error_DataAdded); ps.Streams.Verbose.DataAdded += new EventHandler <DataAddedEventArgs>(Verbose_DataAdded); ApplicationObject.StatusBar.Text = "Loading Custom Design Rules..."; // TODO - look into adding a script to allow calling a copy from a central rule repository string dllFile = (new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath; System.Diagnostics.Debug.WriteLine(dllFile); FileInfo dll = new FileInfo(dllFile); DirectoryInfo scripts = new DirectoryInfo(dll.Directory.FullName + "\\SSAS_Design_Rules"); foreach (FileInfo f in scripts.GetFiles(modelType + "_*.ps1")) { TextReader tr = new StreamReader(f.OpenRead()); string psScript = tr.ReadToEnd(); ps.Commands.Clear(); ps.AddScript(psScript); ps.Invoke(); } ps.Commands.Clear(); // get a list of functions var pipeline = ps.Runspace.CreatePipeline(); var cmd1 = new System.Management.Automation.Runspaces.Command("get-childitem"); cmd1.Parameters.Add("Path", @"function:\Check*"); pipeline.Commands.Add(cmd1); var cmd2 = new System.Management.Automation.Runspaces.Command("where-object"); var sb = ScriptBlock.Create("$_.Parameters.Count -eq 0"); cmd2.Parameters.Add("FilterScript", sb); pipeline.Commands.Add(cmd2); var funcs = pipeline.Invoke(); var funcList = new List <string>(); foreach (var f in funcs) { funcList.Add(f.ToString()); } ps.Commands.Clear(); ApplicationObject.StatusBar.Text = ""; // show dialog var dialog = new CustomDesignRulesCheckerForm(); dialog.Functions = funcList; dialog.Plugin = this; if (dialog.ShowDialog() == DialogResult.OK) { var iCnt = 0; // run selected functions foreach (var f in dialog.Functions) { ApplicationObject.StatusBar.Progress(true, string.Format("Running rule '{0}'", f), iCnt, dialog.Functions.Count); ps.AddCommand(f); ps.Invoke(); iCnt++; } ApplicationObject.StatusBar.Progress(false); } } catch (System.Exception ex) { AddTaskItem(PriorityType.Error, "ERROR RUNNING DESIGN CHECKS: " + ex.Message); } }
public void RunCustomDesignRules(Database db, Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox) { UIHierarchy solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer; if (((System.Array)solExplorer.SelectedItems).Length != 1) return; //UIHierarchyItem hierItem = ((UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0)); // TODO - get database properties //ProjectItem pi = (ProjectItem)hierItem.Object; //Window w = pi.Open(BIDSViewKinds.Designer); //opens the designer //w.Activate(); try { //ClearTaskList(); ClearErrorList(); ApplicationObject.StatusBar.Text = "Starting Powershell Design Rules Engine..."; var ps = PowerShell.Create(); string modelType; if (db == null) { modelType = "Tabular"; ps.Runspace.SessionStateProxy.SetVariable("WorkspaceConnection", sandbox.AdomdConnection); ps.Runspace.SessionStateProxy.SetVariable("CurrentCube", sandbox.Cube); } else { modelType = "MultiDim"; ps.Runspace.SessionStateProxy.SetVariable("CurrentDB", db); } ps.Runspace.SessionStateProxy.SetVariable("ModelType", modelType); ps.Runspace.SessionStateProxy.SetVariable("VerbosePreference", "Continue"); ps.Streams.Debug.DataAdded += new EventHandler<DataAddedEventArgs>(Debug_DataAdded); ps.Streams.Warning.DataAdded += new EventHandler<DataAddedEventArgs>(Warning_DataAdded); ps.Streams.Error.DataAdded += new EventHandler<DataAddedEventArgs>(Error_DataAdded); ps.Streams.Verbose.DataAdded += new EventHandler<DataAddedEventArgs>(Verbose_DataAdded); ApplicationObject.StatusBar.Text = "Loading Custom Design Rules..."; // TODO - look into adding a script to allow calling a copy from a central rule repository string dllFile = (new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath; System.Diagnostics.Debug.WriteLine(dllFile); FileInfo dll = new FileInfo(dllFile); DirectoryInfo scripts = new DirectoryInfo(dll.Directory.FullName + "\\SSAS_Design_Rules"); foreach (FileInfo f in scripts.GetFiles(modelType + "_*.ps1")) { TextReader tr = new StreamReader(f.OpenRead()); string psScript = tr.ReadToEnd(); ps.Commands.Clear(); ps.AddScript(psScript); ps.Invoke(); } ps.Commands.Clear(); // get a list of functions var pipeline = ps.Runspace.CreatePipeline(); var cmd1 = new System.Management.Automation.Runspaces.Command("get-childitem"); cmd1.Parameters.Add("Path", @"function:\Check*"); pipeline.Commands.Add(cmd1); var cmd2 = new System.Management.Automation.Runspaces.Command("where-object"); var sb = ScriptBlock.Create("$_.Parameters.Count -eq 0"); cmd2.Parameters.Add("FilterScript", sb); pipeline.Commands.Add(cmd2); var funcs = pipeline.Invoke(); var funcList = new List<string>(); foreach (var f in funcs) { funcList.Add(f.ToString()); } ps.Commands.Clear(); ApplicationObject.StatusBar.Text = ""; // show dialog var dialog = new CustomDesignRulesCheckerForm(); dialog.Functions = funcList; dialog.Plugin = this; if (dialog.ShowDialog() == DialogResult.OK) { var iCnt = 0; // run selected functions foreach (var f in dialog.Functions) { ApplicationObject.StatusBar.Progress(true,string.Format("Running rule '{0}'",f),iCnt, dialog.Functions.Count ); ps.AddCommand(f); ps.Invoke(); iCnt++; } ApplicationObject.StatusBar.Progress(false); } } catch (System.Exception ex) { AddTaskItem(PriorityType.Error, "ERROR RUNNING DESIGN CHECKS: " + ex.Message); } }
// InlineScript needs to handle these specially, since it might go through the PowerShell AddScript() API. // If the parameter "CommandName" is in use, we add the preference configuration to the command parameters, // otherwise, we add the preference configuration to the preference variable. // All other activities have this set automatically by the infrastructure via parameters. private void UpdatePreferenceVariables(ActivityImplementationContext implementationContext) { System.Management.Automation.PowerShell session = implementationContext.PowerShellInstance; System.Management.Automation.Runspaces.Command command = null; if (!_commandSpecified) { // "CommandName" and "Parameters" are in use command = session.Commands.Commands[0]; } if (implementationContext.Verbose != null) { if (command != null) { command.Parameters.Add("Verbose", implementationContext.Verbose); } else { // Map the boolean / switch to an actual action preference ActionPreference preference = ActionPreference.SilentlyContinue; if (implementationContext.Verbose.Value) { preference = ActionPreference.Continue; } implementationContext.PSActivityEnvironment.Variables["VerbosePreference"] = preference; } } if (implementationContext.Debug != null) { if (command != null) { command.Parameters.Add("Debug", implementationContext.Debug); } else { // Map the boolean / switch to an actual action preference ActionPreference preference = ActionPreference.SilentlyContinue; if (implementationContext.Debug.Value) { preference = ActionPreference.Continue; } implementationContext.PSActivityEnvironment.Variables["DebugPreference"] = preference; } } if (implementationContext.WhatIf != null && command != null) { command.Parameters.Add("WhatIf", implementationContext.WhatIf); } if (implementationContext.ErrorAction != null) { if (command != null) { command.Parameters.Add("ErrorAction", implementationContext.ErrorAction); } else { implementationContext.PSActivityEnvironment.Variables["ErrorActionPreference"] = implementationContext.ErrorAction; } } if (implementationContext.WarningAction != null) { if (command != null) { command.Parameters.Add("WarningAction", implementationContext.WarningAction); } else { implementationContext.PSActivityEnvironment.Variables["WarningPreference"] = implementationContext.WarningAction; } } if (implementationContext.InformationAction != null) { if (command != null) { command.Parameters.Add("InformationAction", implementationContext.InformationAction); } else { implementationContext.PSActivityEnvironment.Variables["InformationPreference"] = implementationContext.InformationAction; } } }
public static void AddParameter(this SMA.Runspaces.Command cmd, string name, object value) { var parameter = new SMA.Runspaces.CommandParameter(name, value); cmd.Parameters.Add(parameter); }