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.