【问题标题】:Blank file in Tee-Object file PowershellTee-Object文件Powershell中的空白文件
【发布时间】: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


【解决方案1】:

作为PetSerAl hints at,您不能直接从foreach(){} 循环管道输出。你有几个选择:

包裹在子表达式中:

$(foreach($i in 1..10){
  $i * $i
}) | Tee-Object -File C:\Scripts\Squares.txt

包装在一个脚本块中:

&{
  foreach($i in 1..10){
    $i * $i
  }
} | Tee-Object -File C:\Scripts\Squares.txt

使用ForEach-Object 代替foreach(){}

1..10 | ForEach-Object {
  $i * $i
} | Tee-Object -File C:\Scripts\Squares.txt

您的脚本也可以大大简化:

Get-Content C:\Scripts\Computers.txt |ForEach-Object {
    Invoke-Command -ComputerName $_ -ScriptBlock { 
        # Enable winmgmt standalone mode
        winmgmt -standalonehost 

        # Stop services
        Get-Service UALSVC,MMS,iphlpsvc,hpqams,wscsvc -ErrorAction SilentlyContinue |Stop-Service -Force

        # Restart winmgmt 
        Restart-Service winmgmt -Force 

        # Start services
        Get-Service hpqams,iphlpsvc,MMS,UALSVC,wscsvc -ErrorAction SilentlyContinue |Start-Service -Force
    }
} | Tee-Object -FilePath c:\Scripts\WMI-Output.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2019-01-16
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多