0

I am developing a UWP app with .NET Standard class library for Utility logic. In this application I need to collect some metadata related to operating PC

I have come up with following structure to read the data

 public static void LoadSystemInfo(this Payload payload)
        {
            payload.SystemInfo = new SystemInfo
            {
                Machine = new Machine
                {
                    SerialNumber = SystemInfo("SerialNumber"),
                    UuId = SystemInfo("UuId"),
                },
                HostName = SystemInfo("HostName"),
                OsVersion = SystemInfo("OsVersion"),
                OsManufacturer = SystemInfo("OsManufacturer"),
                DeviceId = SystemInfo("DeviceId"),
                SystemManufacturer = SystemInfo("SystemManufacturer"),
                SystemModel = SystemInfo("SystemModel"),
                SystemType = SystemInfo("SystemType"),
                SystemLocale = SystemInfo("SystemLocale"),
                TimeZone = SystemInfo("TimeZone"),
                TotalPhysicalMemory = SystemInfo("TotalPhysicalMemory"),
                AvailablePhysicalMemory = SystemInfo("AvailablePhysicalMemory"),
            };
        }
        private static string  SystemInfo(string key)
        {
            switch (key)
            {
                case "SerialNumber":
                    return GetMotherBoardId();
                case "UuId":
                    return "";
                case "HostName":
                    return "";
                case "OsVersion":
                    return "";
                case "OsManufacturer":
                    return "";
                case "DeviceId":
                    return "";
                case "SystemManufacturer":
                    return "";
                case "SystemModel":
                    return "";
                case "SystemType":
                    return "";
                case "SystemLocale":
                    return "";
                case "TimeZone":
                    return "";
                case "TotalPhysicalMemory":
                    break;
                case "AvailablePhysicalMemory":
                    return "";
                default:
                    return $"Missing Case for {key}";
            }
            return null;
        }

I tried to get Get Mother Board Id as below

public static string GetMotherBoardId()
        {
            string mbInfo = string.Empty;
            ManagementScope scope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
            scope.Connect();
            ManagementObject wmiClass = new ManagementObject(scope,
                new ManagementPath("Win32_BaseBoard.Tag=\"Base Board\""), new ObjectGetOptions());

            foreach (PropertyData propData in wmiClass.Properties)
            {
                if (propData.Name == "SerialNumber")
                    mbInfo = $"{propData.Name,-25}{Convert.ToString(propData.Value)}";
            }

            return mbInfo;
        }

That throws error as System.Management currently is only supported for Windows desktop applications.

How do I get all the above properties from local PC which is running my UWP app.

Also tried powershell script like below

using (PowerShell powerShellInstance = PowerShell.Create())
                    {
                        powerShellInstance.AddCommand("get-wmiobject");
                        powerShellInstance.AddParameter("class", "Win32_ComputerSystemProduct");
                        //powerShellInstance.AddScript(
                        //    "get-wmiobject Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID");
                        Collection<PSObject> psOutput = powerShellInstance.Invoke();

                    }

that gives error as below

The term 'get-wmiobject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Update


Followed this article https://learn.microsoft.com/en-us/windows/desktop/wmisdk/retrieving-an-instance and it still fails

string Namespace = @"root\cimv2";
string className = "Win32_LogicalDisk";

CimInstance myDrive = new CimInstance(className, Namespace);
CimSession mySession = CimSession.Create("localhost");
CimInstance searchInstance = mySession.GetInstance(Namespace, myDrive);

Throws following error

Access to a CIM resource was not available to the client.

When I try this

  ManagementObjectSearcher mgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
                    ManagementObjectCollection colDisks = mgmtObjSearcher.Get();

I get this error

System.Management currently is only supported for Windows desktop applications.

When I try this

string command = "Get-Command Write-Output";
                    using (var ps = PowerShell.Create())
                    {
                        var results = ps.AddScript(command).Invoke();
                        foreach (var result in results)
                        {
                           Console.WriteLine(result.ToString());
                        }
                        ps.Commands.Clear();
                    }

I get this error

An error occurred while creating the pipeline. --> Method not found: 'System.Text.StringBuilder System.Text.StringBuilder.Append(System.Text.StringBuilder)'.

Any help i greatly appreciated.

HaBo
  • 13,999
  • 36
  • 114
  • 206

1 Answers1

0

I filled your switch based on data from microsoft toolkit SystemInformation helper, hostname from NetworkInformation class, timezone from TimeZone.CurrentTimeZone and SystemLocale from HomeGeographicRegion

I haven't filled TotalPhysicalMemory because in UWP you can not get the system RAM information since it is sandbox. But you can get app memory usage limit from MemoryManager

For SerialNumber you can use SystemIdentification.GetSystemIdForPublisher() will receive raw data as buffer you can process it.

private static string SystemInfo(string key)
{
   EasClientDeviceInformation deviceInfo = new EasClientDeviceInformation();
   switch (key)
   {
     case "SerialNumber":
        break ;
     case "UuId":
        return deviceInfo.Id.ToString();
     case "HostName":
        var hostNames = NetworkInformation.GetHostNames();
        return  hostNames.FirstOrDefault(name => name.Type == HostNameType.DomainName)?.DisplayName ?? "???";                     
     case "OsVersion":
        ulong version = ulong.Parse(AnalyticsInfo.VersionInfo.DeviceFamilyVersion);
        return  ((version & 0xFFFF000000000000L) >> 48).ToString()+"."+
          ((version & 0x0000FFFF00000000L) >> 32).ToString()+"."+((version & 0x00000000FFFF0000L) >> 16).ToString()+"."+(version & 0x000000000000FFFFL).ToString();
     case "OsManufacturer":
        return deviceInfo.OperatingSystem;
     case "DeviceId":
        return "";
     case "SystemManufacturer":
        return deviceInfo.SystemManufacturer; 
     case "SystemModel":
        return deviceInfo.SystemProductName;
     case "SystemType":
        return Package.Current.Id.Architecture.ToString();
     case "SystemLocale":
        return Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion;
     case "TimeZone":
        return TimeZone.CurrentTimeZone.StandardName.ToString();
     case "TotalPhysicalMemory":
        break;
     case "AvailablePhysicalMemory":
        return ((float)MemoryManager.AppMemoryUsageLimit / 1024 / 1024).ToString();
     default:
        return $"Missing Case for {key}";
    }
  return null;
}
Vignesh
  • 1,824
  • 2
  • 10
  • 27
  • Thanks @Vignesh, still struggling to get SerialNumber and ProductId. Serial Number through Powershell script get-ciminstance win32_bios | format-list serialnumber and ProductID from command prompt by typing systeminfo. – HaBo Dec 19 '18 at 06:43
  • UWP has some system level api's restricted. If you really need to access those you can create a [appservice][1] to run a small wpf snippet and get data to UWP app. [1] : https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/AppServiceBridgeSample – Vignesh Dec 19 '18 at 12:20