【发布时间】:2017-01-31 18:17:15
【问题描述】:
为什么我在 TEE-Object 输出这个脚本时得到一个空白文件,我在屏幕上看到输出?
$Computers = (gc C:\Scripts\Computers.txt)
foreach ($Computer in $Computers)
{
$Computer
Invoke-Command -ComputerName $Computer -ScriptBlock { winmgmt -standalonehost }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "UALSVC" -ErrorAction SilentlyContinue){ Stop-Service UALSVC -Force } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "MMS" -ErrorAction SilentlyContinue){ Stop-Service MMS -Force } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "iphlpsvc" -ErrorAction SilentlyContinue){ Stop-Service iphlpsvc -Force } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "hpqams" -ErrorAction SilentlyContinue){ Stop-Service hpqams -Force } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "wscsvc" -ErrorAction SilentlyContinue){ Stop-Service wscsvc -Force } }
Invoke-Command -ComputerName $Computer -ScriptBlock { Restart-Service winmgmt -Force }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "hpqams" -ErrorAction SilentlyContinue){ Start-Service hpqams } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "iphlpsvc" -ErrorAction SilentlyContinue){ Start-Service iphlpsvc } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "MMS" -ErrorAction SilentlyContinue){ Start-Service MMS } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "UALSVC" -ErrorAction SilentlyContinue){ Start-Service UALSVC } }
Invoke-Command -ComputerName $Computer -ScriptBlock { if (Get-Service "wscsvc" -ErrorAction SilentlyContinue){ Start-Service wscsvc } }
} Tee-Object -file c:\Scripts\WMI-Output.txt
【问题讨论】:
-
& { foreach (...) {...} } | Tee-Object ... -
只是一个观察 - 所有的服务操作都可以使用 Get-Service/Stop-Service/Start-Service 的 -ComputerName 参数通过 RPC 完成。
标签: powershell output