public static RemoteSystemInfo GetSystemInfo()
        {
            var systemInfo = new RemoteSystemInfo();
            var taskResult = new TaskResult();

            systemInfo.Result = taskResult;

            ConnectionOptions op = new ConnectionOptions();

            if (GlobalVar.UseAlternateCredentials)
            {
                op.Username  = GlobalVar.AlternateUsername;
                op.Password  = GlobalVar.AlternatePassword;
                op.Authority = $"NTLMDOMAIN:{GlobalVar.AlternateDomain}";
            }
            ManagementScope          sc       = new ManagementScope($@"\\{TargetComputer}\root\CIMV2", op);
            ObjectQuery              query    = new ObjectQuery("SELECT Caption,Description,LastBootUpTime,Version,ProductType FROM Win32_OperatingSystem");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query);

            try
            {
                foreach (ManagementObject obj in searcher.Get())
                {
                    systemInfo.ComputerType         = (obj["ProductType"] != null) ? obj["ProductType"].ToString() : string.Empty;
                    systemInfo.WindowsVersionNumber = (obj["Version"] != null) ? obj["Version"].ToString().Trim() : string.Empty;
                    systemInfo.WindowsVersion       = (obj["Caption"] != null) ? obj["Caption"].ToString().Trim() : string.Empty;
                    systemInfo.ComputerDescription  = (obj["Description"] != null) ? obj["Description"].ToString().Trim() : string.Empty;
                    int index = systemInfo.WindowsVersion.IndexOf(@"(R)", StringComparison.OrdinalIgnoreCase);
                    while (index >= 0)
                    {
                        systemInfo.WindowsVersion = systemInfo.WindowsVersion.Remove(index, @"(R)".Length);
                        index = systemInfo.WindowsVersion.IndexOf(@"(R)", StringComparison.OrdinalIgnoreCase);
                    }
                    index = systemInfo.WindowsVersion.IndexOf(@"®", StringComparison.OrdinalIgnoreCase);
                    while (index >= 0)
                    {
                        systemInfo.WindowsVersion = systemInfo.WindowsVersion.Remove(index, @"®".Length);
                        index = systemInfo.WindowsVersion.IndexOf(@"®", StringComparison.OrdinalIgnoreCase);
                    }

                    if (obj["LastBootUpTime"] != null)
                    {
                        DateTime lastBoot = ManagementDateTimeConverter.ToDateTime(obj["LastBootUpTime"].ToString());
                        TimeSpan ts       = DateTime.Now - lastBoot;
                        string   uptime;
                        if (ts.Days > 0)
                        {
                            uptime = string.Format("{0} day{1}, {2} hour{3}, {4} minute{5}",
                                                   ts.Days, ts.Days == 1 ? "" : "s",
                                                   ts.Hours, ts.Hours == 1 ? "" : "s",
                                                   ts.Minutes, ts.Minutes == 1 ? "" : "s");
                        }
                        else if (ts.Hours > 0)
                        {
                            uptime = string.Format("{0} hour{1}, {2} minute{3}",
                                                   ts.Hours, ts.Hours == 1 ? "" : "s",
                                                   ts.Minutes, ts.Minutes == 1 ? "" : "s");
                        }
                        else if (ts.Minutes > 0)
                        {
                            uptime = string.Format("{0} minute{1}",
                                                   ts.Minutes, ts.Minutes == 1 ? "" : "s");
                        }
                        else
                        {
                            uptime = string.Format("{0} second{1}",
                                                   ts.Seconds, ts.Seconds == 1 ? "" : "s");
                        }
                        systemInfo.Uptime = uptime;
                    }

                    //foreach (var prop in obj.Properties)
                    //{
                    //    if (prop.Name == "OSArchitecture" && obj["OSArchitecture"] != null)
                    //        systemInfo.WindowsArchitecture = obj["OSArchitecture"].ToString();
                    //}
                }

                //if (systemInfo.WindowsArchitecture == null)
                //{
                WindowsArchitecture = "32-bit";
                query    = new ObjectQuery("SELECT Name,VariableValue FROM Win32_Environment");
                searcher = new ManagementObjectSearcher(sc, query);

                foreach (ManagementObject obj in searcher.Get())
                {
                    if (obj["Name"] != null && obj["Name"].ToString() == "PROCESSOR_ARCHITECTURE" && obj["VariableValue"] != null && obj["VariableValue"].ToString().ToUpper() == "AMD64")
                    {
                        WindowsArchitecture = "64-bit";
                        break;
                    }
                    else if (obj["Name"] != null && obj["Name"].ToString() == "PROCESSOR_ARCHITEW6432 " && obj["VariableValue"] != null && obj["VariableValue"].ToString().ToUpper() == "AMD64")
                    {
                        WindowsArchitecture = "64-bit";
                        break;
                    }
                }
                //}


                if (systemInfo.WindowsVersionNumber.StartsWith("5.0") || systemInfo.WindowsVersionNumber.StartsWith("5.2"))
                {
                    query = new ObjectQuery("SELECT CurrentClockSpeed FROM Win32_Processor");
                }
                else
                {
                    query = new ObjectQuery("SELECT CurrentClockSpeed,NumberOfLogicalProcessors FROM Win32_Processor");
                }
                searcher = new ManagementObjectSearcher(sc, query);
                UInt32 clockSpeed            = 0;
                UInt32 numberOfProcessors    = 1;
                bool   isLogicalCpuSupported = false;
                foreach (ManagementObject obj in searcher.Get())
                {
                    if (obj["CurrentClockSpeed"] != null)
                    {
                        clockSpeed = (UInt32)obj["CurrentClockSpeed"];
                    }
                    if (systemInfo.WindowsVersionNumber.StartsWith("5.0") || systemInfo.WindowsVersionNumber.StartsWith("5.2"))
                    {
                        break;
                    }
                    else if (obj["NumberOfLogicalProcessors"] != null)
                    {
                        isLogicalCpuSupported = true;
                    }
                    //foreach (var prop in obj.Properties)
                    //{
                    //    if (prop.Name == "NumberOfLogicalProcessors" && obj["NumberOfLogicalProcessors"] != null)
                    //    {
                    //        isLogicalCpuSupported = true;
                    //        break;
                    //    }
                    //}
                    break;
                }


                if (isLogicalCpuSupported == true)
                {
                    query = new ObjectQuery("SELECT Manufacturer,Model,Name,NumberOfLogicalProcessors,NumberOfProcessors FROM Win32_ComputerSystem");
                }
                else
                {
                    query = new ObjectQuery("SELECT Manufacturer,Model,Name,NumberOfProcessors FROM Win32_ComputerSystem");
                }
                searcher = new ManagementObjectSearcher(sc, query);
                foreach (ManagementObject obj in searcher.Get())
                {
                    if (obj["Manufacturer"] != null)
                    {
                        systemInfo.ComputerManufacturer = obj["Manufacturer"].ToString();
                    }
                    if (obj["Model"] != null)
                    {
                        systemInfo.ComputerModel = obj["Model"].ToString();
                    }
                    if (obj["Name"] != null)
                    {
                        systemInfo.ComputerName = obj["Name"].ToString();
                    }
                    if (isLogicalCpuSupported == true && obj["NumberOfLogicalProcessors"] != null)
                    {
                        numberOfProcessors = (UInt32)obj["NumberOfLogicalProcessors"];
                    }
                    else if (isLogicalCpuSupported == false && obj["NumberOfProcessors"] != null)
                    {
                        numberOfProcessors = (UInt32)obj["NumberOfProcessors"];
                    }
                    else
                    {
                        numberOfProcessors = 1;
                    }
                }
                systemInfo.Processor = string.Format("{0} Core{1} @ {2:0.#} {3}",
                                                     numberOfProcessors, numberOfProcessors == 1 ? "" : "s",
                                                     clockSpeed > 1000 ? (double)clockSpeed / 1000.0 : clockSpeed,
                                                     clockSpeed > 1000 ? "GHz" : "MHz");


                query    = new ObjectQuery("SELECT SerialNumber FROM Win32_SystemEnclosure");
                searcher = new ManagementObjectSearcher(sc, query);
                foreach (ManagementObject obj in searcher.Get())
                {
                    systemInfo.ComputerSerialNumber = (obj["SerialNumber"] != null) ? obj["SerialNumber"].ToString() : string.Empty;
                    break;
                }


                query    = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
                searcher = new ManagementObjectSearcher(sc, query);
                UInt64 totalMemory = 0;
                foreach (ManagementObject m in searcher.Get())
                {
                    if (m["Capacity"] != null)
                    {
                        totalMemory += (UInt64)m["Capacity"];
                    }
                }
                systemInfo.Memory = RemoteAdmin.ConvertBytesToString(totalMemory);


                // Determine computer type:
                if (!string.IsNullOrEmpty(systemInfo.ComputerType) && systemInfo.ComputerType == "3")
                {
                    systemInfo.ComputerType = "Server";
                }
                else
                {
                    systemInfo.ComputerType = "Desktop";
                }

                if (systemInfo.ComputerManufacturer == "VMware, Inc." || (systemInfo.ComputerManufacturer == "Xen" && systemInfo.ComputerModel == "HVM domU"))
                {
                    if (systemInfo.ComputerType == "Server")
                    {
                        systemInfo.ComputerType = "Server (Virtual Machine)";
                    }
                    else
                    {
                        systemInfo.ComputerType = "Virtual Machine";
                    }
                }
                query    = new ObjectQuery("SELECT BatteryStatus FROM Win32_Battery");
                searcher = new ManagementObjectSearcher(sc, query);
                foreach (ManagementObject m in searcher.Get())
                {
                    systemInfo.ComputerType = "Laptop / Portable";
                    break;
                }

                taskResult.DidTaskSucceed = true;



                query    = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True");
                searcher = new ManagementObjectSearcher(sc, query);
                foreach (ManagementObject obj in searcher.Get())
                {
                    string[] ipAddresses = (string[])(obj["IPAddress"]);
                    systemInfo.IpAddresses = ipAddresses.FirstOrDefault(s => s.Contains('.'));
                }
            }
            catch
            {
                taskResult.DidTaskSucceed = false;
            }

            systemInfo.IsRebootRequired = GetSysRebootState();

            return(systemInfo);
        }
        public static RemoteSystemSpecs GetCpuRamDetails()
        {
            var systemSpecs   = new RemoteSystemSpecs();
            var memoryModules = new List <RemoteMemoryModule>();

            systemSpecs.Result = new TaskResult();
            systemSpecs.Result.DidTaskSucceed = true;

            var op = new ConnectionOptions();

            if (GlobalVar.UseAlternateCredentials)
            {
                op.Username  = GlobalVar.AlternateUsername;
                op.Password  = GlobalVar.AlternatePassword;
                op.Authority = $"NTLMDOMAIN:{GlobalVar.AlternateDomain}";
            }
            var sc       = new ManagementScope($@"\\{RemoteSystemInfo.TargetComputer}\root\CIMV2", op);
            var query    = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
            var searcher = new ManagementObjectSearcher(sc, query);

            // Determine the number of physical memory slots.
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    systemSpecs.MemorySlotsTotal += (m["MemoryDevices"] != null) ? (UInt16)m["MemoryDevices"] : (UInt16)0;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine the capacity, speed, and location of each memory module.
            query    = new ObjectQuery("SELECT DeviceLocator,Capacity,Speed FROM Win32_PhysicalMemory");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    string location = (m["DeviceLocator"] != null) ? m["DeviceLocator"].ToString() : string.Empty;
                    UInt64 capacity = (m["Capacity"] != null) ? (UInt64)m["Capacity"] : (UInt64)0;
                    UInt32 speed    = (m["Speed"] != null) ? (UInt32)m["Speed"] : (UInt32)0;
                    memoryModules.Add(new RemoteMemoryModule {
                        MemoryLocation = location, MemoryCapacityBytes = capacity, MemorySpeed = speed
                    });
                    systemSpecs.MemorySpeedString = speed.ToString() + " MHz";
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine how many memory slots are in use (This will be the size of the memoryModules List).
            systemSpecs.MemorySlotsInUse = memoryModules.Count;


            // Determine amount of memory reserved by the BIOS.
            query    = new ObjectQuery("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    systemSpecs.MemoryTotalBytes = (m["TotalPhysicalMemory"] != null) ? (UInt64)m["TotalPhysicalMemory"] : 0;
                    UInt64 totalPhysicalMemory = 0;
                    foreach (RemoteMemoryModule mem in memoryModules)
                    {
                        totalPhysicalMemory += mem.MemoryCapacityBytes;
                    }
                    systemSpecs.MemoryTotalString = RemoteAdmin.ConvertBytesToString(totalPhysicalMemory);
                    UInt64 biosReserved = (systemSpecs.MemoryTotalBytes > 0 && totalPhysicalMemory > 0) ? totalPhysicalMemory - systemSpecs.MemoryTotalBytes : 0;
                    systemSpecs.MemoryBiosReservedString = RemoteAdmin.ConvertBytesToString(biosReserved);
                    break;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine amount of memory in use / available.
            // Determine Memory Committed and Commit Limit.
            // Determine Paged and Non-Paged pool.
            query    = new ObjectQuery("SELECT AvailableBytes,CommittedBytes,CommitLimit,PoolNonpagedBytes,PoolPagedBytes FROM Win32_PerfRawData_PerfOS_Memory");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    UInt64 availableBytes = (m["AvailableBytes"] != null) ? (UInt64)m["AvailableBytes"] : 0;
                    UInt64 inUseBytes     = systemSpecs.MemoryTotalBytes - availableBytes;

                    UInt64 committedBytes   = (m["CommittedBytes"] != null) ? (UInt64)m["CommittedBytes"] : 0;
                    UInt64 commitLimitBytes = (m["CommitLimit"] != null) ? (UInt64)m["CommitLimit"] : 0;

                    UInt64 pagedPoolBytes    = (m["PoolPagedBytes"] != null) ? (UInt64)m["PoolPagedBytes"] : 0;
                    UInt64 nonPagedPoolBytes = (m["PoolNonpagedBytes"] != null) ? (UInt64)m["PoolNonpagedBytes"] : 0;

                    systemSpecs.MemoryAvailableString    = RemoteAdmin.ConvertBytesToString(availableBytes);
                    systemSpecs.MemoryInUseString        = RemoteAdmin.ConvertBytesToString(inUseBytes);
                    systemSpecs.MemoryCommittedString    = RemoteAdmin.ConvertBytesToString(committedBytes);
                    systemSpecs.MemoryCommitLimitString  = RemoteAdmin.ConvertBytesToString(commitLimitBytes);
                    systemSpecs.MemoryPagedPoolString    = RemoteAdmin.ConvertBytesToString(pagedPoolBytes);
                    systemSpecs.MemoryNonPagedPoolString = RemoteAdmin.ConvertBytesToString(nonPagedPoolBytes);
                    break;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine CPU product name, max clock speed, number of cpu sockets, number of cores, number of logical processors.
            query    = new ObjectQuery("SELECT * FROM Win32_Processor");
            searcher = new ManagementObjectSearcher(sc, query);
            int  i = 0;
            bool isLogicalCpuWmiSupported = false;

            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    ++i;

                    UInt32 clockSpeed = (m["MaxClockSpeed"] != null) ? (UInt32)m["MaxClockSpeed"] : (UInt32)0;
                    systemSpecs.CpuClockSpeed = string.Format("{0:0.00} {1}",
                                                              clockSpeed > 1000 ? (double)clockSpeed / 1000.0 : clockSpeed,
                                                              clockSpeed > 1000 ? "GHz" : "MHz");

                    // Get CPU model/name then remove extra whitespace and "(R)" from the middle of the string.
                    string cpuName        = (m["Name"] != null) ? m["Name"].ToString() : string.Empty;
                    var    cpuNameTrimmed = new StringBuilder();
                    for (int j = 0; j < cpuName.Length; ++j)
                    {
                        if (cpuName[j] == ' ')
                        {
                            cpuNameTrimmed.Append(cpuName[j]);
                            while (++j < cpuName.Length && cpuName[j] == ' ')
                            {
                                continue;
                            }
                            --j;
                        }
                        else if (cpuName[j] == '(' && j <= cpuName.Length - 2 && cpuName[j + 1] == 'R' && cpuName[j + 2] == ')')
                        {
                            j += 2;
                        }
                        else
                        {
                            cpuNameTrimmed.Append(cpuName[j]);
                        }
                    }
                    systemSpecs.CpuName = cpuNameTrimmed.ToString();

                    // Determine if target OS is Server 2003.  If so, you cannot determine the number of cores and physical processors.  Only the number of logical processors can be determined.
                    foreach (var prop in m.Properties)
                    {
                        if (prop.Name == "NumberOfLogicalProcessors")
                        {
                            isLogicalCpuWmiSupported = true;
                            break;
                        }
                    }

                    if (isLogicalCpuWmiSupported)
                    {
                        systemSpecs.CpuNumberOfLogicalProcessors += (m["NumberOfLogicalProcessors"] != null) ? (UInt32)m["NumberOfLogicalProcessors"] : (UInt32)0;
                        systemSpecs.CpuNumberOfCores             += (m["NumberOfCores"] != null) ? (UInt32)m["NumberOfCores"] : (UInt32)0;
                    }
                }
                // If the number of logical processors is the same as the number of cores.  Set the value to 0 so it does not display on the System Info window.
                if (systemSpecs.CpuNumberOfLogicalProcessors == systemSpecs.CpuNumberOfCores)
                {
                    systemSpecs.CpuNumberOfLogicalProcessors = 0;
                }

                if (isLogicalCpuWmiSupported)
                {
                    systemSpecs.CpuNumberOfSockets = (UInt32)i;
                }
                else
                {
                    // If the target OS is Server 2003, the number of cores and physical processors are incorrectly reported through WMI.  Only the total number of logical CPUs can be determined.
                    systemSpecs.CpuNumberOfSockets           = (UInt32)0;
                    systemSpecs.CpuNumberOfCores             = (UInt32)0;
                    systemSpecs.CpuNumberOfLogicalProcessors = (UInt32)i;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine L1 / L2 / L3 cache size.
            query    = new ObjectQuery("SELECT MaxCacheSize,Purpose FROM Win32_CacheMemory");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    string purpose   = (m["Purpose"] != null) ? m["Purpose"].ToString().ToUpper() : string.Empty;
                    UInt32 cacheSize = (m["MaxCacheSize"] != null) ? (UInt32)m["MaxCacheSize"] : (UInt32)0;
                    if (purpose.Contains("L1"))
                    {
                        systemSpecs.L1CacheSize = RemoteAdmin.ConvertBytesToString(cacheSize * 1024);
                    }
                    else if (purpose.Contains("L2"))
                    {
                        systemSpecs.L2CacheSize = RemoteAdmin.ConvertBytesToString(cacheSize * 1024);
                    }
                    else if (purpose.Contains("L3"))
                    {
                        systemSpecs.L3CacheSize = RemoteAdmin.ConvertBytesToString(cacheSize * 1024);
                    }
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine number of running processes.
            query    = new ObjectQuery("SELECT NumberOfProcesses FROM Win32_OperatingSystem");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    systemSpecs.CpuNumberOfProcesses = (m["NumberOfProcesses"] != null) ? (UInt32)m["NumberOfProcesses"] : 0;
                    break;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            // Determine number of threads and handles.
            query    = new ObjectQuery("SELECT HandleCount,ThreadCount FROM Win32_PerfRawData_PerfProc_Process WHERE Name = '_Total'");
            searcher = new ManagementObjectSearcher(sc, query);
            try
            {
                foreach (ManagementObject m in searcher.Get())
                {
                    systemSpecs.CpuHandleCount = (m["HandleCount"] != null) ? (UInt32)m["HandleCount"] : 0;
                    systemSpecs.CpuThreadCount = (m["ThreadCount"] != null) ? (UInt32)m["ThreadCount"] : 0;
                    break;
                }
            }
            catch
            {
                systemSpecs.Result.DidTaskSucceed = false;
            }


            return(systemSpecs);
        }