/// <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;
        }
Exemplo n.º 2
0
		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;
			}
		}
Exemplo n.º 3
0
		public static PowerShell AddScriptWithTrace(this PowerShell ps, string script)
		{
			Log.WriteInfo(script);
			return ps.AddScript(script);
		}
Exemplo n.º 4
0
		public static void AddScript(this PowerShell ps, string script, bool useLocalScope, params object[] args)
		{
			ps.AddScript(String.Format(script, args), useLocalScope);
		}
Exemplo n.º 5
0
		public static void AddConnectionScript(this PowerShell ps)
		{
			ps.AddScript(GetVMMServerByName, ConfigurationManager.AppSettings["SCVMMServerName"], ConfigurationManager.AppSettings["SCVMMServerPort"]);
		}
Exemplo n.º 6
0
		/// <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);
		}