Skip to content

Commit

Permalink
new function
Browse files Browse the repository at this point in the history
  • Loading branch information
jorioux committed Apr 5, 2018
1 parent bf79cc2 commit 55b6e49
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Functions/Get-SQLRestoreSessions.psm1
Original file line number Diff line number Diff line change
@@ -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"
}
Binary file modified PowerDP.psd1
Binary file not shown.

0 comments on commit 55b6e49

Please sign in to comment.