forked from Skatterbrainz/sccm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Send-CmSiteServerLogs.ps1
113 lines (98 loc) · 3.57 KB
/
Send-CmSiteServerLogs.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
<#
.SYNOPSIS
Send-CmSiteServerLogs.ps1 collects SCCM site server logs, compresses
them into a tasty little chocolate-covered ZIP file, and sends it out
through the vast and spacious Internet, using Gmail as the courier
pidgeon.
.DESCRIPTION
The Synopsis should be enough. Or maybe I'm doing this wrong?
.PARAMETER SendFrom
[string] (required) sender's email address
.PARAMETER SendTo
[string] (required) recipient's email address
.PARAMETER LogFilter
[string] (optional) wildcard pattern for collecting only a subset
of log files to send. Example "w*"
.NOTES
Author..... skattebrainz.wordpress.com / GitHub / Twitter
Created.... 10/27/2016
Modified...
Support.... 800.555.1212
USE AT YOUR OWN RISK. AUTHOR IS NOT RESPONSIBLE FOR FLYING BODY PARTS.
#>
param (
[parameter(Mandatory=$True)] [string] $SendFrom,
[parameter(Mandatory=$True)] [string] $SendTo,
[parameter(Mandatory=$False)] [string] $LogFilter = ""
)
$smtp = "smtp.gmail.com"
$port = 587
$Subject = "SCCM Log Sender`: $($env:COMPUTERNAME)"
$temp = $env:TEMP
if ($SendMail) {
$cred = Get-Credential -Message "Provide credentials to use SMTP service" -UserName $SendFrom
}
<#
.SYNOPSIS
Get-CmSiteInstallPath returns [string] path to the base installation
of System Center Configuration Manager on the site server.
.DESCRIPTION
Returns the full SCCM installation path using a registry query.
.NOTES
#>
function Get-CmSiteInstallPath {
$x = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\SMS\setup"
$x.'Installation Directory'
}
# begin the assault!
$cminst = Get-CmSiteInstallPath
if ($cminst -ne $null) {
$cmlogpath = "$cminst\logs"
if (Test-Path $cmlogpath) {
Write-Output "Backing up site server log files..."
$src = "$temp\logtemp"
if (Test-Path $src) {
Write-Output "Removing previous temp folder..."
rd $src -Recurse -Force
}
md $src
if ($LogFilter -ne "") {
Write-Output "Filtering logs: $LogFilter.log"
copy "$cmlogpath\$LogFilter.log" "$src"
$cmlogs = Get-ChildItem -Path $src -Filter "$LogFilter.log"
}
else {
copy "$cmlogpath\*.log" "$src"
$cmlogs = Get-ChildItem -Path $src -Filter "*.log"
}
$lc = $cmlogs.Length
$dest = "$temp\sccmlogs.zip"
if (Test-Path $dest) {
Write-Output "Removing previous archive file..."
del $dest -Force
}
Write-Output "Compressing backup log files..."
Add-Type -AssemblyName "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($src,$dest)
if (Test-Path "$dest") {
$size = $(Get-Item -Path "$dest").Length
$size = [math]::Round($size / 1MB,2)
Write-Output "Archive created: $dest ($size MB)"
if ($SendMail) {
$Message = "Sending $lc SCCM Log files"
Write-Output "Sending email with zip attachment..."
Send-MailMessage -Subject "$Subject" -Body "$Message" -From $SendFrom -To $SendTo -SmtpServer $smtp -Attachments "$dest" -Port $Port -UseSsl -Credential $cred
}
Write-Output "finished!"
}
else {
Write-Output "error: unable to create archive"
}
}
else {
Write-Output "error: unable to locate logs folder"
}
}
else {
Write-Output "error: SCCM installation path not found."
}