Script cleanup

This commit is contained in:
2018-11-18 22:56:16 +01:00
parent f575f3ea33
commit d8acbdd773

View File

@@ -65,98 +65,36 @@ function Build-XML($Job, $LastResult)
} }
$command = @' function Test-XMLFile
cmd.exe /C "C:\Program Files\Acronis\CommandLineTool\acrocmd.exe" list plans --host=$Server --credentials=$User,$Password --log="c:\tmp\AcronisBackupStatus.xml"
'@
################# erstellen der XML datei und einlesen
function Test-XMLFile {
<#
.SYNOPSIS
Test the validity of an XML file
#>
[CmdletBinding()]
param (
[parameter(mandatory=$true)][ValidateNotNullorEmpty()][string]$xmlFilePath
)
# Check the file exists
if (!(Test-Path -Path $xmlFilePath))
{ {
throw "$xmlFilePath is not valid. Please provide a valid path to the .xml fileh" [CmdletBinding()]
} param (
[parameter(mandatory=$true)][ValidateNotNullorEmpty()][string]$xmlFilePath
)
# Check for Load or Parse errors when loading the XML file if(!(Test-Path -Path $xmlFilePath))
$xml = New-Object System.Xml.XmlDocument {
try { throw "$xmlFilePath is not valid. Please provide a valid path to the .xml fileh"
$xml.Load((Get-ChildItem -Path $xmlFilePath).FullName)
return $true
}
catch [System.Xml.XmlException] {
Write-Verbose "$xmlFilePath : $($_.toString())"
return $false
}
}
Invoke-Expression -Command:$command
$Data = New-Object xml
$Data.psbase.PreserveWhitespace = $false
while ((Test-XMLFile "c:\tmp\AcronisBackupStatus.xml") -eq $false ) {
$null = Invoke-Expression -Command:$command
}
$Data.Load("c:\tmp\AcronisBackupStatus.xml")
ForEach( $Plan in $Data.list_plans.output_data.list.plan ) {
$Plan.Status = $Plan.Status.Replace("`r`n","")
$Plan.Status = $Plan.Status.Replace(" ","")
$Plan.Status = $Plan.Status.ToLower()
If ($Plan.Status -eq "ok") {
$errorvalue = 0
} else {
$errorvalue = $errorvalue + 1
$text = $text + $Plan.Name + "has Status " + $Plan.Status + " ! "
} }
$Readdate = [datetime]::ParseExact($Plan.next_start.substring(0,19), "dd.MM.yyyy HH:mm:ss", $null)
if ($Readdate -ge $curdate) { $xml = New-Object System.Xml.XmlDocument
} else { try
$errorvalue = $errorvalue + 1 {
$text = $text + $Plan.Name + "has Next Start Time in the Past : " + $Readdate + " ! " $xml.Load((Get-ChildItem -Path $xmlFilePath).FullName)
return $true
} }
$Plan.State = $Plan.State.Replace("`r`n","") catch [System.Xml.XmlException]
$Plan.State = $Plan.State.Replace(" ","") {
$Plan.State = $Plan.State.ToLower() Write-Verbose "$xmlFilePath : $($_.toString())"
$Plan.State return $false
If ($Plan.State -eq "need_interaction") {
$errorvalue = $errorvalue + 1
$text = $text + $Plan.Name + "has State Need Interaction ! "
} else {
} }
} }
write-host "<prtg>"
Build-XML Server1 ok
Build-XML Server2 Error
write-host "</prtg>"
"<prtg>"
"<result>"
"<channel>"
"BackupStatus"
"</channel>"
"<unit>Custom</unit>"
"<CustomUnit>Status</CustomUnit>"
"<FLOAT>0</FLOAT>"
"<value>"
echo $errorvalue
"</value>"
"<LimitMaxError>0,5</LimitMaxError>"
"<LimitMode>1</LimitMode>"
"</result>"
"<Text>"
If ($errorvalue -eq 0) {
echo "Alles Ok"
} else {
echo $text
}
"</Text>"
"</prtg>"