param(
[string] $FolderPath = "C:\",
[string] $Extension = "*.undefined",
[int] $FileMinimumSizeByte = 1
)
Function WriteXmlToScreen ([xml]$xml)
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
Try {
$Files = Get-ChildItem -Path $FolderPath -Filter $Extension | Where {$_.Lengthength -gt $FileMinimumSizeByte} | Sort-Object CreationTime -Descending
If ($Files.Count -gt 0) {
Foreach ($File in $Files)
{
$TotalFiles = $Files.Count
$Date = Get-Date
$FileAgeSeconds = [math]::Round(($Date - $File.CreationTime).TotalSeconds)
$FileSize = $File.Length
$FileName = $File.Name
$XML = "<prtg>"
$XML += "<result><channel>Total Files</channel><value>$TotalFiles</value><unit>Count</unit></result>"
$XML += "<result><channel>Newest File - Age in Seconds</channel><value>$FileAgeHours</value><unit>TimeSeconds</unit></result>"
$XML += "<result><channel>Newest File - Size in Bytes</channel><value>$FileSize</value><unit>Byte</unit></result>"
$XML += "<text>$FileName</text>"
$XML += "</prtg>"
WriteXmlToScreen $XML
Break;
}
} else {
$XML = "<prtg>"
$XML += "<result><channel>Total Files</channel><value>0</value><unit>Count</unit></result>"
$XML += "<result><channel>Newest File - Age in Seconds</channel><value>0</value><unit>Count</unit></result>"
$XML += "<result><channel>Newest File - Size in Bytes</channel><value>0</value><unit>Byte</unit></result>"
$XML += "<text>No Files found</text>"
$XML += "</prtg>"
WriteXmlToScreen $XML
}
} Catch {
$XML = "<prtg>"
$XML += "<result><channel>Total Files</channel><value>-1</value><unit>Count</unit></result>"
$XML += "<result><channel>Newest File - Age in Seconds</channel><value>-1</value><unit>Count</unit></result>"
$XML += "<result><channel>Newest File - Size in Bytes</channel><value>-1</value><unit>Byte</unit></result>"
$XML += "<text>Error</text>"
$XML += "</prtg>"
WriteXmlToScreen $XML
}