From 21c803cabacc744786085bcc10c7ab81958aaab6 Mon Sep 17 00:00:00 2001 From: jorioux Date: Fri, 8 Mar 2019 14:45:11 -0500 Subject: [PATCH] Major refactoring --- Functions/ConvertFrom-OmnidbDetail.psm1 | 62 +++++++++++++ ...To-Array.psm1 => ConvertFrom-Omnirpt.psm1} | 35 ++----- Functions/ConvertTo-Int.psm1 | 25 +++++ Functions/Get-ListSessions.psm1 | 36 +++++++- Functions/Get-Omnidb.psm1 | 43 +++++++++ Functions/Get-Omnirpt.psm1 | 64 +++++++++++++ Functions/Get-SessionObjects.psm1 | 27 ------ License.md | 2 +- PowerDP.psd1 | Bin 8584 -> 8956 bytes README.md | 87 +++++++++++------- 10 files changed, 285 insertions(+), 96 deletions(-) create mode 100644 Functions/ConvertFrom-OmnidbDetail.psm1 rename Functions/{ConvertTo-Array.psm1 => ConvertFrom-Omnirpt.psm1} (84%) create mode 100644 Functions/ConvertTo-Int.psm1 create mode 100644 Functions/Get-Omnidb.psm1 create mode 100644 Functions/Get-Omnirpt.psm1 delete mode 100644 Functions/Get-SessionObjects.psm1 diff --git a/Functions/ConvertFrom-OmnidbDetail.psm1 b/Functions/ConvertFrom-OmnidbDetail.psm1 new file mode 100644 index 0000000..a2ac491 --- /dev/null +++ b/Functions/ConvertFrom-OmnidbDetail.psm1 @@ -0,0 +1,62 @@ +Function ConvertFrom-OmnidbDetail { + <# + .SYNOPSIS + Returns an array from "omnidb [options] -detail" output + .DESCRIPTION + Accepts the output of "omnidb [options] -detail" in pipline and returns a PowerShell array + .EXAMPLE + omnidb -session 2019/02/07-7 -detail | ConvertFrom-OmnidbDetail + .LINK + https://github.com/jorioux/PowerDP + #> + + Param( + [Parameter(ValueFromPipeline = $true)] + [string[]]$Lines, + [switch]$FullObjectName + ) + BEGIN { + $ArrayOutput = @() + $Item = New-Object PSObject + } + PROCESS { + + $Lines | %{ + #An empty line means its a new item + if([string]::IsNullOrWhiteSpace($_)){ + #Add Item to Array only if its not empty + if(($Item|Get-Member -Type NoteProperty).count -gt 0){ + $ArrayOutput += $Item + $Item = New-Object PSObject + } + } + if($_ -match "^\s*((?:[\w\[\]]\s?)+)\s+:\s(.*)"){ + $Name = $Matches[1].trimend() + $Value = $Matches[2] + + if($Name -eq "Object name" -and !$FullObjectName){ + $Value = [regex]::Match($Value, '^(.+):.*').groups[1].value + } + + #Convert values to correct type + if($Name -match "^.*(Time|Started|Finished).*$"){ + $Value = $Value + } elseif($Name -match "^.*(Number).*$"){ + $Value = ConvertTo-Int($Value) + } elseif($Value -match "^(\d+)\sKB"){ + $Name = $Name + " (GB)" + $Value = ConvertTo-Int($Matches[1]/1024/1024) + } elseif($Name -match "^(.*)\s\[KB.*"){ + $Name = $Matches[1] + " (GB)" + $Value = ConvertTo-Int($Value/1024/1024) + } + $Item | Add-Member -type NoteProperty -Name $Name -Value $Value + + } + } + } + END { + $ArrayOutput += $Item + return $ArrayOutput | Select-Object * + } +} diff --git a/Functions/ConvertTo-Array.psm1 b/Functions/ConvertFrom-Omnirpt.psm1 similarity index 84% rename from Functions/ConvertTo-Array.psm1 rename to Functions/ConvertFrom-Omnirpt.psm1 index fac4716..e7a28fb 100644 --- a/Functions/ConvertTo-Array.psm1 +++ b/Functions/ConvertFrom-Omnirpt.psm1 @@ -1,13 +1,13 @@ -Function ConvertTo-Array { +Function ConvertFrom-Omnirpt { <# .SYNOPSIS Creates an array out of the omnirpt output .DESCRIPTION Converts the "omnirpt <...> -tab" output into a native PowerShell array for easy filtering and manipulation. .EXAMPLE - omnirpt -report list_sessions -timeframe 24 24 -tab | ConvertTo-Array + omnirpt -report list_sessions -timeframe 24 24 -tab | ConvertFrom-Omnirpt .EXAMPLE - omnirpt -report used_media -timeframe 24 24 -tab | ConvertTo-Array | Where-Object {$_.Location -like "*HP:MSL6480*"} + omnirpt -report used_media -timeframe 24 24 -tab | ConvertFrom-Omnirpt | Where-Object {$_.Location -like "*HP:MSL6480*"} .LINK https://github.com/jorioux/PowerDP #> @@ -132,6 +132,9 @@ Function ConvertTo-Array { } elseif($Headers[$_] -match '^.*\[kB\]') { $Size = $ArrLine[$_]/1KB/1KB $Item | Add-Member -type NoteProperty -Name $Headers[$_].replace(' [kB]',' (GB)') -Value $Size + } elseif($Headers[$_] -match '^.*\[MB\]') { + $Size = [int]($ArrLine[$_]).split(',')[0]/1KB + $Item | Add-Member -type NoteProperty -Name $Headers[$_].replace(' [MB]',' (GB)') -Value $Size } elseif($Headers[$_] -match '^.*\[MB/min\]') { $Value = ConvertTo-Int $ArrLine[$_] $Item | Add-Member -type NoteProperty -Name $Headers[$_].replace(' [MB/min]',' (MB/min)') -Value $Value @@ -150,30 +153,4 @@ Function ConvertTo-Array { END { return $ArrayOutput } -} - -Function ConvertTo-Int { - <# - .SYNOPSIS - Converts a string value to a Int or Double - #> - [CmdletBinding()] - Param( - [Parameter(ValueFromPipeline = $true)] - [ValidateNotNullOrEmpty()] - $IntVal - ) - try{ - if($IntVal.GetType().FullName -match '^.*Int.*$') { - return [int]$IntVal - } elseif($IntVal.GetType().FullName -match '^.*(Double|Float).*$') { - return [math]::Round([double]$IntVal,2) - } elseif($IntVal -match '^.*(\,|\.).*$') { - return [math]::Round([double]($IntVal.replace(',','.')),2) - } else { - return [int]($IntVal) - } - } catch { - return $IntVal - } } \ No newline at end of file diff --git a/Functions/ConvertTo-Int.psm1 b/Functions/ConvertTo-Int.psm1 new file mode 100644 index 0000000..06c1beb --- /dev/null +++ b/Functions/ConvertTo-Int.psm1 @@ -0,0 +1,25 @@ +Function ConvertTo-Int { + <# + .SYNOPSIS + Converts a string value to a Int or Double + #> + [CmdletBinding()] + Param( + [Parameter(ValueFromPipeline = $true)] + [ValidateNotNullOrEmpty()] + $IntVal + ) + try{ + if($IntVal.GetType().FullName -match '^.*Int.*$') { + return [int]$IntVal + } elseif($IntVal.GetType().FullName -match '^.*(Double|Float).*$') { + return [math]::Round([double]$IntVal,2) + } elseif($IntVal -match '^.*(\,|\.).*$') { + return [math]::Round([double]($IntVal.replace(',','.')),2) + } else { + return [int]($IntVal) + } + } catch { + return $IntVal + } +} \ No newline at end of file diff --git a/Functions/Get-ListSessions.psm1 b/Functions/Get-ListSessions.psm1 index 849cc2f..3212661 100644 --- a/Functions/Get-ListSessions.psm1 +++ b/Functions/Get-ListSessions.psm1 @@ -19,7 +19,8 @@ Function Get-ListSessions { [int]$Days = 0, [string]$Timeframe, [string]$Mode = "*", #full,incr,trans,etc... - [String]$Status = "*" + [String]$Status = "*", + [switch]$WithMedia #fetch for Media label and pool name ) $Cmd = 'omnirpt -report list_sessions' @@ -30,13 +31,38 @@ Function Get-ListSessions { if($Hours -lt 1){ $Hours = 24 } - $Timeframe = "$Hours $Hours" + $Timeframe = $([string]$Hours + ' ' + [string]$Hours) } - $Cmd += " -timeframe $Timeframe" - $Cmd += " -tab" + $Cmd += ' -timeframe ' + $Timeframe + $Cmd += ' -tab' - $Array = Invoke-Expression -Command $Cmd | ConvertTo-Array + $Array = Invoke-Expression -Command $Cmd | ConvertFrom-Omnirpt + + if($WithMedia){ + $NewArray = @() + $Array | %{ + $Session = $_ + $PoolName = '' + $Medias = '' + #Fetch Media info only if session type is Copy + if($Session.'Session Type' -like "*copy*"){ + $Cmd = 'omnirpt -report session_media' + $Cmd += ' -session ' + $Session.'Session ID' + $Cmd += ' -tab' + + $(Invoke-Expression -Command $Cmd | ConvertFrom-Omnirpt) | %{ + $Medias += (($_.Label).split('[')[1]).split(']')[0] + ' ' + $PoolName = $_.'Pool Name' + } + $Medias = $Medias.Trim() + } + $NewArray += $Session | + Select-Object -Property *, @{n='Media Labels'; e={$SessionID = $Session.'Session ID'; $Medias}} | + Select-Object -Property *, @{n='Pool Name'; e={$SessionID = $Session.'Session ID'; $PoolName}} + } + $Array = $NewArray + } return $Array | Where { $_.Specification -like "$Specification" -and diff --git a/Functions/Get-Omnidb.psm1 b/Functions/Get-Omnidb.psm1 new file mode 100644 index 0000000..58e6cc7 --- /dev/null +++ b/Functions/Get-Omnidb.psm1 @@ -0,0 +1,43 @@ +Function Get-Omnidb { + <# + .SYNOPSIS + Returns an array with session detail + .DESCRIPTION + Fetches the session detail and returns a PowerShell array + .EXAMPLE + Get-Omnidb -Session 2019/02/07-7 + .LINK + https://github.com/jorioux/PowerDP + #> + + [CmdletBinding()] + Param( + [Parameter(Mandatory=$true, ValueFromPipeline = $true)] + [ValidateNotNullOrEmpty()] + [string]$Session, + [switch]$Media, + [switch]$Listcopies + ) + + if($Media){ + $Cmd = "omnidb -session $Session -media -detail" + } elseif($Listcopies){ + $Output = (Get-Omnidb -Session $Session) + if($Output -eq $null){ + return $Output + } else { + $CopyID = $Output.'Copy ID' + } + $CopyID = $CopyID.split(' ')[0] + $Cmd = "omnidb -copyid $CopyID -listcopies -detail" + } else { + $Cmd = "omnidb -session $Session -detail" + } + + $Output = (Invoke-Expression -Command $Cmd) + if($Output.count -le 2){ + return $Output + } else { + return $Output | ConvertFrom-OmnidbDetail + } +} \ No newline at end of file diff --git a/Functions/Get-Omnirpt.psm1 b/Functions/Get-Omnirpt.psm1 new file mode 100644 index 0000000..ee2c746 --- /dev/null +++ b/Functions/Get-Omnirpt.psm1 @@ -0,0 +1,64 @@ +Function Get-Omnirpt { + <# + .SYNOPSIS + omnirpt -report session_objects + .DESCRIPTION + Converts the session_objects report to a PowerShell array + .EXAMPLE + Get-Omnirpt 2018/03/30-01 + .LINK + https://github.com/jorioux/PowerDP + #> + + [CmdletBinding()] + Param( + [Parameter(ValueFromPipeline = $true)] + [string]$Report, + [string]$Session, + [string]$Timeframe, + [int]$Days + ) + + $ValidReports = @( + 'list_sessions', + 'used_media', + 'host_statistics', + 'obj_nobackup', + 'obj_copies', + 'obj_lastbackup', + 'obj_avesize', + 'media_list', + 'single_session', + 'session_objects', + 'session_hosts', + 'session_devices', + 'session_media', + 'session_objcopies' + ) + + if(!$ValidReports.Contains($Report)){ + write-host "Not a valid report. Valid reports are :`n" + $ValidReports | %{write-host "`t$_"} + return "" + } + + $Cmd = "omnirpt -report $Report " + + if(![string]::IsNullOrEmpty($Session)){ + $Cmd += " -session $Session " + } + + if($Days -ge 1){ + $Timeframe = [string]($Days*24) + $Timeframe = $Timeframe + " " + $Timeframe + $Cmd += " -timeframe $Timeframe " + } elseif(![string]::IsNullOrEmpty($Timeframe)){ + $Cmd += " -timeframe $Timeframe " + } + + $Cmd += ' -tab' + + $Array = Invoke-Expression -Command $Cmd | ConvertFrom-Omnirpt + + return $Array | Select-Object * +} \ No newline at end of file diff --git a/Functions/Get-SessionObjects.psm1 b/Functions/Get-SessionObjects.psm1 deleted file mode 100644 index 2600976..0000000 --- a/Functions/Get-SessionObjects.psm1 +++ /dev/null @@ -1,27 +0,0 @@ -Function Get-SessionObjects { - <# - .SYNOPSIS - omnirpt -report session_objects - .DESCRIPTION - Converts the session_objects report to a PowerShell array - .EXAMPLE - Get-SessionObjects 2018/03/30-01 - .LINK - https://github.com/jorioux/PowerDP - #> - - [CmdletBinding()] - Param( - [Parameter(Mandatory=$true, ValueFromPipeline = $true)] - [ValidateNotNullOrEmpty()] - [string]$Session - ) - - $Cmd = 'omnirpt -report session_objects' - $Cmd += " -session $Session" - $Cmd += " -tab" - - $Array = Invoke-Expression -Command $Cmd | ConvertTo-Array - - return $Array | Select-Object * | Sort-Object -Property "Start Time" -} \ No newline at end of file diff --git a/License.md b/License.md index c54e2e0..3446d4d 100644 --- a/License.md +++ b/License.md @@ -1,4 +1,4 @@ -Copyright (c) 2018, Jonathan Rioux +Copyright (c) 2019, Jonathan Rioux All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/PowerDP.psd1 b/PowerDP.psd1 index 4b8c451f156ff3d1fab85b51708fcaa4fc928ff6..4e2bc51459de827331e84f5fe45901a37d6ae615 100644 GIT binary patch delta 353 zcmeBh{^PnqjfvHeL65;;vo_OWCPvH6=UFUyCNs&1OjZ!&nXDzKHu;qRm!LmGE<+we zCPNWJ0YeFcI)lz+Mj26L71A&jDGW&rE)1zaRf$0LIcREiCi{qRA(<{Qd7hvO3O`Or zj}h5)kO~fdWYbY?2g~!qaB9fB>1lfQNu$deoA^>HpiNn;O sIec=XNbF=TQI*LD1X+yL8Qj5M(q%wa4Ri>D0?>o#au7#oi^lQ*06*7HGynhq delta 207 zcmez4+TpxGjfvHOL65;~vo_OWCPs_R=UFUy7!@WP%85>P5K@`!Ajl^g!jR9P%izdR z1cZqUl?>_(It(xs9&oifiop!248=g02~?lQ;Lni6kOdS;W+(xQA?wkZd_qWV^9123 yjQnUuPhKx7K3PUcVDc7GnaOpcVv~*JM1al^m@Fh_B#y)I$@*fJn-_^i@c;l>w=huv diff --git a/README.md b/README.md index 2c34fe3..f9ba1a2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ PowerShell for Micro Focus Data Protector (DP) - -This PowerShell Module converts the `omnirpt <...> -tab` output into a PowerShell array for easy filtering and manipulation. It also provides a few functions that outputs native PowerShell arrays. I intent to develop other functions in the future that builds on top of that. +This PowerShell Module uses the `omnirpt` and `omnidb` and converts the output into native PowerShell arrays. Installation - @@ -18,27 +18,67 @@ Install-Module PowerDP #### PowerShell v4 and earlier Get [PowerShellGet Module](https://docs.microsoft.com/en-us/powershell/gallery/psget/get_psget_module) then do `Install-Module PowerDP` -Usage +Usage examples - ```PowerShell -#The main function is `ConvertTo-Array`. You can pipline the output from the `omnirpt` command or pass it as a parameter. -#The column names are kept the same, so you can easily filter with the columns names and display only the columns you want. -#It's important to include the `-tab` parameter because the function needs the input to be tab separated. -omnirpt -report list_sessions -timeframe 24 24 -tab | ConvertTo-Array -omnirpt -report used_media -timeframe 48 48 -tab | ConvertTo-Array | Where-Object {$_.Location -like "*HP:MSL6480*"} +# The main functions are `Get-Omnidb` and `Get-Omnirpt`. +# The column names are kept the same, so you can easily filter with the columns names and display only the columns you want. +Get-Omnirpt -Report obj_copies -Days 7 +Get-Omnirpt -Report obj_nobackup +Get-Omnirpt -Report session_objects -Session 2019/03/03-12 -#Converts the list_sessions report to a PowerShell array +Get-Omnidb -Session 2019/03/07-7 -Listcopies +Get-Omnidb -Session 2019/03/03-2 -Media + +#Converts the list_sessions report to a PowerShell array with additionnal filters Get-ListSessions | Out-GridView Get-ListSessions -Specification *full* -Days 7 -SessionType backup Get-ListSessions -Specification *sql* -Hours 4 -Mode trans -#Converts the session_objects report to a PowerShell array -Get-SessionObjects 2018/03/30-01 - #Fetches the session messages of each SQL restore sessions and returns a PowerShell array Get-SQLRestoreSessions -Days 2 ``` +`Get-Omnirpt` usage +- +You must specify a report. You can omit `-Report` and only write `Get-Omnirpt ` if you want. Some reports requires additional parameters like `-Session` or `-Timeframe`. You can substitute `-Timeframe` by `-Days`. + +A few examples: +```PowerShell +Get-Omnirpt obj_copies -Days 2 +Get-Omnirpt obj_copies -Timeframe "48 48" +Get-Omnirpt session_media -Session 2019/03/03-6 +``` + +Available reports for `-Report` parameter are: +* list_sessions +* used_media +* host_statistics +* obj_nobackup +* obj_copies +* obj_lastbackup +* obj_avesize +* media_list +* single_session +* session_objects +* session_hosts +* session_devices +* session_media +* session_objcopies + +`Get-Omnidb` usage +- +You must specify a session with `-Session`. You can then specify one these parameters: + +* `-Media` +* `-Listcopies` + +A few examples: +```PowerShell +Get-Omnidb 2019/03/07-7 -Listcopies +Get-Omnidb 2019/03/03-2 -Media +``` + Prerequisites - PowerDP requires `omniback` to be installed, and its `bin` directory must be in the PATH environment variable. @@ -48,27 +88,6 @@ Compatibility - Confirmed working on DP versions 10.00 and up. -Release notes +Contribution - -#### 1.0.6 (October 25, 2018) -* Bugfix: The Duration and EndTime fields now shows *null* when the job has not ended yet -* Since DP is now owned by Micro Focus, I changed the title of the module accordingly - -#### 1.0.5 (April 20, 2018) -* Converts string numbers to Int to allow sorting -* Some bugfix - -#### 1.0.4 (April 3, 2018) -* Added new function : Get-SessionObjects -* Enhanced function ConvertTo-Array - - Removed "[" and "]" in column names so they are displayed correctly in Out-GridView - - "Duration" column now displays with seconds - - Converts the "Size [kB]" column to "Size (GB)" for better readability of the size - -#### 1.0.3 (March 22, 2018) -* Added new function : Get-ListSessions -* Removed "\_t" (timestamp) column and keep only the non-"\_t" column since its already converted to DateTime format -* Some code optimization - -#### 1.0.2 (March 21, 2018) -* Initial stable release \ No newline at end of file +Feel free to open issues and to contribute! \ No newline at end of file