Files

223 lines
6.9 KiB
PowerShell

$Monitoring_Host = "10.243.14.247"
$URL = "http://" + $Monitoring_Host + ":9801/MDTMonitorData/Computers/"
$Mail_From = "$tsenv:OSDSENDMAILFROM"
$Mail_To = "$tsenv:OSDSendMailToPri"
$Mail_CC = "$tsenv:OSDSendMailToSec"
$Mail_SMTP = New-Object Net.Mail.SmtpClient($tsenv:OSDSendMailSMTPServer)
function Get_My_MonitoringData {
$Data = Invoke-RestMethod $URL
foreach($property in ($Data.content.properties))
{
$Percent = $property.PercentComplete.'#text'
$Current_Steps = $property.CurrentStep.'#text'
$Total_Steps = $property.TotalSteps.'#text'
If ($Current_Steps -eq $Total_Steps)
{
If ($Percent -eq $null)
{
$Step_Status = "Not started"
}
Else
{
$Step_Status = "$Current_Steps / $Total_Steps"
}
}
Else
{
$Step_Status = "$Current_Steps / $Total_Steps"
}
$Step_Name = $property.StepName
If ($Percent -eq 100)
{
$Global:StepName = "Deployment finished"
$Percent_Value = $Percent + "%"
}
Else
{
If ($Step_Name -eq "")
{
If ($Percent -gt 0)
{
$Global:StepName = "Computer restarted"
$Percent_Value = $Percent + "%"
}
Else
{
$Global:StepName = "Deployment not started"
$Percent_Value = "Not started"
}
}
Else
{
$Global:StepName = $property.StepName
$Percent_Value = $Percent + "%"
}
}
$Deploy_Status = $property.DeploymentStatus.'#text'
If (($Percent -eq 100) -and ($Step_Name -eq "") -and ($Deploy_Status -eq 1))
{
$Global:StepName = "Running in PE"
}
$End_Time = $property.EndTime.'#text'
If ($End_Time -eq $null)
{
If ($Percent -eq $null)
{
$EndTime = "Not started"
$Ellapsed = "Not started"
}
Else
{
$EndTime = "Not finished"
$Ellapsed = "Not finished"
}
}
Else
{
$EndTime = ([datetime]$($property.EndTime.'#text')).ToLocalTime().ToString('HH:mm:ss')
$Ellapsed = new-timespan -start ([datetime]$($property.starttime.'#text')).ToString('HH:mm:ss') -end ([datetime]$($property.endTime.'#text')).ToString('HH:mm:ss');
}
New-Object PSObject -Property @{
"Computer Name" = $($property.Name);
"Percent Complete" = $Percent_Value;
"Step Name" = $StepName;
"Step status" = $Step_Status;
Warnings = $($property.Warnings.'#text');
Errors = $($property.Errors.'#text');
"Deployment Status" = $(
Switch ($property.DeploymentStatus.'#text') {
1 { "Running" }
2 { "Failed" }
3 { "Success" }
4 { "Unresponsive" }
Default { "Unknown" }
}
);
"Date" = $($property.StartTime.'#text').split("T")[0];
"Start time" = ([datetime]$($property.StartTime.'#text')).ToLocalTime().ToString('HH:mm:ss')
"End time" = $EndTime;
"Ellapsed time" = $Ellapsed;
}
}
}
$MyData = Get_My_MonitoringData | Select Date, "Computer Name", DeployID, "Percent Complete", PercentNumber, "Step Name", Warnings, Errors, "Start time", "End Time", "Ellapsed time", DeployStatus
Do
{
$Deployment_Date = $MyData.Date
$Deployment_ComputerName = $MyData."Computer name"
$Deployment_Status = $MyData.DeployStatus
$Deployment_LastTime = $MyData.LastTime
$Deployment_StepName = $MyData."Step Name"
$Deployment_Warnings = $MyData.Warnings
$Deployment_Errors = $MyData.Errors
$Deployment_Percent_Complete = $MyData."Percent Complete"
$Deployment_Start_time = $MyData."Start time"
$Deployment_End_time = $MyData."End Time"
$Deployment_Ellapsedtime = $MyData."Ellapsed time"
# COPIER LE SCRIPT DE NOTIFICATION ICI
# SCRIPT DE NOTIFICATION MAIL
If ($Deployment_Status -eq 4) # deployment unresponsive
{
$Body = "<p>
<span>Hello,<br><br>
Operating System deployment <strong><span style=color:red>is unresponsive</span></strong> on the computer: <span style=color:blue><strong>$Deployment_ComputerName</strong></span> - $(get-date)
<br><br>
<span style=color:black>
See below some informations about your deployment :
<br><br>
<strong>Computer name: </strong>$Deployment_ComputerName
<br>
<strong>Start time: </strong>$Start
<br>
<strong>Last time: </strong>$Deployment_LastTime
<br>
<strong>Warning: </strong>$Deployment_Warnings
<br>
<strong>Error: </strong>$Deployment_Errors
<br>
<strong>Step name: </strong>$Deployment_StepName
<br>
<strong>Percent: </strong>$Deployment_Percent_Complete
<br><br>
Best regards
</span>
</span></p>
"
send-mailmessage -from "$Mail_From" -to "$Mail_To" -subject "MDT Monitoring status: Unresponsive - On $Deployment_ComputerName" -body $Body -BodyAsHtml -priority Normal -smtpServer $Mail_SMTP
break
}
ElseIf ($Deployment_Status -eq 3) # Deployment success
{
$Body = "<p>
<span>Hello,<br><br>
Operating System deployment <strong><span style=color:green>completed successfully</span></strong> on the computer: <span style=color:blue><strong>$Deployment_ComputerName</strong></span> - $(get-date)
<br><br>
<span style=color:black>
See below some informations about your deployment :
<br><br>
<strong>Computer name: </strong>$Deployment_ComputerName
<br>
<strong>Start time: </strong>$Deployment_Start_time
<br>
<strong>End time: </strong>$Deployment_End_time
<br>
<strong>Elapsed time: </strong>$Deployment_Ellapsedtime
<br>
<strong>Warning: </strong>$Deployment_Warnings
<br>
<strong>Error: </strong>$Deployment_Errors
<br><br>
Best regards
</span>
</span></p>
"
send-mailmessage -from "$Mail_From" -to "$Mail_To" -subject "MDT Monitoring status: Success - On $Deployment_ComputerName" -body $Body -BodyAsHtml -priority Normal -smtpServer $Mail_SMTP
break
}
ElseIf ($Deployment_Status -eq 2) # Deployment failed
{
$Body = "<p>
<span>Hello,<br><br>
Operating System deployment <strong><span style=color:red>failed</span></strong> on the computer: <span style=color:blue><strong>$Deployment_ComputerName</strong></span> - $(get-date)
<br><br>
<span style=color:black>
See below some informations about your deployment :
<br><br>
<strong>Computer name: </strong>$Deployment_ComputerName
<br>
<strong>Start time: </strong>$Deployment_Start_time
<br>
<strong>Last time: </strong>$Deployment_End_time
<br>
<strong>Warning: </strong>$Deployment_Warnings
<br>
<strong>Error: </strong>$Deployment_Errors
<br>
<strong>Step name: </strong>$Deployment_StepName
<br>
<strong>Percent: </strong>$Deployment_Percent_Complete
<br><br>
Best regards
</span>
</span></p>
"
send-mailmessage -from "$Mail_From" -to "$Mail_To" -Cc "$Mail_CC" -subject "MDT Monitoring status: Failed - On $Deployment_ComputerName" -body $Body -BodyAsHtml -priority Normal -smtpServer $Mail_SMTP
break
}
}
While ($MyData.Percent_Complete -lt 100)
write-host "Deployment finished"