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.Word" Version="2.7.5.2" />
|
||||
<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="MySqlBackup.NET.MySqlConnector" Version="2.3.8" />
|
||||
<PackageReference Include="NewLife.Redis" Version="6.0.2024.1006" />
|
||||
|
||||
@ -190,6 +190,7 @@ public static class ComputerUtil
|
||||
else
|
||||
{
|
||||
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"));
|
||||
}
|
||||
return cpuRates;
|
||||
@ -219,6 +220,7 @@ public static class ComputerUtil
|
||||
else
|
||||
{
|
||||
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);
|
||||
if (outputArr.Length == 2)
|
||||
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()
|
||||
{
|
||||
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 lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (lines.Length <= 0) return metrics;
|
||||
@ -415,7 +418,7 @@ public class ShellUtil
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// windows系统命令
|
||||
/// windows CMD 系统命令
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="args"></param>
|
||||
@ -436,6 +439,25 @@ public class ShellUtil
|
||||
}
|
||||
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
|
||||
@ -467,7 +489,7 @@ public class ShellHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Windows 系统命令
|
||||
/// Windows CMD 系统命令
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="args"></param>
|
||||
@ -488,4 +510,23 @@ public class ShellHelper
|
||||
}
|
||||
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