Windows 可执行文件看不到 PowerShell 命令的输出



这有效:

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> ...

我错过了什么才能使这项工作成为单行本?我已经尝试了几乎所有我能想到的东西(脚本块等(,但似乎没有任何成功。

$_

调用作用域中不起作用。将 sc.exe 语句包装在ForEach-Object脚本块中:

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

最新更新