Hello,
I have a function which checks for certain event id by accepting 2 parameter values, i.e; ServerName and BackupType. For some reason I am unable to get any output for the function.
Function Check-CMBackup
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, HelpMessage="SCCM Server Name", ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string]$ServerName,
[Parameter(Mandatory=$True, HelpMessage="SCCM BackupType", ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullorEmpty()]
[ValidateSet('Site','DB','Both')]
[string]$BackupType
)
BEGIN
{
$a = Get-Date
$b = $a.AddDays(-1)
$b = $b.ToShortDateString();
$c = Get-Date
$c = $c.ToShortDateString();
$StartTime = "10:00:00 PM"
$EndTime = "06:00:00 AM"
$before = $c + " " + $EndTime
$after = $b + " " + $StartTime
$before = [datetime]$before
$after = [datetime]$after
$props = [ordered]@{
ServerName = $ServerName
SiteBackup = $null
DBBackup =$null
Date = Get-Date -Format "yyyy-MM-dd";
Time = Get-Date -Format "HH:mm:ss";
}
}
PROCESS
{
if(Test-Connection -ComputerName $ServerName -BufferSize 2 -Count 3)
{
if($BackupType -eq 'Site')
{
Try
{
if($sbkp = Get-EventLog -ComputerName $SiteServer -LogName Application -EntryType Information -after $after -before $before -Source "SMS Server"|?{$_.EventID -eq 6833})
{
$props.SiteBackup = "Success"
}
else
{
$props.SiteBackup = "Failed"
}
New-Object -TypeName PSObject -Property $props
}
Catch
{
$props.SiteBackup = "Error"
}
}
if($BackupType -eq 'DB')
{
Try
{
if($sbkp = Get-EventLog -ComputerName $DBServer -LogName Application -EntryType Information -after $after -before $before -Source "SMS Server"|?{$_.EventID -eq 18264})
{
$props.DBBackup = "Success"
}
else
{
$props.DBBackup = "Failed"
}
New-Object -TypeName PSObject -Property $props
}
Catch
{
$props.DBBackup = "Error"
}
}
if($BackupType -eq 'Both')
{
Try
{
if($sbkp = Get-EventLog -ComputerName $SiteServer -LogName Application -EntryType Information -after $after -before $before -Source "SMS Server"|?{$_.EventID -eq 6833})
{
$props.SiteBackup = "Success"
}
else
{
$props.SiteBackup = "Failed"
}
if($sbkp = Get-EventLog -ComputerName $DBServer -LogName Application -EntryType Information -after $after -before $before -Source "SMS Server"|?{$_.EventID -eq 18264})
{
$props.DBBackup = "Success"
}
else
{
$props.DBBackup = "Failed"
}
New-Object -TypeName PSObject -Property $props
}
Catch
{
$props.DBBackup = "Error"
}
}
}
else
{
$props.SiteBackup = "Offline"
$props.DBBackup = "Offline"
}
}
}