在 PowerShell 中执行 C# 代码导致错误 - 验证是否加载了包含此类型的程序集
Executing C# code in PowerShell results in an error - verify that the assembly containing this type is loaded
我正在尝试在 PowerShell 中执行以下代码,但出现以下错误(请参考屏幕截图)。我已经尝试了很多方法来处理这个错误,但没有运气。
我参考了这些文章:
- whosebug.com/questions/12923074/how-to-load-assemblies-in-powershell
- whosebug.com/questions/64266974/powershell-5-1-cannot-find-type-mytype-verify-that-the-assembly-containing
- hanselman.com/blog/watir-for-net-watin-approaches-08-release-and-automating-ie-from-powershell
- https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/using-using-namespace
你能帮我解决这个问题吗?
提前致谢!
$id = get-random
$Source = @”
using OpenHardwareMonitor.Hardware;
using System;
namespace GetCpuTemperatureExample
{
public class GetCpuTemerature$id : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
public static double GetSystemInfo(GetCpuTemerature$id updateVisitor)
{
double result=0;
try
{
Computer computer = new Computer();
computer.Open();
computer.CPUEnabled = true;
computer.Accept(updateVisitor);
for (int i = 0; i < computer.Hardware.Length; i++)
{
double sum = 0;
int countOfCPU = 0;
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
{
if (computer.Hardware[i].Sensors[j].Name.Contains("Core"))
{
sum += Convert.ToDouble(computer.Hardware[i].Sensors[j].Value.ToString());
countOfCPU++;
}
}
}
result=(sum / countOfCPU);
}
}
computer.Close();
}
catch (Exception ex)
{
}
return result;
}
}
}
"@
$referencedAssemblies = "C:\Program Files\OpenHardwareMonitorLib.dll"
Import-Module 'C:\Program Files\OpenHardwareMonitorLib.dll'
Add-Type -Path $referencedAssemblies
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.ReferencedAssemblies.Add($referencedAssemblies)
Add-Type -TypeDefinition $Source -CompilerParameters $cpar -Language CSharp
$GetCpuTemeratureObj = New-Object GetCpuTemerature$id
$CPUTemp = [GetCpuTemperatureExample.GetCpuTemerature]::GetSystemInfo($GetCpuTemeratureObj)
echo $CPUTemp
你需要在这两种情况下使用全类型名称(包括命名空间和末尾的ID):
$GetCpuTemeratureObj = New-Object "GetCpuTemperatureExample.GetCpuTemerature$id"
$CPUTemp = ([type]"GetCpuTemperatureExample.GetCpuTemerature$id")::GetSystemInfo($GetCpuTemeratureObj)
echo $CPUTemp
我正在尝试在 PowerShell 中执行以下代码,但出现以下错误(请参考屏幕截图)。我已经尝试了很多方法来处理这个错误,但没有运气。
我参考了这些文章:
- whosebug.com/questions/12923074/how-to-load-assemblies-in-powershell
- whosebug.com/questions/64266974/powershell-5-1-cannot-find-type-mytype-verify-that-the-assembly-containing
- hanselman.com/blog/watir-for-net-watin-approaches-08-release-and-automating-ie-from-powershell
- https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/using-using-namespace
你能帮我解决这个问题吗?
提前致谢!
$id = get-random
$Source = @”
using OpenHardwareMonitor.Hardware;
using System;
namespace GetCpuTemperatureExample
{
public class GetCpuTemerature$id : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
public static double GetSystemInfo(GetCpuTemerature$id updateVisitor)
{
double result=0;
try
{
Computer computer = new Computer();
computer.Open();
computer.CPUEnabled = true;
computer.Accept(updateVisitor);
for (int i = 0; i < computer.Hardware.Length; i++)
{
double sum = 0;
int countOfCPU = 0;
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
{
if (computer.Hardware[i].Sensors[j].Name.Contains("Core"))
{
sum += Convert.ToDouble(computer.Hardware[i].Sensors[j].Value.ToString());
countOfCPU++;
}
}
}
result=(sum / countOfCPU);
}
}
computer.Close();
}
catch (Exception ex)
{
}
return result;
}
}
}
"@
$referencedAssemblies = "C:\Program Files\OpenHardwareMonitorLib.dll"
Import-Module 'C:\Program Files\OpenHardwareMonitorLib.dll'
Add-Type -Path $referencedAssemblies
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.ReferencedAssemblies.Add($referencedAssemblies)
Add-Type -TypeDefinition $Source -CompilerParameters $cpar -Language CSharp
$GetCpuTemeratureObj = New-Object GetCpuTemerature$id
$CPUTemp = [GetCpuTemperatureExample.GetCpuTemerature]::GetSystemInfo($GetCpuTemeratureObj)
echo $CPUTemp
你需要在这两种情况下使用全类型名称(包括命名空间和末尾的ID):
$GetCpuTemeratureObj = New-Object "GetCpuTemperatureExample.GetCpuTemerature$id"
$CPUTemp = ([type]"GetCpuTemperatureExample.GetCpuTemerature$id")::GetSystemInfo($GetCpuTemeratureObj)
echo $CPUTemp