wingetautoupdate/Winget-AutoUpdate/functions/Write-ToLog.ps1

42 lines
1.4 KiB
PowerShell
Raw Permalink Normal View History

#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-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) {
2023-11-16 21:51:21 +00:00
$Log = " `n###########################################################`n# $LogMsg - $(Get-Date -Format (Get-culture).DateTimeFormat.ShortDatePattern)`n###########################################################`n"
2023-10-17 14:29:22 +00:00
}
else {
$Log = "$(Get-Date -UFormat "%T") - $LogMsg"
}
#Echo log
$Log | Write-host -ForegroundColor $LogColor
#Write log to file
$Log | Out-File -FilePath $LogFile -Append
2022-10-26 22:49:10 +00:00
}