PowerShell Function provides Logging feature;
Following function can be used to provide logging into your
scripts
<#
.NOTES
#=============================================
# Script : Function-Write-Log.ps1
# Created : ISE 3.0
# Author(s) : Casey.Dedeal
# Date : 04/18/2020 10:11:03
# Org : ETC Solutions
# File Name : Function-Write-Log.ps1
# Comments : adding logging function
# Assumptions :
#==============================================
SYNOPSIS : Function-Write-Log.ps1
DESCRIPTION : Log function
Acknowledgements : Open license
Limitations : None
Known issues : None
Credits : Please visit:
https://simplepowershell.blogspot.com
https://msazure365.blogspot.com
.EXAMPLE
.\Function-Write-Log.ps1
#(a)_.Write to log examples
$message1 = 'This is Error'
Write-Log -Message "test" -Severity Error
#(b)_.Write to log examples
$message2 = 'This is Information'
Write-Log -Message "Log message inf" -Severity Information
#(c)_.Write to log examples
$message3 = 'This is Warning '
Write-Log -Message "test" -Severity Warning
MAP:
-----------
#(1)_.Add log Vars, CSV file
#(2)_.Add log Function
#(3)_.Write to log examples
#(4)_.Write to log examples
#(5)_.Write to log examples
#>
#(1)_.Add log Vars, CSV file
$fname
= 'LogFile.CSV'
$luser
= $env:USERNAME
$Logpath = 'C:\Temp\Migration_\Logs_\'
$now
= get-date -format 'dd-MMM-yyyy-HH-mm'
$Logfile = $Logpath + $now + "_" + $fname
#(2)_.Add log Function
function Write-Log {
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateSet('Information','Warning','Error')]
[string]$Severity = 'Information'
)
[pscustomobject]@{
Time =
(Get-Date -f g)
Message = $Message
Severity = $Severity
} | Export-Csv -Path $Logfile -Append -NoTypeInformation
}
#(3)_.Write to log examples
$message1 = 'This is Error'
Write-Log -Message "test" -Severity Error
#(4)_.Write to log examples
$message2 = 'This is Information'
Write-Log -Message "Log message inf" -Severity Information
#(5)_.Write to log examples
$message3 = 'This is Warning '
Write-Log -Message "test" -Severity Warning
|
Azure Solutions
Architect
AWS Certified Cloud
Practitioner
Azure Certified
Security Engineer Associate
No comments:
Post a Comment