2017-01-31 12 views
0

Warum bekomme ich eine leere Datei, wenn ich dieses Skript TEE-Object ausgegeben habe, sehe ich die Ausgabe auf dem Bildschirm?Leere Datei in Tee-Objekt Datei Powershell

$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 
+2

'& {foreach (...) {...}} | Tee-Objekt ... ' – PetSerAl

+0

Perfekt, danke! – pinchepooch

+0

Nur eine Beobachtung - die gesamte Dienstmanipulation konnte über RPC mit dem Parameter -ComputerName von Get-Service/Stop-Service/Start-Service durchgeführt werden. –

Antwort

1

Als PetSerAl hints at, können Sie nicht Rohrausgang direkt von einer foreach(){} Schleife. Sie haben ein paar Optionen hier:

Wrap in Unterausdruck:

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

Wrap in einer Skript:

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

Verwenden ForEach-Object statt foreach(){}:

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

Ihr Skript kann auch stark vereinfacht werden:

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