代码之家  ›  专栏  ›  技术社区  ›  Ian Kemp

Windows可执行文件无法查看PowerShell命令的输出

  •  -2
  • Ian Kemp  · 技术社区  · 8 年前

    这项工作:

    PS> $serviceName = Get-WmiObject Win32_service -filter "displayname = 'Security Center'" |select -expandproperty name
    PS> sc.exe start $serviceName
    

    这不是-就像管道变量 $_ 没有传递给 sc.exe :

    PS> Get-WmiObject Win32_service -filter "displayname = 'Security Center'" |select -expandproperty name |sc.exe start "$_"
    DESCRIPTION:
            Starts a service running.
    USAGE:
            sc <server> start [service name] <arg1> <arg2> ...
    

    我错过了什么让这成为一条龙?我已经尝试了我能想到的一切(脚本块等等),但似乎没有成功。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Mathias R. Jessen    8 年前

    $_ 无法在调用范围内神奇地工作。包装 sc.exe a中的声明 ForEach-Object 脚本块:

    Get-WmiObject Win32_service -filter "displayname = 'Security Center'" |select -expandproperty name |ForEach-Object {
      sc.exe start "$_"
    }
    
    推荐文章