-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLogging Function.ps1
72 lines (58 loc) · 2.46 KB
/
Logging Function.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
# v1.1 - can be used with AA, Function will check if running onprem, AA or Hybrid
# v1.0 - Init
#region Parameters
[string]$LogPath = "D:\_SCOWorkingDir\PowerShell\Warranty Info" #Path to store the Lofgile, only local or Hybrid
[string]$LogfileName = "GetWarranty" #FileName of the Logfile, only local or Hybrid
[int]$DeleteAfterDays = 10 #Time Period in Days when older Files will be deleted, only local or Hybrid
#endregion Parameters
#region Function
function Write-TechguyLog {
[CmdletBinding()]
param
(
[ValidateSet('DEBUG', 'INFO', 'WARNING', 'ERROR')]
[string]$Type,
[string]$Text
)
#Decide Platform
$environment = "local"
if ($env:AZUREPS_HOST_ENVIRONMENT) { $environment = "AAnoHybrid" }
if ($env:AUTOMATION_WORKER_CERTIFICATE) { $environment = "AAHybrid" }
if ($environment -eq "AAHybrid" -or $environment -eq "local") {
# Set logging path
if (!(Test-Path -Path $logPath)) {
try {
$null = New-Item -Path $logPath -ItemType Directory
Write-Verbose ("Path: ""{0}"" was created." -f $logPath)
}
catch {
Write-Verbose ("Path: ""{0}"" couldn't be created." -f $logPath)
}
}
else {
Write-Verbose ("Path: ""{0}"" already exists." -f $logPath)
}
[string]$logFile = '{0}\{1}_{2}.log' -f $logPath, $(Get-Date -Format 'yyyyMMdd'), $LogfileName
$logEntry = '{0}: <{1}> {2}' -f $(Get-Date -Format yyyyMMdd_HHmmss), $Type, $Text
Add-Content -Path $logFile -Value $logEntry
}
elseif ($environment -eq "AAHybrid" -or $environment -eq "AAnoHybrid") {
$logEntry = '{0}: <{1}> {2}' -f $(Get-Date -Format yyyyMMdd_HHmmss), $Type, $Text
switch ($Type) {
INFO { Write-Output $logEntry }
WARNING { Write-Warning $logEntry }
ERROR { Write-Error $logEntry }
DEBUG { Write-Output $logEntry }
Default { Write-Output $logEntry }
}
}
}
#endregion Function
Write-TechguyLog -Type INFO -Text "START Script"
#Clean Logs
if ($environment -eq "AAHybrid" -or $environment -eq "local") {
Write-TechguyLog -Type INFO -Text "Clean Log Files"
$limit = (Get-Date).AddDays(-$DeleteAfterDays)
Get-ChildItem -Path $LogPath -Filter "*$LogfileName.log" | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
}
Write-TechguyLog -Type INFO -Text "END Script"