【问题标题】:How to include string inside another string如何在另一个字符串中包含字符串
【发布时间】: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


【解决方案1】:

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

public static string PlatformModelCommand => @"""Get-WmiObject Win32_PnPSignedDriver -filter \""DeviceName = 'Microsoft Bluetooth LE Enumerator'\"" | select -ExpandProperty driverversion""";

编辑。 由于您将命令作为参数传递给 powershell.exe,因此您必须转义内引号。 我同意 Luuk 的观点,因为您可能有更好的时间使用 MS's abstractions 来解决这个问题。 SO也有some goodexamples of that方法。

【讨论】:

    【解决方案2】:

    如果你在 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
    

    我不知道你为什么在单引号前加上\

    但也许你应该使用System.Management.Automation.Powershell,这似乎更适合这项任务?

    【讨论】:

    • 我赞成这个答案,因为 PowerShell 应该通过 System.Management.Automation.Powershell 而不是 Process 调用。
    猜你喜欢
    • 1970-01-01
    • 2011-08-31
    • 2017-12-24
    • 2011-02-05
    • 2021-05-06
    • 1970-01-01
    • 2013-04-08
    • 2014-07-24
    相关资源
    最近更新 更多