2023-10-03 08:18:00 +00:00
|
|
|
#Write to Log Function
|
2022-03-14 13:55:02 +00:00
|
|
|
|
2023-10-17 20:35:36 +00:00
|
|
|
function Write-ToLog {
|
|
|
|
|
|
|
|
[CmdletBinding()]
|
|
|
|
param(
|
|
|
|
[Parameter()] [String] $LogMsg,
|
|
|
|
[Parameter()] [String] $LogColor = "White",
|
|
|
|
[Parameter()] [Switch] $IsHeader = $false
|
|
|
|
)
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-17 14:29:22 +00:00
|
|
|
#Create file if doesn't exist
|
|
|
|
if (!(Test-Path $LogFile)) {
|
|
|
|
New-Item -ItemType File -Path $LogFile -Force | Out-Null
|
|
|
|
|
|
|
|
#Set ACL for users on logfile
|
|
|
|
$NewAcl = Get-Acl -Path $LogFile
|
|
|
|
$identity = New-Object System.Security.Principal.SecurityIdentifier S-1-5-11
|
|
|
|
$fileSystemRights = "Modify"
|
|
|
|
$type = "Allow"
|
|
|
|
$fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $type
|
|
|
|
$fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList
|
|
|
|
$NewAcl.SetAccessRule($fileSystemAccessRule)
|
|
|
|
Set-Acl -Path $LogFile -AclObject $NewAcl
|
|
|
|
}
|
|
|
|
|
|
|
|
#If header requested
|
|
|
|
if ($IsHeader) {
|
2024-07-21 03:23:40 +00:00
|
|
|
$Log = "#"*65 + "`n#`t$(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n" + "#"*65
|
2023-10-17 14:29:22 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$Log = "$(Get-Date -UFormat "%T") - $LogMsg"
|
|
|
|
}
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2023-10-03 08:18:00 +00:00
|
|
|
#Echo log
|
|
|
|
$Log | Write-host -ForegroundColor $LogColor
|
|
|
|
|
|
|
|
#Write log to file
|
|
|
|
$Log | Out-File -FilePath $LogFile -Append
|
2023-09-15 14:40:37 +00:00
|
|
|
|
2022-10-26 22:49:10 +00:00
|
|
|
}
|