【发布时间】:2021-06-06 23:43:19
【问题描述】:
我正在尝试使用 Power shell 运行此命令:
Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = 'Microsoft Bluetooth LE Enumerator'" | select -ExpandProperty driverversion
当手动运行它时,它工作正常,但是当我通过代码运行它时,我收到这个错误:
Get-WmiObject:找不到接受参数“Microsoft Bluetooth LE Enumerator”的位置参数。
在 line:1 char:1
+ Get-WmiObject Win32_PnPSignedDriver -filter DeviceName = 'Microsoft B ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
这是我的代码:
public static string PlatformModelCommand => $ "Get-WmiObject Win32_PnPSignedDriver -filter \"DeviceName = \'Microsoft Bluetooth LE Enumerator\'\" | select -ExpandProperty driverversion";
string projectName = RunCommandUtil.RunPsProcess(PlatformModelCommand);
public static string RunPsProcess(string arguments)
{
return RunProcess("powershell.exe", arguments);
}
public static string RunProcess(string fileName, string arguments) {
Process process = new Process();
process.StartInfo.FileName = fileName;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = arguments;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
var output = process.StandardOutput.ReadToEnd();
output += process.StandardError.ReadToEnd();
process.WaitForExit();
return output;
}
【问题讨论】:
-
RunPsProcess是做什么的?你为什么在不使用RunProcess的时候发布它的来源?另外,您是否在进程内运行 PowerShell? -
对不起,忘记添加一个函数,编辑我的问题
标签: c# powershell wmic