这有效:
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 "$_"
}