Exemplo n.º 1
0
        private void GetSystemSummary()
        {
            const UInt64 Size1KB = 0x400;
            const UInt64 Size1GB = 0x40000000;

            // check free/total RAM
            WmiUtils wmi = new WmiUtils("root\\cimv2");
            ManagementObjectCollection objOses = wmi.ExecuteQuery("SELECT * FROM Win32_OperatingSystem");

            foreach (ManagementObject objOs in objOses)
            {
                UInt64 freeRam  = Convert.ToUInt64(objOs["FreePhysicalMemory"]) / Size1KB;
                UInt64 totalRam = Convert.ToUInt64(objOs["TotalVisibleMemorySize"]) / Size1KB;

                // write to the registry
                RegistryUtils.SetRegistryKeyStringValue(RegistryOutputKey, RAM_SUMMARY_KEY,
                                                        String.Format("{0}:{1}", freeRam, totalRam));

                objOs.Dispose();
            }

            // check local HDD logical drives
            ManagementObjectCollection objDisks = wmi.ExecuteQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3");
            StringBuilder sb    = new StringBuilder();
            bool          first = true;

            foreach (ManagementObject objDisk in objDisks)
            {
                if (!first)
                {
                    sb.Append(";");
                }

                sb.Append(objDisk["DeviceID"]);
                sb.Append(Convert.ToInt32(Convert.ToDouble(objDisk["FreeSpace"]) / (double)Size1GB)).Append(":");
                sb.Append(Convert.ToInt32(Convert.ToDouble(objDisk["Size"]) / (double)Size1GB));

                first = false;

                objDisk.Dispose();
            }

            // write HDD info
            RegistryUtils.SetRegistryKeyStringValue(RegistryOutputKey, HDD_SUMMARY_KEY, sb.ToString());

            // dispose resources
            objOses.Dispose();
            objDisks.Dispose();
        }
        private string GetAdministratorName()
        {
            WmiUtils wmi = new WmiUtils("root\\cimv2");
            ManagementObjectCollection objUsers = wmi.ExecuteQuery("Select * From Win32_UserAccount Where LocalAccount = TRUE");

            foreach (ManagementObject objUser in objUsers)
            {
                string sid = (string)objUser["SID"];
                if (sid != null && sid.StartsWith("S-1-5-") && sid.EndsWith("-500"))
                {
                    return((string)objUser["Name"]);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
 private void RebootSystem()
 {
     try
     {
         ServiceLog.WriteStart("RebootSystem");
         WmiUtils wmi = new WmiUtils("root\\cimv2");
         ManagementObjectCollection objOses = wmi.ExecuteQuery("SELECT * FROM Win32_OperatingSystem");
         foreach (ManagementObject objOs in objOses)
         {
             objOs.Scope.Options.EnablePrivileges = true;
             objOs.InvokeMethod("Reboot", null);
         }
         ServiceLog.WriteEnd("RebootSystem");
     }
     catch (Exception ex)
     {
         ServiceLog.WriteError("Reboot System error:", ex);
     }
 }
Exemplo n.º 4
0
		private void RebootSystem()
		{
			try
			{
				ServiceLog.WriteStart("RebootSystem");
				WmiUtils wmi = new WmiUtils("root\\cimv2");
				ManagementObjectCollection objOses = wmi.ExecuteQuery("SELECT * FROM Win32_OperatingSystem");
				foreach (ManagementObject objOs in objOses)
				{
					objOs.Scope.Options.EnablePrivileges = true;
					objOs.InvokeMethod("Reboot", null);
				}
				ServiceLog.WriteEnd("RebootSystem");
			}
			catch (Exception ex)
			{
				ServiceLog.WriteError("Reboot System error:", ex);
			}
		}
Exemplo n.º 5
0
        private void GetSystemSummary()
        {
            const UInt64 Size1KB = 0x400;
            const UInt64 Size1GB = 0x40000000;

            // check free/total RAM
            WmiUtils wmi = new WmiUtils("root\\cimv2");
            ManagementObjectCollection objOses = wmi.ExecuteQuery("SELECT * FROM Win32_OperatingSystem");
            foreach (ManagementObject objOs in objOses)
            {
                UInt64 freeRam = Convert.ToUInt64(objOs["FreePhysicalMemory"]) / Size1KB;
                UInt64 totalRam = Convert.ToUInt64(objOs["TotalVisibleMemorySize"]) / Size1KB;

                // write to the registry
                RegistryUtils.SetRegistryKeyStringValue(RegistryOutputKey, RAM_SUMMARY_KEY,
                    String.Format("{0}:{1}", freeRam, totalRam));

                objOs.Dispose();
            }

            // check local HDD logical drives
            ManagementObjectCollection objDisks = wmi.ExecuteQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType = 3");
            StringBuilder sb = new StringBuilder();
            bool first = true;
            foreach (ManagementObject objDisk in objDisks)
            {
                if(!first)
                    sb.Append(";");

                sb.Append(objDisk["DeviceID"]);
                sb.Append(Convert.ToInt32(Convert.ToDouble(objDisk["FreeSpace"]) / (double)Size1GB)).Append(":");
                sb.Append(Convert.ToInt32(Convert.ToDouble(objDisk["Size"]) / (double)Size1GB));

                first = false;

                objDisk.Dispose();
            }

            // write HDD info
            RegistryUtils.SetRegistryKeyStringValue(RegistryOutputKey, HDD_SUMMARY_KEY, sb.ToString());

            // dispose resources
            objOses.Dispose();
            objDisks.Dispose();
        }
        private string GetAdministratorName()
        {
            WmiUtils wmi = new WmiUtils("root\\cimv2");
            ManagementObjectCollection objUsers = wmi.ExecuteQuery("Select * From Win32_UserAccount Where LocalAccount = TRUE");

            foreach (ManagementObject objUser in objUsers)
            {
                string sid = (string)objUser["SID"];
                if (sid != null && sid.StartsWith("S-1-5-") && sid.EndsWith("-500"))
                    return (string)objUser["Name"];
            }

            return null;
        }
Exemplo n.º 7
0
        public ExecutionResult Run(ref ExecutionContext context)
        {
            ExecutionResult ret = new ExecutionResult();

            ret.ResultCode     = 0;
            ret.ErrorMessage   = null;
            ret.RebootRequired = false;

            context.ActivityDescription = "Configuring network adapter...";
            context.Progress            = 0;
            if (!CheckParameter(context, "MAC"))
            {
                ProcessError(context, ret, null, 2, "Parameter 'MAC' is not specified");
                return(ret);
            }
            string macAddress = context.Parameters["MAC"];

            if (!IsValidMACAddress(macAddress))
            {
                ProcessError(context, ret, null, 2, "Parameter 'MAC' has invalid format. It should be in 12:34:56:78:90:ab format.");
                return(ret);
            }

            string                     adapterId   = null;
            ManagementObject           objAdapter  = null;
            ManagementObjectCollection objAdapters = null;
            int attempts = 0;

            try
            {
                WmiUtils wmi   = new WmiUtils("root\\cimv2");
                string   query = string.Format("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress = '{0}' AND IPEnabled = True", macAddress);
                //try to find adapter for 10 times
                while (true)
                {
                    objAdapters = wmi.ExecuteQuery(query);

                    if (objAdapters.Count > 0)
                    {
                        foreach (ManagementObject adapter in objAdapters)
                        {
                            objAdapter = adapter;
                            adapterId  = (string)adapter["SettingID"];
                        }
                        break;
                    }
                    if (attempts > 9)
                    {
                        ProcessError(context, ret, null, 2, "Network adapter not found");
                        return(ret);
                    }

                    attempts++;
                    Log.WriteError(string.Format("Attempt #{0} to find network adapter (mac: {1}) failed!", attempts, macAddress));
                    // wait 1 min
                    System.Threading.Thread.Sleep(60000);
                    //repeat loop
                }
            }
            catch (Exception ex)
            {
                ProcessError(context, ret, ex, 2, "Network adapter configuration error: ");
                return(ret);
            }

            if (CheckParameter(context, "EnableDHCP", "True"))
            {
                try
                {
                    EnableDHCP(objAdapter);
                }
                catch (Exception ex)
                {
                    ProcessError(context, ret, ex, 2, "DHCP error: ");
                    return(ret);
                }
            }
            else if (CheckParameter(context, "EnableDHCP", "False"))
            {
                if (!CheckParameter(context, "DefaultIPGateway"))
                {
                    ProcessError(context, ret, null, 2, "Parameter 'DefaultIPGateway' is not specified");
                    return(ret);
                }
                if (!CheckParameter(context, "IPAddress"))
                {
                    ProcessError(context, ret, null, 2, "Parameter 'IPAddresses' is not specified");
                    return(ret);
                }
                if (!CheckParameter(context, "SubnetMask"))
                {
                    ProcessError(context, ret, null, 2, "Parameter 'SubnetMasks' is not specified");
                    return(ret);
                }
                try
                {
                    DisableDHCP(context, objAdapter);
                }
                catch (Exception ex)
                {
                    ProcessError(context, ret, ex, 2, "Network adapter configuration error: ");
                    return(ret);
                }
            }
            if (CheckParameter(context, "PreferredDNSServer"))
            {
                try
                {
                    SetDNSServer(context, objAdapter);
                }
                catch (Exception ex)
                {
                    ProcessError(context, ret, ex, 2, "DNS error: ");
                    return(ret);
                }
            }
            context.Progress = 100;
            return(ret);
        }
		public ExecutionResult Run(ref ExecutionContext context)
		{
			ExecutionResult ret = new ExecutionResult();
			ret.ResultCode = 0;
			ret.ErrorMessage = null;
			ret.RebootRequired = false;

			context.ActivityDescription = "Configuring network adapter...";
			context.Progress = 0;
			if (!CheckParameter(context, "MAC"))
			{
				ProcessError(context, ret, null, 2, "Parameter 'MAC' is not specified");
				return ret;
			}
			string macAddress = context.Parameters["MAC"];
			if (!IsValidMACAddress(macAddress))
			{
				ProcessError(context, ret, null, 2, "Parameter 'MAC' has invalid format. It should be in 12:34:56:78:90:ab format.");
				return ret;
			}
			
			string adapterId = null;
			ManagementObject objAdapter = null;
			ManagementObjectCollection objAdapters = null;
			int attempts = 0;
			try
			{
				WmiUtils wmi = new WmiUtils("root\\cimv2");
				string query = string.Format("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress = '{0}' AND IPEnabled = True", macAddress);
				//try to find adapter for 10 times
				while (true)
				{

					objAdapters = wmi.ExecuteQuery(query);

					if (objAdapters.Count > 0)
					{
						foreach (ManagementObject adapter in objAdapters)
						{
							objAdapter = adapter;
							adapterId = (string)adapter["SettingID"];
						}
						break;

					}
					if (attempts > 9)
					{
						ProcessError(context, ret, null, 2, "Network adapter not found");
						return ret;
					}
					
					attempts++;
                    Log.WriteError(string.Format("Attempt #{0} to find network adapter (mac: {1}) failed!", attempts, macAddress));
					// wait 1 min
					System.Threading.Thread.Sleep(60000);
					//repeat loop
				}
				
			}
			catch (Exception ex)
			{
				ProcessError(context, ret, ex, 2, "Network adapter configuration error: ");
				return ret;
			}

			if (CheckParameter(context, "EnableDHCP", "True"))
			{
				try
				{
					EnableDHCP(objAdapter);
				}
				catch (Exception ex)
				{
					ProcessError(context, ret, ex, 2, "DHCP error: ");
					return ret;
				}

			}
			else if (CheckParameter(context, "EnableDHCP", "False"))
			{
				if (!CheckParameter(context, "DefaultIPGateway"))
				{
					ProcessError(context, ret, null, 2, "Parameter 'DefaultIPGateway' is not specified");
					return ret;
				}
				if (!CheckParameter(context, "IPAddress"))
				{
					ProcessError(context, ret, null, 2, "Parameter 'IPAddresses' is not specified");
					return ret;
				}
				if (!CheckParameter(context, "SubnetMask"))
				{
					ProcessError(context, ret, null, 2, "Parameter 'SubnetMasks' is not specified");
					return ret;
				}
				try
				{
					DisableDHCP(context, objAdapter);
				}
				catch (Exception ex)
				{
					ProcessError(context, ret, ex, 2, "Network adapter configuration error: ");
					return ret;
				}
			}
			if (CheckParameter(context, "PreferredDNSServer"))
			{
				try
				{
					SetDNSServer(context, objAdapter);
				}
				catch (Exception ex)
				{
					ProcessError(context, ret, ex, 2, "DNS error: ");
					return ret;
				}

			}
			context.Progress = 100;
			return ret;
		}