Skip to content

Commit

Permalink
Major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jorioux committed Mar 8, 2019
1 parent 0c609d7 commit 21c803c
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 96 deletions.
62 changes: 62 additions & 0 deletions Functions/ConvertFrom-OmnidbDetail.psm1
Original file line number Diff line number Diff line change
@@ -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 *
}
}
Original file line number Diff line number Diff line change
@@ -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
#>
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
25 changes: 25 additions & 0 deletions Functions/ConvertTo-Int.psm1
Original file line number Diff line number Diff line change
@@ -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
}
}
36 changes: 31 additions & 5 deletions Functions/Get-ListSessions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions Functions/Get-Omnidb.psm1
Original file line number Diff line number Diff line change
@@ -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
}
}
64 changes: 64 additions & 0 deletions Functions/Get-Omnirpt.psm1
Original file line number Diff line number Diff line change
@@ -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 *
}
27 changes: 0 additions & 27 deletions Functions/Get-SessionObjects.psm1

This file was deleted.

2 changes: 1 addition & 1 deletion License.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file modified PowerDP.psd1
Binary file not shown.
Loading

0 comments on commit 21c803c

Please sign in to comment.