Skip to content

Commit

Permalink
【MachineInfo】在 Windows 系统中,如果调用 wmic 失败,则尝试调用 PowerShell 。
Browse files Browse the repository at this point in the history
  • Loading branch information
Soar360 authored and nnhy committed Aug 1, 2024
1 parent 3defcbb commit 6559919
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,11 @@ private void LoadWindowsInfo()
// OSVersion = GetInfo("Win32_OperatingSystem", "Version");
#else
var os = ReadWmic("os", "Caption", "Version");
if (os != null)
if (os == null || os.Count == 0)
{
os = ReadPowerShell("Get-WmiObject Win32_OperatingSystem | Select-Object Caption, Version | ConvertTo-Json");
}
if (os is { Count: > 0 })
{
if (os.TryGetValue("Caption", out str)) OSName = str.TrimStart("Microsoft").Trim();
if (os.TryGetValue("Version", out str)) OSVersion = str;
Expand Down Expand Up @@ -914,6 +918,25 @@ private static Boolean TryRead(String fileName, [NotNullWhen(true)] out String?
return str.SplitAsDictionary(":", "\n", true);
}

/// <summary>
/// 通过 PowerShell 命令读取信息
/// </summary>
public static IDictionary<string, string> ReadPowerShell(string command)
{
var dic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var args = $"-Command \"{command}\"";
var str = Execute("powershell.exe", args) ?? string.Empty;

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / test

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context

Check failure on line 929 in NewLife.Core/Common/MachineInfo.cs

View workflow job for this annotation

GitHub Actions / build-publish

The name 'Execute' does not exist in the current context
if (!string.IsNullOrWhiteSpace(str))
{
foreach (var item in str.DecodeJson()!)
{
dic[item.Key] = item.Value?.ToString() ?? string.Empty;
}
}
return dic;
}

/// <summary>通过WMIC命令读取信息</summary>
/// <param name="type"></param>
/// <param name="keys"></param>
Expand Down

0 comments on commit 6559919

Please sign in to comment.