LogWert/logwert.ps1

180 lines
7.2 KiB
PowerShell
Raw Permalink Normal View History

2019-09-24 01:00:18 +00:00
#!/usr/bin/pwsh
2015-09-24 06:17:43 +00:00
##############################################################
2015-09-23 10:44:43 +00:00
## ##
2015-09-24 09:20:20 +00:00
## logwert.ps1 version. 1.2 ##
2015-09-23 10:44:43 +00:00
## ##
2019-09-24 01:14:59 +00:00
## This is a smal help script ##
## It evaluates robocopy logs and summarizes them ##
2015-09-23 10:44:43 +00:00
## ##
2019-09-24 01:14:59 +00:00
## @parameter: [path-log1] [path-log2] [path-log3] ... ##
2015-09-23 10:44:43 +00:00
## ##
2021-04-24 23:03:44 +00:00
## @author: 6543@obermui.de ##
2015-09-23 10:44:43 +00:00
## ##
##############################################################
2015-09-24 06:17:43 +00:00
################################### Einstellungen ####################################################
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
# b (=Bytes), k (=Kilobytes), m (=Megabytes), g (=Gigabytes) und t (=Terabytes)
[String]$Global:Log_Copy_Size_Einheit = "g"
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
################################### Hilfsvariablen NICHT editieren #######################################
2015-09-23 10:44:43 +00:00
[Double]$Global:zLog_Copy_errors = 0
[Double]$Global:zLog_Copy_Size = 0
[Double]$Global:zLog_Copy_real_Size = 0
[Double]$Global:zLog_Copy_item = 0
[Double]$Global:zLog_Copy_real_item = 0
################################### Functionen ##########################################################
2019-09-24 01:16:14 +00:00
function Log_Start ([String]$sLogpath){
If(Test-Path $sLogpath) { #test if file exists
2015-09-23 10:44:43 +00:00
$tmp = ""
2019-09-24 01:16:14 +00:00
$tmp = Get-Content $sLogpath #copy file into list
$tmp | ForEach-Object { Log_Main $_ } #process each line
2015-09-23 10:44:43 +00:00
}
2019-09-24 01:16:14 +00:00
} # start processing
2015-09-23 10:44:43 +00:00
function Log_Main ([String]$sLine){
2019-09-24 01:16:14 +00:00
# separate Set character
2015-09-23 10:44:43 +00:00
$sLine = $sLine.replace("`"", "#")
$sLine = $sLine.replace(",", "#")
2015-09-24 06:17:43 +00:00
$sLine = $sLine.replace("Insgesamt:", "")
2015-09-23 10:44:43 +00:00
2015-09-24 09:20:20 +00:00
if($sLine -match "[0-z]") {
2015-09-23 10:44:43 +00:00
2015-09-24 09:20:20 +00:00
2019-09-24 01:16:14 +00:00
### rule out anomalies
2015-09-24 09:20:20 +00:00
while(" ".Equals($sLine.Substring(0,1))) {$sLine = $sLine.Substring(1, $sLine.Length - 1)}
2019-09-24 01:16:14 +00:00
#Remove when String Begins with Lehrzeichen
2015-09-24 09:20:20 +00:00
while("#".Equals($sLine.Substring(0,1))) {$sLine = $sLine.Substring(1, $sLine.Length - 1)}
2019-09-24 01:16:14 +00:00
#If string begins with delimiter remove
2015-09-24 09:20:20 +00:00
while(" ".Equals($sLine.Substring($sLine.Length - 1,1))) {$sLine = $sLine.Substring(0, $sLine.Length - 1)}
2019-09-24 01:16:14 +00:00
#If String Ends with Lehrzeichen Remove String
2015-09-24 09:20:20 +00:00
while("#".Equals($sLine.Substring($sLine.Length - 1,1))) {$sLine = $sLine.Substring(0, $sLine.Length - 1)}
2019-09-24 01:16:14 +00:00
#Remove if string ends with delimiters
2015-09-24 09:20:20 +00:00
while($sLine.IndexOf("##") -gt -1 ) {$sLine = $sLine.Replace("##", "#")}
2019-09-24 01:16:14 +00:00
#Remove several following delimiters in a row
2015-09-24 09:20:20 +00:00
while($sLine.IndexOf(" ") -gt -1 ) {$sLine = $sLine.Replace(" ", " ")}
2019-09-24 01:16:14 +00:00
#Remove several following characters in a row
2015-09-24 09:20:20 +00:00
$sLine.Split("#") | ForEach-Object { WichCase $_ }
}
2019-09-24 01:16:14 +00:00
} #Line by line evaluation
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
function WichCase ([String]$sToken) {
2015-09-24 09:20:20 +00:00
if($sToken -match "[0-z]") {
while(" ".Equals($sToken.Substring(0,1))) {$sToken = $sToken.Substring(1, $sToken.Length - 1)}
2019-09-24 01:16:14 +00:00
#remove blank before rating
2015-09-24 09:20:20 +00:00
while(" ".Equals($sToken.Substring($sToken.Length - 1,1))) {$sToken = $sToken.Substring(0, $sToken.Length - 1)}
2019-09-24 01:16:14 +00:00
#remove blank avter rating
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
$aToken = $sToken.Split(" ")
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
# Count_Copy_errors
2015-09-24 09:20:20 +00:00
If(("Fehler".Equals($aToken[1])) -and ($aToken[0] -notmatch "[a-z]")) {
[Double]$Global:zLog_Copy_errors = [Double]$Global:zLog_Copy_errors + $aToken[0]
2015-09-24 06:17:43 +00:00
}
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
# Count_Items
2015-09-24 09:20:20 +00:00
If(("von".Equals($aToken[1])) -and ("Dateien".Equals($aToken[3])) -and ($aToken[0] -notmatch "[a-z]") -and ($aToken[2] -notmatch "[a-z]")) {
2015-09-24 06:17:43 +00:00
[Double]$Global:zLog_Copy_item += $aToken[2]
[Double]$Global:zLog_Copy_real_item += $aToken[0]
}
2015-09-23 10:44:43 +00:00
}
2015-09-24 06:17:43 +00:00
# Count_Size
2015-09-24 09:20:20 +00:00
If(("von".Equals($aToken[2])) -and ("kopiert".Equals($aToken[5])) -and ($aToken[0] -notmatch "[a-z]") -and ($aToken[3] -notmatch "[a-z]") ){# -and ($aToken[1] -match "[b,k,m,g,t]") -and ($aToken[4] -match "[b,k,m,g,t]")) {
[Double]$Global:zLog_Copy_Size += (Calc_Byte_Sice $aToken[3] $aToken[4] b)
[Double]$Global:zLog_Copy_real_Size += (Calc_Byte_Sice $aToken[0] $aToken[1] b)
2015-09-23 10:44:43 +00:00
}
2019-09-24 01:16:14 +00:00
} #Transfer of the line; evaluation; storage in global variables
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
function Calc_Byte_Sice ([Double]$Wert, [String]$EinheitEingabe, [String]$EinheitAusgabe ) {
2015-09-23 10:44:43 +00:00
2019-09-24 01:16:14 +00:00
#Calc_Byte_Sice: [value] [unit outpute] [unit input]
# If you enter something other than KB,MB,GB,TB for unit, it is calculated with bytes.
2015-09-23 10:44:43 +00:00
2019-09-24 01:16:14 +00:00
#fertieren the different size specifications (conversion unit= 1024)
2015-09-23 10:44:43 +00:00
# b (=Bytes), k (=Kilobytes), m (=Megabytes), g (=Gigabytes) und t (=Terabytes)
2015-09-24 06:17:43 +00:00
If("".Equals("$Wert") -and "0".Equals("$Wert")) {return 0} else {
2015-09-23 10:44:43 +00:00
$EinheitEingabe = ($EinheitEingabe.substring(0,1)).replace("K","k")
2015-09-24 06:17:43 +00:00
$EinheitEingabe = ($EinheitEingabe.substring(0,1)).replace("M","m")
2015-09-23 10:44:43 +00:00
$EinheitEingabe = ($EinheitEingabe.substring(0,1)).replace("G","g")
2015-09-24 06:17:43 +00:00
$EinheitEingabe = ($EinheitEingabe.substring(0,1)).replace("T","t")
2015-09-23 10:44:43 +00:00
$EinheitAusgabe = ($EinheitAusgabe.substring(0,1)).replace("K","k")
2015-09-24 06:17:43 +00:00
$EinheitAusgabe = ($EinheitAusgabe.substring(0,1)).replace("M","m")
2015-09-23 10:44:43 +00:00
$EinheitAusgabe = ($EinheitAusgabe.substring(0,1)).replace("G","g")
2015-09-24 06:17:43 +00:00
$EinheitAusgabe = ($EinheitAusgabe.substring(0,1)).replace("T","t")
2019-09-24 01:16:14 +00:00
# Catching size information that does not exist/that are not handled
2015-09-24 06:17:43 +00:00
If ( -not ("b".Equals("$EinheitAusgabe") -or "k".Equals("$EinheitAusgabe") -or "m".Equals("$EinheitAusgabe") -or "g".Equals("$EinheitAusgabe") -or "t".Equals("$EinheitAusgabe"))) {$EinheitAusgabe = "else"}
If ( -not ("b".Equals("$EinheitEingabe") -or "k".Equals("$EinheitEingabe") -or "m".Equals("$EinheitEingabe") -or "g".Equals("$EinheitEingabe") -or "t".Equals("$EinheitEingabe"))) {$EinheitEingabe = "else"}
2015-09-23 10:44:43 +00:00
switch("$EinheitEingabe") {
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"k" { $Wert = $Wert * 1024 }
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"m" {$Wert = $Wert * 1048576}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"g" {$Wert = $Wert * 1073741824}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"t" {$Wert = $Wert * 1099511627776}
2015-09-24 06:17:43 +00:00
"else" {$Wert = 0 }
2015-09-23 10:44:43 +00:00
}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
switch("$EinheitAusgabe") {
2015-09-24 06:17:43 +00:00
"b" {$Wert = [math]::round($Wert, 0)}
2015-09-23 10:44:43 +00:00
"k" {$Wert = $Wert / 1024}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"m" {$Wert = $Wert / 1048576}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"g" {$Wert = $Wert / 1073741824}
2015-09-24 06:17:43 +00:00
2015-09-23 10:44:43 +00:00
"t" {$Wert = $Wert / 1099511627776}
2015-09-24 06:17:43 +00:00
"else" {$Wert = 0 }
2015-09-23 10:44:43 +00:00
}
return $Wert
}
2019-09-24 01:16:14 +00:00
} #[value] [unit outpute] [unit input]
2015-09-23 10:44:43 +00:00
2015-09-24 06:17:43 +00:00
##########################################################################################################
2015-09-23 10:44:43 +00:00
2019-09-24 01:26:28 +00:00
If ($args[0] -eq "--version") {
echo "Version: 1.2"
} ElseIf ($args[0] -eq "--help") {
echo "./logwert.ps1 [log1] [log2] ..."
} Else {
2019-09-24 01:27:06 +00:00
#for each param do start(param)
$args | ForEach-Object { Log_Start $_ }
2015-09-23 10:44:43 +00:00
2019-09-24 01:27:06 +00:00
[String]$sLog_Copy_errors = "$Global:zLog_Copy_errors"
[String]$sLog_Copy_Size = [math]::round((Calc_Byte_Sice $Global:zLog_Copy_Size b $Global:Log_Copy_Size_Einheit), 2)
[String]$sLog_Copy_real_Size = [math]::round((Calc_Byte_Sice $Global:zLog_Copy_real_Size b $Global:Log_Copy_Size_Einheit), 2)
[String]$sLog_Copy_item = "$Global:zLog_Copy_item"
[String]$sLog_Copy_real_item = "$Global:zLog_Copy_real_item"
[String]$sLog_Copy_Size_Einheit = "$Global:Log_Copy_Size_Einheit"
2015-09-23 10:44:43 +00:00
2019-09-24 01:27:06 +00:00
echo "Summary: $sLog_Copy_real_item of $sLog_Copy_item files copied, $sLog_Copy_errors errors, $sLog_Copy_real_Size $sLog_Copy_Size_Einheit of $sLog_Copy_Size $sLog_Copy_Size_Einheit copied"
2019-09-24 01:26:28 +00:00
}