-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSccm.OsBuild.Failure.ps1
140 lines (106 loc) · 4.09 KB
/
Sccm.OsBuild.Failure.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
$ErrorActionPreference = 'stop'
Set-PSDebug -Strict
$PSScriptDirectory = (Split-Path $MyInvocation.MyCommand.Path -Parent).ToLower()
Import-Module ($PSScriptDirectory + '\Sccm.OsBuild.Logic.psm1') -Verbose
Import-Module ($PSScriptDirectory + '\Sccm.OsBuild.Data.psm1') -Verbose
$_settingsFileJson = Get-Content ($PSScriptDirectory + '\Sccm.OsBuild.Scripts.Settings.json') -Raw | ConvertFrom-Json
$_smtpServer = $_settingsFileJson.Generic_Settings.SMTP_Server
$_cssStle = $_settingsFileJson.Generic_Settings.CSS_Style
$_emailTo = $_settingsFileJson.Generic_Settings.Email_To
$_sccmOsdReportURL = $_settingsFileJson.Sccm_OsBuild_Failure.SCCMOsdReportURL
function Send-CustomMailMessage($smtpFrom, $displayName, $subject, $body, $smtpTo, $attachments = $null, $html = $true)
{
<#
.Synopsis
Sends emails by relay
.Description
Can be used to send emails as someone else
.Example
Send-CustomMailMessage $_mailErrorTo 'belal, abu' 'relayed emailed' 'body of email' '[email protected]'
.Notes
AUTHOR: Abu Belal
LASTEDIT: 06-June-2013
EDITS: Removed logging info
Removed try catch around send mail, callee should handle this
#>
$developer = "[email protected]"
$from = New-Object System.Net.Mail.MailAddress($smtpFrom, $displayName)
$mail = New-Object System.Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($_smtpServer)
$mail.From = $from
$mail.To.Add($smtpTo)
#$mail.BCC.Add($_mailErrorToCc)
$mail.Subject = $subject
$mail.Body = $body
$mail.IsBodyHtml = $html
if ($attachments -ne $null)
{
foreach ($attachement in $attachments)
{
$mail.Attachments.Add($attachement)
}
}
$smtp.Send($mail)
}
function Get-CustomMDTComputerSettings
{
$systemDetails = Get-CustomSystemDetails
$macAddresses = New-Object System.Collections.Generic.List[string]
if ($systemDetails.mac.count)
{
if ($systemDetails.mac.count -gt 1)
{
foreach ($macAddress in $systemDetails.mac)
{
$macAddresses.Add($macAddress)
}
$macAddresses.Sort()
$systemDetails.mac = $macAddresses[1]
}
}
$mdtComputerRecord = Get-SccmComputerSettings -macAddress $systemDetails.mac
return $mdtComputerRecord
}
function Add-EmailTableData($property, $value)
{
[Void] $_emailLog.Append('<tr>')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('<td>' + $property + '</td>')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('<td>' + $value + '</td>')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('</tr>')
[Void] $_emailLog.AppendLine()
}
$_systemDetails = Get-CustomSystemDetails
$_emailLog = New-Object System.Text.StringBuilder
[Void] $_emailLog.Append('<html><head>' + $_cssStle + '</head><body>')
[Void] $_emailLog.Append('<h2 align="center">Build Failed</h2>')
[Void] $_emailLog.Append('View realtime build report by clicking <a href="' + $_sccmOsdReportURL + $_systemDetails.mac + '">here </a> and review for problems')
[Void] $_emailLog.Append('<br><br>')
[Void] $_emailLog.Append('<table id=#buildresult cellspacing="0" border="0">')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('<tr>')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('<td>Property</td>')
[Void] $_emailLog.AppendLine()
[Void] $_emailLog.Append('<td>Value</td>')
[Void] $_emailLog.Append('</tr>')
[Void] $_emailLog.AppendLine()
$_mdtComputerSettings = Get-SccmComputerSettings -macAddress $_systemDetails.Item('mac')
foreach ($property in $_mdtComputerSettings.Table.Columns)
{
if (($_mdtComputerSettings[$property.ColumnName]) -ne [System.DBNull]::Value)
{
Write-Host $property.ColumnName $_mdtComputerSettings[$property.ColumnName]
Add-EmailTableData $property.ColumnName $_mdtComputerSettings[$property.ColumnName]
}
}
$_appliedRoles = Get-CustomAppliedRoles $_mdtComputerSettings['ID']
foreach ($role in $_appliedRoles)
{
Add-EmailTableData 'Roles' $role.Role
}
[Void] $_emailLog.Append('</table>')
[Void] $_emailLog.AppendLine()
Send-CustomMailMessage ('Sccm.OsBuild.Failure@' + $_mdtComputerSettings['OSDComputerName']) 'Sccm.OsBuild.Failure' ('Build FAILED on ' + $_mdtComputerSettings['OSDComputerName']) $_emailLog $_emailTo