C# 如何在另一个字符串中包含字符串

C# 如何在另一个字符串中包含字符串,c#,powershell,wmic,C#,Powershell,Wmic,我正在尝试使用Power shell运行此命令: Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = 'Microsoft Bluetooth LE Enumerator'" | select -ExpandProperty driverversion 手动运行时,它工作正常,但当我通过代码运行时,收到以下错误: Get-WMIOObject:找不到接受参数“Microsoft Bluetooth LE枚举器”

我正在尝试使用Power shell运行此命令:

Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = 'Microsoft Bluetooth LE Enumerator'" | select -ExpandProperty driverversion
手动运行时,它工作正常,但当我通过代码运行时,收到以下错误:

Get-WMIOObject:找不到接受参数“Microsoft Bluetooth LE枚举器”的位置参数。
第1行字符:1
+获取WmiObject Win32\u PnPSignedDriver-filter DeviceName='Microsoft B…
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidArgument:(:)[Get-WMIOObject],ParameterBindingException +FullyQualifiedErrorId:PositionParameterNotFound,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;
}

如果在Powershell中运行此操作:

Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = \'Microsoft Bluetooth LE Enumerator\'" | select -ExpandProperty driverversion
您还将得到一个错误:

Get-WmiObject : Invalid query "select * from Win32_PnPSignedDriver where DeviceName = \'Microsoft Bluetooth LE Enumerat
or\'"
At line:1 char:1
+ Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = \'Microsoft ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
我不知道你为什么把
\
放在单引号之前


但也许您应该使用,这似乎更适合此任务?

如果您在Powershell中运行此功能:

Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = \'Microsoft Bluetooth LE Enumerator\'" | select -ExpandProperty driverversion
您还将得到一个错误:

Get-WmiObject : Invalid query "select * from Win32_PnPSignedDriver where DeviceName = \'Microsoft Bluetooth LE Enumerat
or\'"
At line:1 char:1
+ Get-WmiObject Win32_PnPSignedDriver -filter "DeviceName = \'Microsoft ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
我不知道你为什么把
\
放在单引号之前


但也许您应该使用,这似乎更适合任务?

如果我理解正确,您只想将literal命令传递给PS。如果是这样,您可以删除和单引号的转义:

public static string PlatformModelCommand => @"""Get-WmiObject Win32_PnPSignedDriver -filter \""DeviceName = 'Microsoft Bluetooth LE Enumerator'\"" | select -ExpandProperty driverversion""";
编辑。 由于将命令作为参数传递给powershell.exe,因此必须转义内部引号。
我同意Luuk的观点,你可能会有更好的时间来解决这个问题。还有一种方法是这样的。

如果我理解正确,您只需要将literal命令传递给PS。如果是这样,您可以删除和单引号的转义:

public static string PlatformModelCommand => @"""Get-WmiObject Win32_PnPSignedDriver -filter \""DeviceName = 'Microsoft Bluetooth LE Enumerator'\"" | select -ExpandProperty driverversion""";
编辑。 由于将命令作为参数传递给powershell.exe,因此必须转义内部引号。
我同意Luuk的观点,你可能会有更好的时间来解决这个问题。同样也有一些方法。

RunpProcess做什么?为什么在没有使用的情况下发布
RunProcess
的源代码?另外,您是否在proc中运行PowerShell?抱歉,忘了添加一个func,请编辑我的问题
RunPsProcess
做什么?为什么在没有使用的情况下发布
RunProcess
的源代码?另外,您是否在proc中运行PowerShell?抱歉,忘记添加一个func,编辑我的问题我对这个答案投了赞成票,因为PowerShell应该通过
System.Management.Automation.PowerShell
而不是
Process
调用。我对这个答案投了赞成票,因为PowerShell应该通过
System.Management.Automation.PowerShell
而不是
Process
调用。