Skip to content

Commit 8dbdacc

Browse files
Logging
More changes to the logging
1 parent f882aa7 commit 8dbdacc

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

LT-ScriptExport.ps1

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
Catch {
7171
$ErrorMessage = $_.Exception.Message
7272
$FailedItem = $_.Exception.ItemName
73-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Error durring config creation: $FailedItem, $ErrorMessage `n$Error[0]"
73+
Log-Error -LogPath $FullLogPath -ErrorDesc "Error durring config creation: $FailedItem, $ErrorMessage `n$Error[0]"
7474
}
7575
}
7676
Else {
@@ -87,6 +87,8 @@
8787
#Log File Info
8888
$LogName = "LT-ScriptExport.log"
8989
$LogPath = ($Config.Settings.LogPath)
90+
$FullLogPath = $LogPath + "\" + $LogName
91+
9092

9193
#Location to the backp repository
9294
$BackupRoot = $Config.Settings.BackupRoot
@@ -141,6 +143,7 @@ Function Log-Start{
141143
Creation Date: 7/17/2015
142144
Purpose/Change: Added directory creation if not present.
143145
Added Append option
146+
144147
145148
146149
.EXAMPLE
@@ -152,10 +155,10 @@ Function Log-Start{
152155
Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LogName, [Parameter(Mandatory=$true)][string]$ScriptVersion, [Parameter(Mandatory=$false)][switch]$Append)
153156

154157
Process{
155-
$sFullPath = $LogPath + "\" + $LogName
158+
$FullLogPath = $LogPath + "\" + $LogName
156159
#Check if file exists and delete if it does
157-
If((Test-Path -Path $sFullPath) -and $Append -ne $true){
158-
Remove-Item -Path $sFullPath -Force
160+
If((Test-Path -Path $FullLogPath) -and $Append -ne $true){
161+
Remove-Item -Path $FullLogPath -Force
159162
}
160163

161164
#Check if folder exists if not create
@@ -164,18 +167,18 @@ Function Log-Start{
164167
}
165168

166169
#Create file and start logging
167-
If($(Test-Path -Path $sFullPath) -ne $true) {
170+
If($(Test-Path -Path $FullLogPath) -ne $true) {
168171
New-Item -Path $LogPath -Value $LogName -ItemType File
169172
}
170173

171-
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
172-
Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
173-
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
174-
Add-Content -Path $sFullPath -Value ""
175-
Add-Content -Path $sFullPath -Value "Running script version [$ScriptVersion]."
176-
Add-Content -Path $sFullPath -Value ""
177-
Add-Content -Path $sFullPath -Value "***************************************************************************************************"
178-
Add-Content -Path $sFullPath -Value ""
174+
Add-Content -Path $FullLogPath -Value "***************************************************************************************************"
175+
Add-Content -Path $FullLogPath -Value "Started processing at [$([DateTime]::Now)]."
176+
Add-Content -Path $FullLogPath -Value "***************************************************************************************************"
177+
Add-Content -Path $FullLogPath -Value ""
178+
Add-Content -Path $FullLogPath -Value "Running script version [$ScriptVersion]."
179+
Add-Content -Path $FullLogPath -Value ""
180+
Add-Content -Path $FullLogPath -Value "***************************************************************************************************"
181+
Add-Content -Path $FullLogPath -Value ""
179182

180183
#Write to screen for debug mode
181184
Write-Debug "***************************************************************************************************"
@@ -221,15 +224,15 @@ Function Log-Write{
221224
Purpose/Change: Added debug mode support
222225
223226
.EXAMPLE
224-
Log-Write -LogPath "C:\Windows\Temp\Test_Script.log" -LineValue "This is a new line which I am appending to the end of the log file."
227+
Log-Write -FullLogPath "C:\Windows\Temp\Test_Script.log" -LineValue "This is a new line which I am appending to the end of the log file."
225228
#>
226229

227230
[CmdletBinding()]
228231

229-
Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LineValue)
232+
Param ([Parameter(Mandatory=$true)][string]$FullLogPath, [Parameter(Mandatory=$true)][string]$LineValue)
230233

231234
Process{
232-
Add-Content -Path $LogPath -Value $LineValue
235+
Add-Content -Path $FullLogPath -Value $LineValue
233236

234237
Write-Output $LineValue
235238

@@ -271,17 +274,17 @@ Function Log-Error{
271274
Author: Luca Sturlese
272275
Creation Date: 19/05/12
273276
Purpose/Change: Added debug mode support. Added -ExitGracefully parameter functionality
274-
277+
275278
.EXAMPLE
276279
Log-Error -LogPath "C:\Windows\Temp\Test_Script.log" -ErrorDesc $_.Exception -ExitGracefully $True
277280
#>
278281

279282
[CmdletBinding()]
280283

281-
Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$ErrorDesc, [Parameter(Mandatory=$true)][boolean]$ExitGracefully)
284+
Param ([Parameter(Mandatory=$true)][string]$FullLogPath, [Parameter(Mandatory=$true)][string]$ErrorDesc, [Parameter(Mandatory=$true)][boolean]$ExitGracefully)
282285

283286
Process{
284-
Add-Content -Path $LogPath -Value "Error: An error has occurred [$ErrorDesc]."
287+
Add-Content -Path $FullLogPath -Value "Error: An error has occurred [$ErrorDesc]."
285288

286289
Write-Error $ErrorDesc
287290

@@ -290,7 +293,7 @@ Function Log-Error{
290293

291294
#If $ExitGracefully = True then run Log-Finish and exit script
292295
If ($ExitGracefully -eq $True){
293-
Log-Finish -LogPath $LogPath
296+
Log-Finish -LogPath $FullLogPath
294297
Breaåk
295298
}
296299
}
@@ -310,6 +313,9 @@ Function Log-Finish{
310313
.PARAMETER NoExit
311314
Optional. If this is set to True, then the function will not exit the calling script, so that further execution can occur
312315
316+
.PARAMETER Limit
317+
Optional. Sets the max linecount of the script.
318+
313319
.INPUTS
314320
Parameters above
315321
@@ -332,8 +338,11 @@ Function Log-Finish{
332338
Creation Date: 01/08/12
333339
Purpose/Change: Added option to not exit calling script if required (via optional parameter)
334340
341+
Version: 1.3
342+
Author: Chris Taylor
343+
Creation Date: 7/17/2015
344+
Purpose/Change: Added log line count limit.
335345
336-
337346
.EXAMPLE
338347
Log-Finish -LogPath "C:\Windows\Temp\Test_Script.log"
339348
@@ -343,13 +352,13 @@ Function Log-Finish{
343352

344353
[CmdletBinding()]
345354

346-
Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$false)][string]$NoExit, [Parameter(Mandatory=$false)][int]$Limit )
355+
Param ([Parameter(Mandatory=$true)][string]$FullLogPath, [Parameter(Mandatory=$false)][string]$NoExit, [Parameter(Mandatory=$false)][int]$Limit )
347356

348357
Process{
349-
Add-Content -Path $LogPath -Value ""
350-
Add-Content -Path $LogPath -Value "***************************************************************************************************"
351-
Add-Content -Path $LogPath -Value "Finished processing at [$([DateTime]::Now)]."
352-
Add-Content -Path $LogPath -Value "***************************************************************************************************"
358+
Add-Content -Path $FullLogPath -Value ""
359+
Add-Content -Path $FullLogPath -Value "***************************************************************************************************"
360+
Add-Content -Path $FullLogPath -Value "Finished processing at [$([DateTime]::Now)]."
361+
Add-Content -Path $FullLogPath -Value "***************************************************************************************************"
353362

354363
#Write to screen for debug mode
355364
Write-Debug ""
@@ -359,7 +368,7 @@ Function Log-Finish{
359368

360369
if ($Limit){
361370
#Limit Log file to 50000 lines
362-
(Get-Content $LogPath -tail $Limit -readcount 0) | Set-Content $LogPath -Force -Encoding Unicode
371+
(Get-Content $FullLogPath -tail $Limit -readcount 0) | Set-Content $FullLogPath -Force -Encoding Unicode
363372
}
364373
#Exit calling script if NoExit has not been specified or is set to False
365374
If(!($NoExit) -or ($NoExit -eq $False)){
@@ -427,7 +436,7 @@ Function Get-LTData {
427436
}
428437

429438
Catch {
430-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to run query : $query `n$Error[0]"
439+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to run query : $query `n$Error[0]"
431440
}
432441
}
433442

@@ -563,7 +572,7 @@ Function Export-LTScript {
563572
Catch {
564573
$ErrorMessage = $_.Exception.Message
565574
$FailedItem = $_.Exception.ItemName
566-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to remove folder data from XML: $FailedItem, $ErrorMessage `n$Error[0]"
575+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to remove folder data from XML: $FailedItem, $ErrorMessage `n$Error[0]"
567576
}
568577
}
569578
Else {
@@ -572,8 +581,8 @@ Function Export-LTScript {
572581

573582
#Check if folder is no longer present.
574583
if ($FolderData -eq $null) {
575-
Log-Write -LogPath "$($LogPath)\$($LogName)" -LineValue "ScritID $($ScriptXML.ScriptId) references folder $($ScriptXML.FolderId), this folder is no longer present. Setting to root folder."
576-
Log-Write -LogPath "$($LogPath)\$($LogName)" -LineValue "It is recomended that you move this script to a folder."
584+
Log-Write -FullLogPath $FullLogPath -LineValue "ScritID $($ScriptXML.ScriptId) references folder $($ScriptXML.FolderId), this folder is no longer present. Setting to root folder."
585+
Log-Write -FullLogPath $FullLogPath -LineValue "It is recomended that you move this script to a folder."
577586

578587
#Set to FolderID 0
579588
$ExportTemplate.LabTech_Expansion.PackedScript.NewDataSet.Table.FolderId = "0"
@@ -586,7 +595,7 @@ Function Export-LTScript {
586595
Catch {
587596
$ErrorMessage = $_.Exception.Message
588597
$FailedItem = $_.Exception.ItemName
589-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to remove folder data from XML: $FailedItem, $ErrorMessage `n$Error[0]"
598+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to remove folder data from XML: $FailedItem, $ErrorMessage `n$Error[0]"
590599
}
591600
}
592601
Else {
@@ -622,7 +631,7 @@ Function Export-LTScript {
622631
Catch {
623632
$ErrorMessage = $_.Exception.Message
624633
$FailedItem = $_.Exception.ItemName
625-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to save script: $FailedItem, $ErrorMessage `n$Error[0]"
634+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to save script: $FailedItem, $ErrorMessage `n$Error[0]"
626635
}
627636
}
628637
Else {
@@ -653,7 +662,7 @@ Function Export-LTScript {
653662
Catch {
654663
$ErrorMessage = $_.Exception.Message
655664
$FailedItem = $_.Exception.ItemName
656-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to save script: $FailedItem, $ErrorMessage `n$Error[0]"
665+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to save script: $FailedItem, $ErrorMessage `n$Error[0]"
657666
}
658667
}
659668

@@ -673,11 +682,11 @@ Function Export-LTScript {
673682
Catch {
674683
$ErrorMessage = $_.Exception.Message
675684
$FailedItem = $_.Exception.ItemName
676-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Error durring log/backup directory creation: $FailedItem, $ErrorMessage `n$Error[0]"
685+
Log-Error -LogPath $FullLogPath -ErrorDesc "Error durring log/backup directory creation: $FailedItem, $ErrorMessage `n$Error[0]"
677686
}
678687

679-
Log-Write -LogPath "$($LogPath)\$($LogName)" -LineValue "Getting list of all scripts."
680-
688+
Log-Write -FullLogPath $FullLogPath -LineValue "Getting list of all scripts."
689+
681690
$ScriptIDs = @{}
682691
#Query list of all ScriptID's
683692
if ($($Config.Settings.LastExport) -eq 0) {
@@ -688,7 +697,7 @@ Function Export-LTScript {
688697
$ScriptIDs = Get-LTData $Query
689698
}
690699

691-
Log-Write -LogPath "$($LogPath)\$($LogName)" -LineValue "$(@($ScriptIDs).count) scripts to process."
700+
Log-Write -FullLogPath $FullLogPath -LineValue "$(@($ScriptIDs).count) scripts to process."
692701

693702
#Process each ScriptID
694703
$n = 0
@@ -701,7 +710,7 @@ Function Export-LTScript {
701710
Export-LTScript -ScriptID $($ScriptID.ScriptID)
702711
}
703712

704-
Log-Write -LogPath "$($LogPath)\$($LogName)" -LineValue "Export finished."
713+
Log-Write -FullLogPath $FullLogPath -LineValue "Export finished."
705714

706715
try {
707716
$Config.Settings.LastExport = "$($Date.ToString("yyy-MM-dd HH:mm:ss"))"
@@ -710,12 +719,9 @@ Function Export-LTScript {
710719
Catch {
711720
$ErrorMessage = $_.Exception.Message
712721
$FailedItem = $_.Exception.ItemName
713-
Log-Error -LogPath "$($LogPath)\$($LogName)" -ErrorDesc "Unable to update config with last export date: $FailedItem, $ErrorMessage `n$Error[0]"
722+
Log-Error -LogPath $FullLogPath -ErrorDesc "Unable to update config with last export date: $FailedItem, $ErrorMessage `n$Error[0]"
714723
}
715724

716-
Log-Finish -LogPath "$($LogPath)\$($LogName)" -NoExit $True
717-
718-
#Limit Log file to 50000 lines
719-
(Get-Content "$($LogPath)\$($LogName)" -tail 50000 -readcount 0) | Set-Content "$($LogPath)\$($LogName)" -Force -Encoding Unicode
725+
Log-Finish -FullLogPath $FullLogPath
720726

721727
#endregion

0 commit comments

Comments
 (0)