/// <summary> /// Invokes an array of scripts using the specified powershell instance. /// </summary> /// <param name="powershell">The powershell instance that executes the scripts.</param> /// <param name="scripts">An array of script to execute.</param> public static Collection<PSObject> InvokeBatchScript( this System.Management.Automation.PowerShell powershell, params string[] scripts) { if (powershell == null) { throw new ArgumentNullException("powershell"); } powershell.Commands.Clear(); foreach (string script in scripts) { Console.Error.WriteLine(script); powershell.AddScript(script); } Collection<PSObject> results = powershell.Invoke(); powershell.DumpStreams(); return results; }
public static Collection<PSObject> NewVirtualNetworkAdapter(this PowerShell ps, string virtualNetwork, ushort? vlanId, Guid jobGroup) { // var results = default(Collection<PSObject>); // try { // Snap-in registration ps.AddSystemCenterSnapIn(); // ps.AddConnectionScript(); ps.AddScriptWithTrace(NewPhysicalAddress); // This is for private network if (vlanId.HasValue && vlanId.Value > ushort.MinValue) { ps.AddScript(NewVirtualNetworkAdapterWithVLan, virtualNetwork, jobGroup, vlanId); } else // This is for public network { ps.AddScript(NewVirtualNetworkAdapterWithoutVLan, virtualNetwork, jobGroup); } // results = ps.InvokeAndDumpResults(); // return results; } catch (Exception e) { Log.WriteError(e); // Re-throw exception throw; } }
public static PowerShell AddScriptWithTrace(this PowerShell ps, string script) { Log.WriteInfo(script); return ps.AddScript(script); }
public static void AddScript(this PowerShell ps, string script, bool useLocalScope, params object[] args) { ps.AddScript(String.Format(script, args), useLocalScope); }
public static void AddConnectionScript(this PowerShell ps) { ps.AddScript(GetVMMServerByName, ConfigurationManager.AppSettings["SCVMMServerName"], ConfigurationManager.AppSettings["SCVMMServerPort"]); }
/// <summary> /// Returns true if no warnings have been issued /// </summary> /// <param name="ps"></param> /// <returns></returns> public static void AddSystemCenterSnapIn(this PowerShell ps) { ps.AddScript("Add-PSSnapin -Name \"{0}\" -ErrorAction SilentlyContinue", VirtualMachineManagerSnapIn); }