-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLtPatchLog.psm1
More file actions
145 lines (137 loc) · 5.18 KB
/
Copy pathLtPatchLog.psm1
File metadata and controls
145 lines (137 loc) · 5.18 KB
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
141
142
143
144
145
function Get-LtPatchingFile {
<#
.DESCRIPTION
Finds the patching LabTech log file given a computername.
.EXAMPLE
$temp = Get-Content computers.txt | Get-LtPatchingFile | Import-LtPatchingLog
.INPUTS
Inputs to this cmdlet can be a list of Windows computer hostnames or IPs.
.OUTPUTS
Output from this cmdlet is a io.fileinfo object.
#>
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
Param
(
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[Alias('Name', '__SERVER', 'CN', 'Computer')]
[string]
$ComputerName = $env:COMPUTERNAME
)
Begin {
# Establish common/initial parameter values
$WmiSplat = @{
Class = 'Win32_Service'
Filter = "Name='LtService'"
ComputerName = $null
}
}
Process {
Foreach ($Computer in $ComputerName) {
# Set the target device (local OK)
$WmiSplat.Set_Item('ComputerName', $Computer)
#Get the path from local POV
$AgentLocalParentPath = (Get-WmiObject @WmiSplat).PathName.Trim() -replace '\\LtSvc\.exe\s.*'
# Add file name to path for FullName
$LocalLogPath = Join-Path $AgentLocalParentPath 'LtPatching.txt'
# Convert to SMB Path
$SmbLogPath = if ($Computer -ne $env:COMPUTERNAME) {
"\\$($Computer)\$($LocalLogPath -replace ':','$')"
}
else {
$LocalLogPath
}#$SmbLogPath = if ($Computer -ne $env:COMPUTERNAME) {
# Write object to pipeline
Get-Item $SmbLogPath
}#Foreach ($Computer in $ComputerName) {
}
End {
}
}
function Import-LtPatchingLog {
<#
.DESCRIPTION
Imports log entries from a specificly formatted LabTech log file.
.EXAMPLE
$temp = Get-LtPatchingFile | Import-LtPatchingLog
.INPUTS
Inputs to this cmdlet come from the Get-LtPatchingFile function.
.OUTPUTS
Output from this cmdlet is a psobject that can be consumed by other functions where noted.
#>
[CmdletBinding()]
[OutputType([psobject])]
Param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $true,
Position = 0)]
[string]
[Alias('FullName')]
$Path
)
Begin {
$ptnLtPatchingLogLine = '(\w+?)\s\sv(\d{3}\.\d{3})\s+?\-\s(\d{1,2}\/\d{1,2}\/20\d{2}\s\d{1,2}:\d{2}:\d{2}\s?[AP]?M?)\s+?-\s(.+?):::'
$USTimeFormat = 'M/d/yyyy h:mm:ss tt'
$UKTimeFormat = 'dd/MM/yyyy HH:mm:ss'
}
Process {
Foreach ($FullName in $Path) {
# Pull apart the Full Path to grab the computername
$Computer = if ($FullName -match '^\\\\') {
$FullName -replace '^\\\\' -replace '\\.*'
}
else {$env:COMPUTERNAME}
# Get the log content
$LogContent = Get-Content $FullName
# Match the content, line by line
$i = 0
Foreach ($line in $LogContent) {
$i++
$Groups = [regex]::Match($line, $ptnLtPatchingLogLine).Groups
# Datetime handling of unknown culture
$strDate = ($Groups | Where-Object {$_.Name -eq 3}).Value
Try{
# Try getting the date string to a datetime
$TimeGenerated = Get-Date $strDate -ea Stop
}
Catch{
Try{
# Try to force the UK time format
$TimeGenerated = Get-Date $strDate -Format $UKTimeFormat -ea Stop
}
Catch{
Try{
# Try to force the US time format
$TimeGenerated = Get-Date $strDate -Format $USTimeFormat -ea Stop
}
Catch{
Try{
# Extract the default Date/Time formatting from the remote Culture
$TimeGenerated = ConvertTo-RemoteDateCulture -Date (
$strDate) -ComputerName $Computer -ea Stop
}
Catch{
# Give up, the line number is good enough
$TimeGenerated = $null
}
}
}
}
New-Object psobject -Property @{
LineNumber = $i
ComputerName = $Computer
Service = [string](($Groups | Where-Object {$_.Name -eq 1}).Value)
Version = [version](($Groups | Where-Object {$_.Name -eq 2}).Value)
TimeGenerated = $TimeGenerated
Message = [string](($Groups | Where-Object {$_.Name -eq 4}).Value)
}#New-Object psobject -Property @{
}#Foreach ($line in $LogContent){
}#Foreach ($item in $Path){
}
End {
}
}