Skip to content

Commit 649d94e

Browse files
committed
Add SCCMLogs script
1 parent 3549afa commit 649d94e

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

SCCMLogs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Write-SCCMLogs.ps1
2+
========
3+
4+
See [tseknet.com/blog/sccmlogs](https://tseknet.com/blog/sccmlogs) to find out
5+
how this script works.

SCCMLogs/Write-SCCMLogs.ps1

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<#
2+
.SYNOPSIS
3+
Export SCCM OS Upgrade logs to the Event Log.
4+
.DESCRIPTION
5+
Regex Parse the SCCM SMSTS.log file and dump to Event Log.
6+
.PARAMETER LogPath
7+
Folder to recursively search for the LogFile.
8+
.PARAMETER LogFile
9+
Location of SMSTS.log on the local system.
10+
.PARAMETER FailureString
11+
The string used to determine when the OS Upgrade failed.
12+
.PARAMETER Source
13+
Specifies the event log source for writing logs.
14+
#>
15+
[CmdletBinding()]
16+
param (
17+
[System.IO.FileInfo]$LogPath = "$env:WINDIR\CCM\Logs\Smstslog",
18+
[System.IO.FileInfo]$LogFile = 'smsts.log',
19+
[string]$FailureString = 'Task sequence execution failed with error code',
20+
[string]$Source = 'InPlaceUpgrade'
21+
)
22+
23+
# Create Log Source if necessary
24+
if (-not [System.Diagnostics.EventLog]::SourceExists($Source)) {
25+
try {
26+
New-EventLog -LogName Application -Source $Source
27+
}
28+
catch {
29+
throw "Failed to create log source: $_"
30+
}
31+
}
32+
33+
$log = "$LogPath\$LogFile"
34+
35+
# Exit if there is no log file to write to.
36+
if (-not (Test-Path $log)) {
37+
throw "Failed to locate SCCM log: $log"
38+
}
39+
40+
# Strip the CMTRACE loginfo from the log file. Logs are wrapped in
41+
# <!\[LOG\[ DATA \]LOG\]!>. All we want is the DATA. Also helps keep
42+
# under the 32KB limit for event log messages.
43+
try {
44+
$formatted_log = Get-Content $log -Raw |
45+
ForEach-Object { $_ -replace '<!\[LOG\[', '' } |
46+
ForEach-Object { $_ -replace '\]LOG\]!>(.*)', '' }
47+
}
48+
catch {
49+
throw "Failed to read log file '$log' with error $_"
50+
}
51+
52+
$length = $formatted_log.Length
53+
# Trim log to under max Event Log size.
54+
if ($length -gt 32766) {
55+
$formatted_log = $formatted_log[($length - 32766)..$length] -join ''
56+
}
57+
58+
# Change log level and prepend custom success/failure string.
59+
if ($formatted_log -match $FailureString) {
60+
$formatted_log = "IPU failed:`n$formatted_log"
61+
$EntryType = 'Error'
62+
}
63+
else {
64+
$formatted_log = "IPU succeeded:`n$formatted_log"
65+
}
66+
67+
try {
68+
$params = @{
69+
LogName = 'Application'
70+
Source = $Source
71+
EntryType = 'Information'
72+
EventId = 1337
73+
Message = $formatted_log
74+
}
75+
Write-EventLog @params
76+
}
77+
catch {
78+
throw "Failed to export SCCM Event Log: $_"
79+
}

0 commit comments

Comments
 (0)