2016-03-19 16 views
-1

this amazing answerを使用していて、RunSpacePoolsでCSVファイルを出力していますが、CSVファイルに空白行があり、空白行がどこから来ているのか分かりません。RunSpacePool出力CSVに空白行が含まれています

空白行が試行錯誤の後,,,

IF(Get-Command Get-SCOMAlert -ErrorAction SilentlyContinue){}ELSE{Import-Module OperationsManager} 

    "Get Pend reboot servers from prod" 
New-SCOMManagementGroupConnection -ComputerName ProdServer1 

$AlertData = get-SCOMAlert -Criteria "Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot'" | Select NetbiosComputerName 

    "Get Pend reboot servers from test" 
#For test information 
New-SCOMManagementGroupConnection -ComputerName TestServer1 

$AlertData += Get-SCOMAlert -Criteria "Severity = 1 AND ResolutionState < 254 AND Name = 'Pending Reboot'" | Select NetbiosComputerName 

    "Remove duplicates" 
$AlertDataNoDupe = $AlertData | Sort NetbiosComputerName -Unique 

$scriptblock = { 
Param([string]$server) 

$csv = Import-Csv D:\Scripts\MaintenanceWindow2.csv 
$window = $csv | where {$_.Computername -eq "$server"} | % CollectionName 
$SCCMWindow = IF ($window){$window}ELSE{"NoDeadline"} 

$PingCheck = Test-Connection -Count 1 $server -Quiet -ErrorAction SilentlyContinue 
     IF($PingCheck){$PingResults = "Alive"} 
     ELSE{$PingResults = "Dead"} 

Try{$operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $server -ErrorAction Stop 
     $LastReboot = [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime) 
     $LastReboot.DateTime} 
     Catch{$LastReboot = "Access Denied!"} 

#create custom object as output for CSV. 
[PSCustomObject]@{  
Server=$server 
MaintenanceWindow=$SCCMWindow 
Ping=$PingResults 
LastReboot=$LastReboot 
}#end custom object 
}#script block end 

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(100,100) 
$RunspacePool.Open() 
$Jobs = 
foreach ($item in $AlertDataNoDupe) 
{ 
$Job = [powershell]::Create(). 
     AddScript($ScriptBlock). 
     AddArgument($item.NetbiosComputerName) 
$Job.RunspacePool = $RunspacePool 

[PSCustomObject]@{ 
    Pipe = $Job 
    Result = $Job.BeginInvoke() 
} 
} 

Write-Host 'Working..' -NoNewline 

Do { 
    Write-Host '.' -NoNewline 
    Start-Sleep -Milliseconds 500 
} While ($Jobs.Result.IsCompleted -contains $false) 

Write-Host ' Done! Writing output file.' 
Write-host "Output file is d:\scripts\runspacetest4.csv" 

$(ForEach ($Job in $Jobs) 
{ $Job.Pipe.EndInvoke($Job.Result) }) | 
Export-Csv d:\scripts\runspacetest4.csv -NoTypeInformation 

$RunspacePool.Close() 
$RunspacePool.Dispose() 
+0

あなたのコードの下部にそのforeachループは、ファイルの書き込みされているものであれば、私はそれをデバッグします。パイプラインを使用しないコードをリファクタリングします。ジョブをループし、各ジョブに関する情報を記録します。別のデータ構造に書き込むと思われるものを集めます。あなたがそれに満足したら、csvにエクスポートしてください。 –

+0

コメントいただきありがとうございます、これは私の頭の上にあるのですか?私は何か動作するようにもっとコード例を探していますが、物事は単に期待どおりに動作していません。 – user4317867

+0

私はこの[実行空間プールのこのメソッド](http://learn-powershell.net/2012/05/10/speedy-network-information-query-using-powershell/)で作業していて、フォーマットされた出力を取得しています必要に応じてデータが正しくありません。 – user4317867

答えて

0

としてメモ帳で示されている、私が近づくためにwith this method of run space poolsを働くことになりました。もっと見ると、出力がWMIの余分な空白によって汚染されていることがわかりました。

これを解決するために、ScriptBlockのTryステートメントで次のコードを使用しました。

$LastReboot = [Management.ManagementDateTimeConverter]::ToDateTime ` 
($operatingSystem.LastBootUpTime).ToString().Trim() 

ここで、返されるデータは、必要に応じてすべて1行です。

- 出力にWMIの余分な空白をコメントする編集See this question for more details

次の方法を検討して、コンピュータの最後の再起動タイムスタンプを返します。必要に応じて文字列をフォーマットすることができます(see this library page for more info)。

$os = (gwmi -Class win32_operatingsystem).LastBootUpTime 
[Management.ManagementDateTimeConverter]::ToDateTime($os) 

Whitespaces in WMI output in PowerShell

空白を除去するTrim()を使用して、文字列への出力を変換することによって除去することができる空白を、観察。

WMI output without extra WhiteSpaces

関連する問題