Merge pull request 'ShellUtil、ShellHelper 增加 PowerShell 用以增强 Cmd 命令 NET.8 可用' (#164) from IMaster/Admin.NET.Pro:template into main
Reviewed-on: http://101.43.53.74:3000/Admin.NET/Admin.NET.Pro/pulls/164
This commit is contained in:
commit
2170428932
@ -29,6 +29,7 @@
|
|||||||
<PackageReference Include="Magicodes.IE.Pdf" Version="2.7.5.2" />
|
<PackageReference Include="Magicodes.IE.Pdf" Version="2.7.5.2" />
|
||||||
<PackageReference Include="Magicodes.IE.Word" Version="2.7.5.2" />
|
<PackageReference Include="Magicodes.IE.Word" Version="2.7.5.2" />
|
||||||
<PackageReference Include="MailKit" Version="4.8.0" />
|
<PackageReference Include="MailKit" Version="4.8.0" />
|
||||||
|
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.4.6" />
|
||||||
<PackageReference Include="MQTTnet" Version="4.3.7.1207" />
|
<PackageReference Include="MQTTnet" Version="4.3.7.1207" />
|
||||||
<PackageReference Include="MySqlBackup.NET.MySqlConnector" Version="2.3.8" />
|
<PackageReference Include="MySqlBackup.NET.MySqlConnector" Version="2.3.8" />
|
||||||
<PackageReference Include="NewLife.Redis" Version="6.0.2024.1006" />
|
<PackageReference Include="NewLife.Redis" Version="6.0.2024.1006" />
|
||||||
|
|||||||
@ -190,6 +190,7 @@ public static class ComputerUtil
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
string output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
|
string output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
|
||||||
|
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_Processor | Measure-Command {Start-Sleep -Seconds 1}");
|
||||||
cpuRates.AddRange(output.Replace("LoadPercentage", string.Empty).Trim().Split("\r\r\n"));
|
cpuRates.AddRange(output.Replace("LoadPercentage", string.Empty).Trim().Split("\r\r\n"));
|
||||||
}
|
}
|
||||||
return cpuRates;
|
return cpuRates;
|
||||||
@ -219,6 +220,7 @@ public static class ComputerUtil
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
string output = ShellUtil.Cmd("wmic", "OS get LastBootUpTime/Value");
|
string output = ShellUtil.Cmd("wmic", "OS get LastBootUpTime/Value");
|
||||||
|
// string output = ShellUtil.PowerShell("(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime");
|
||||||
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (outputArr.Length == 2)
|
if (outputArr.Length == 2)
|
||||||
runTime = DateTimeUtil.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
runTime = DateTimeUtil.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||||
@ -330,6 +332,7 @@ public class MemoryMetricsClient
|
|||||||
public static MemoryMetrics GetWindowsMetrics()
|
public static MemoryMetrics GetWindowsMetrics()
|
||||||
{
|
{
|
||||||
string output = ShellUtil.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
string output = ShellUtil.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
||||||
|
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_OperatingSystem | Select FreePhysicalMemory, TotalVisibleMemorySize");
|
||||||
var metrics = new MemoryMetrics();
|
var metrics = new MemoryMetrics();
|
||||||
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (lines.Length <= 0) return metrics;
|
if (lines.Length <= 0) return metrics;
|
||||||
@ -415,7 +418,7 @@ public class ShellUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// windows系统命令
|
/// windows CMD 系统命令
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
@ -436,6 +439,25 @@ public class ShellUtil
|
|||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Windows POWERSHELL 系统命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string PowerShell(string script)
|
||||||
|
{
|
||||||
|
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||||
|
PowerShellInstance.AddScript(script);
|
||||||
|
var PSOutput = PowerShellInstance.Invoke();
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
foreach (var outputItem in PSOutput)
|
||||||
|
{
|
||||||
|
output.AppendLine(outputItem.BaseObject.ToString());
|
||||||
|
}
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ShellHelper
|
public class ShellHelper
|
||||||
@ -467,7 +489,7 @@ public class ShellHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Windows 系统命令
|
/// Windows CMD 系统命令
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fileName"></param>
|
/// <param name="fileName"></param>
|
||||||
/// <param name="args"></param>
|
/// <param name="args"></param>
|
||||||
@ -488,4 +510,23 @@ public class ShellHelper
|
|||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Windows POWERSHELL 系统命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="script"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string PowerShell(string script)
|
||||||
|
{
|
||||||
|
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||||
|
PowerShellInstance.AddScript(script);
|
||||||
|
var PSOutput = PowerShellInstance.Invoke();
|
||||||
|
|
||||||
|
var output = new StringBuilder();
|
||||||
|
foreach (var outputItem in PSOutput)
|
||||||
|
{
|
||||||
|
output.AppendLine(outputItem.BaseObject.ToString());
|
||||||
|
}
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user