2017-04-13 10 views
1

私はpowershell初心者で、テキストファイルからラップトップとワークステーションのリストをインポートして、ネットワーク上にあるかどうかを確認してチェックを続ける小さなスクリプトを作成していましたコンピュータのエージェントのステータス/ config。Else Statement with String、CSVにエクスポート

コンピュータがオンラインでない場合は、$ workstationがオンラインではないというファイルを出力したいと考えています。

私が持っていた問題は、他のデータで1つのCSVに失敗文字列を取得していますが、IFと他のものを別々にエクスポートできますが、可能であれば1つのCSVに入れていいです。

どのような考えですか?ありがとう!

$workstation=Get-content C:\Scripts\workstation.txt 

$forloop=ForEach ($workstation in $workstation) { 

     if (Test-Connection -ComputerName $workstation -Count 1 -Quiet) {Get-Service -ComputerName $workstation | where-object {$_.Name -eq 'xxx Agent'} | select displayname, name, starttype, status | 
     export-csv -Path C:\Scripts\therealdeal.c } 

     Else {Write-Output "$workstation not available Out"} 
     } 

$forloop | Out-File C:\scripts\output.csv 
+3

多分混乱を避けるために複数形に変更しようとしますか?> '$ workstations = Get-content'そして'($ workstation in $ workstations) '私はこの問題を理解していません。 –

答えて

0

[OK]を私は、私はどのワークステーションが最初にオンラインであるかを確認し、次にオフラインであるものを報告し、残りのものを処理することをテストします。

#Load workstations 
$workstations=Get-content C:\Scripts\workstation.txt 
#Find which ones are online 
[array]$OnlineWorkstations = Test-Connection -ComputerName $workstations -Count 1 -ea 4 |Select -Expand Address 
#If they are not all online generate a file listing which ones are offline 
If($workstations.count -gt $OnlineWorkstations.count){ 
    "Workstations not online:" | Set-Content C:\Scripts\Offline.txt 
    $workstations | Where{$_ -notin $OnlineWorkstations} | Add-Content C:\Scripts\Offline.txt 
} 
#Get data from online workstations 
$Results = ForEach ($workstation in $workstation) { 
     Get-Service -ComputerName $workstation | where-object {$_.Name -eq 'xxx Agent'} | select displayname, name, starttype, status 
} 
$Results | 
    export-csv -Path C:\Scripts\therealdeal.c 

また、あなたのcsvファイル名には、拡張子から 'sv'がないようです。

+0

ありがとうございました!私はこれについて多くを学んだ。 2つのリストの数を比較し、それに基づいて2つの別々のリストを作成しているようです – MrDoe