From 55b6e4934b40e36cfb1593f7ec609bfe310b1fcb Mon Sep 17 00:00:00 2001 From: Jonathan Rioux Date: Thu, 5 Apr 2018 10:51:41 -0400 Subject: [PATCH] new function --- Functions/Get-SQLRestoreSessions.psm1 | 80 ++++++++++++++++++++++++++ PowerDP.psd1 | Bin 8376 -> 8534 bytes 2 files changed, 80 insertions(+) create mode 100644 Functions/Get-SQLRestoreSessions.psm1 diff --git a/Functions/Get-SQLRestoreSessions.psm1 b/Functions/Get-SQLRestoreSessions.psm1 new file mode 100644 index 0000000..a26af4f --- /dev/null +++ b/Functions/Get-SQLRestoreSessions.psm1 @@ -0,0 +1,80 @@ +Function Get-SQLRestoreSessions { + <# + .SYNOPSIS + Returns a table with latest SQL restore sessions + .DESCRIPTION + Fetches the session messages of each SQL restore sessions and creates a PowerShell table + .EXAMPLE + Get-SQLRestoreSessions -Days 2 + .LINK + https://github.com/jorioux/PowerDP + #> + + Param( + [int]$Days = 1 + ) + + $Cmd = "omnidb -session -type restore" + $Cmd += " -last $Days" + + $Sessions = Invoke-Expression -Command $Cmd + + #remove the first 2 lines + $Sessions = $Sessions[2..($Sessions.Count-1)] + + $Array = @() + $i = 0 + $Sessions | %{ + $i++ + $Item = New-Object PSObject + $StartTime, $EndTime, $Duration, $Database, $Target, $Source = "" + + #If the line matches a Session ID + if($_ -match '^(\d{4}\/\d{2}\/\d{2}\-\d+)\s+Restore\s+(In Progress|\w+)\s+.*$'){ + $SessionID = $Matches[1] + + Write-Progress -Activity "Fetching SQL restore sessions" -status "$SessionID" -percentComplete ($i / $Sessions.Count*100) + + $Status = $Matches[2] + $Messages = $(omnidb -session $SessionID -report) + $Messages | %{ + if($_ -match '^.*Time:\s(.*)$'){ + if($StartTime -eq ""){ + $StartTime = [datetime]$Matches[1] + } + $EndTime = [datetime]$Matches[1] + return + } + if($_ -match "^\s+Database name\s+:\s+'(.*)'.*$"){ + $Database = $Matches[1] + return + } + if($_ -match "^\s+Target SQL Server\s+:\s+'(.*)'.*$"){ + $Target = $Matches[1] + return + } + if($_ -match "^\s+Source SQL Server\s+:\s+'(.*)'.*$"){ + $Source = $Matches[1] + return + } + } + if([string]::IsNullOrEmpty($Database)){ + return + } + $Duration = [string]($EndTime - $StartTime) + $Item | Add-Member -type NoteProperty -Name Database -Value $Database + $Item | Add-Member -type NoteProperty -Name Source -Value $Source + $Item | Add-Member -type NoteProperty -Name Target -Value $Target + $Item | Add-Member -type NoteProperty -Name StartTime -Value $StartTime + $Item | Add-Member -type NoteProperty -Name Duration -Value $Duration + $Item | Add-Member -type NoteProperty -Name Status -Value $Status + $Item | Add-Member -type NoteProperty -Name SessionID -Value $SessionID + $Array += $Item + } else { + write-host "Erreur" + return + } + } + + return $Array | Select-Object * | Sort-Object -Property "StartTime" +} \ No newline at end of file diff --git a/PowerDP.psd1 b/PowerDP.psd1 index af158ed422286596fda06c9543f9c8b4272f17c5..7069622399ed6d455517eb6466e9f3071a7f624f 100644 GIT binary patch delta 185 zcmdntc+F|THAd5o*Ug#SO&Jmy%oq$9%z@aL!Gb}TA&nsw2u*=36NVHZOaii!7z}~( z20&ISkW2>RL$xBvhE delta 99 zcmccSw8L@2HAdr&*Ug#S(-;gG3>nNAEEp0Q%o&Urbb&krD9Z#WW&nh`K-nZ9HU_ei kfiQ_7mBA87r!k}ep$QPDZVqMM%eT2k$cJh3ACV{?02k&J4FCWD