2016-11-23 4 views
0

私はこのスクリプトアウトファイルの問題

ForEach ($u in $usersFromFile) 
{ 
try{ 
    $nomailbox = if (-not (get-mailbox $u.alias)){ 
    $notfound = $notfound + $nomailbox 
     Write-host "No mailbox for "$u.alias -F blue|out-file "d:\scripts\nomailbox.txt"} 
    } 

Catch{} 

は何もOUTFILE

私は何をしないのですに書き込まれませんか? TIA

アンディ

+3

'Write-Host'が** host **(または情報ストリーム)に出力しないように書き込みます。 – PetSerAl

+0

['Write-Host'は有害とみなされます](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/) – JosefZ

答えて

0

あなたが5より低いPowerShellのバージョンを使用している場合は、アウトホストホストするために書き込み、PetSerAlが言ったと同じように、あなたの代わりに、書き込み出力を使用する必要があります。 PS 5ではOut-Hostを使用してOut-Hostでこれまで達成できなかったことを達成できます。

0

ちょうど使用:

ForEach ($u in $usersFromFile) { try{ 
    $nomailbox = if (-not (get-mailbox $u.alias)){ 
    $notfound = $notfound + $nomailbox 
     Write-Output "No mailbox for "$u.alias -F blue|out-file "d:\scripts\nomailbox.txt"} 
    } 

Catch{} 

これは動作するはずです。

function writehosttofile { 
Param(
[switch]$para 
) 
Write-Host "No mailbox for "$u.alias -F blue 

if($para){ 
"No mailbox for " + $u.alias 
} 

} 

writehosttofile -para | out-file "d:\scripts\nomailbox.txt" 

書き込みホスト

説明

書き込みホストが送信されます。しかし、私はyoureのつもり色が理由のないこの行は..あなたがあまりにも機能してこれを使用することができない、と思いますオブジェクトをホストに送信します。オブジェクトは返されません。

また、あなたのスクリプトを参照し、ファイル内のすべての出力リダイレクトすることができます。良い一日を

PS C:\temp> cd c:\bin 
PS C:\bin> .\script.ps1 > C:\bin\script_output.txt 

を。

関連する問題