2017-10-20 114 views
0

我運行這個命令來拉日誌文件的最後一行:解析信息

Get-Content c:\temp\MigrationJobStatus-20171020-123839-515.log | 
    Select-Object -Last 1 

結果做給我的最後一行,但現在我需要篩選結果:

10/20/2017 12:38:56 PM信息[事件]:[JobEnd],[JobId]:[70b82296-b6e2-4539-897d-c46384619059],[時間]:[10/20/2017年12月38日49.074],[FilesCreated]:[0],[BytesProcessed]:[0],[ObjectsProcessed]:[34],[TotalExpectedSPObjects]:[34],[TotalErrors]:[19],[TotalWarnings ]:[3],[TotalRetryCount]:[0],[MigrationType]:[無],[MigrationDirection]:[導入] ,[CreatedOrUpdatedFileStatsBySize]:[{}],[ObjectsStatsByType]:[{「SPUser」:{「Count」:1,「TotalTime」:0,「AccumulatedVersions」:0,「ObjectsWithVersions」:0},「SPFolder」: 「計數」:4 「TOTALTIME」:629, 「AccumulatedVersions」:0 「ObjectsWithVersions」:0}, 「SPDocumentLibrary」: 「計數」:1, 「TOTALTIME」:68, 「AccumulatedVersions」:0, 「ObjectsWithVersions」 :0}, 「SPFILE」:{ 「計數」:13, 「TOTALTIME」:0 「AccumulatedVersions」:0 「ObjectsWithVersions」:0}, 「SPListItem」:{ 「計數」:16, 「TOTALTIME」:2240 「AccumulatedVersions」:0, 「ObjectsWithVersions」:0}}],[的correlationID]:[7bbf249e-701A-4000-8eee-c4a7ef172063]

我需要能夠拉動下,出口到CSV :

 
[JobId]: [70b82296-b6e2-4539-897d-c46384619059] 
[FilesCreated]: [0] 
[BytesProcessed]: [0] 
[ObjectsProcessed]: [34] 
[TotalExpectedSPObjects]: [34] 
[TotalErrors]: [19] 
[TotalWarnings]: [3] 

有人可以給我一些關於如何實現這一點的想法嗎? 我正在進行OneDrive 4 Business遷移,需要爲幾千個用戶提供Get-SPOMigrationJobProgress日誌的結果。

+0

我沒有辦法測試,但你可以試試'Get-SPOMigrationJobProgress -nologfile | export-csv。\ mylogfile.csv -notypeinformation'或類似的東西。 – brendan62269

+0

不,這不起作用。 – Chabango

回答

1

需要在那裏添加其他字段,然後再用出文件

$results = "" 
$fields = @("[JobId]", "[FilesCreated]") 
$items = get-content c:\temp\MigrationJobStatus-20171020-123839-515.log | select-object -last 1 | %{ $_.Split(",")} 
foreach($item in $items) 
{ 
    $field = ($item.Split(":")[0]).Trim() 
    if($fields.Contains($field)) { $results+= "$item`r`n" } 
} 

Write-Host $results 
0

您可以使用拆分並抓住你需要的字段保存結果。

$text = get-content c:\temp\MigrationJobStatus-20171020-123839-515.log | select-object -last 1 

$text = ($text -split ",").Trim(" ") 
$csvtext = @" 
$($text[3]) 
$($text[4]) 
$($text[5]) 
$($text[6]) 
$($text[7]) 
$($text[8]) 
"@ 

$csvtext | Out-File ".\logfile.csv" 
0

你可以得到你想要使用正則表達式,然後創建從每場比賽一psobject領域:

$regexPattern = '\[([^]]+)\]: \[([^]]+)\]' 
$result = Get-Content c:\temp\MigrationJobStatus-20171020-123839-515.log | 
    Select-Object -Last 1 | 
    Select-String -Pattern $regexPattern -AllMatches | 
    ForEach-Object { $_.Matches.Value } | 
    ForEach-Object { $_ -match $regexPattern | 
        Select-Object @{n='Name';e={$Matches[1]}},@{n='Value';e={$Matches[2]}} } 

可以過濾下來得到的對象集合與Where-Object和使用Export-Csv來將你的結果寫入一個csv文件。

+0

謝謝曼努埃爾。如果我創建一個psobect,我會用什麼作爲變量? $ object = [pscustomobject] {JobID = $ ???? FilesCreated = $ ???}。謝謝 – Chabango

+0

@Chabango用上面的例子,你會在變量'$ result'中找到一個對象集合。 –