A PowerShell script renames a log folder to function as an archive, creates a new log folder, copies the ACL from the old folder to the new one, then deletes the oldest archives if there are more archives than desired. Snippet follows--
If ( $NumberOfLogsToKeep -gt 0) {
# archive recent logs
$dateStamp = get-date -uformat "%Y-%m-%d@%H-%M-%S"
$archiveFolder = "$LogFolder-$dateStamp"
Rename-Item $LogFolder $archiveFolder
New-Item $LogFolder -ItemType Directory
Get-ACL $archiveFolder | Set-ACL $LogFolder
# prune stale log folders
If ( Test-Path "$PSScriptRoot\Logs-*" ) {
$archiveFolders = Get-ChildItem -Path "$PSScriptRoot\Logs-*" -Directory | Sort-Object -Descending
$i = 0
$archiveFolders | ForEach-Object {
$i = $i + 1
If ( $i -gt $NumberOfLogsToKeep ) {
Remove-Item $_ -Recurse
}
}
}
}
$NumberOfLogsToKeep and $LogFolder (a share hosted on a remote server) are specified in a "preferences" section at the top of the script.
On a WS2019 domain and WS2019 server, works perfectly when run as a Scheduled Task using Domain Admin creds. Does not work at all when run as a Scheduled Task using NT AUTHORITY\SYSTEM creds. The log folder is not renamed; don't know if the rest of the code would run correctly because it has nothing to do if the log folder is not renamed. SYSTEM has full control on the folder, subfolders, and files.
But on a different, WS2012R2 domain, on a WS2012R2 server, runs perfectly as a Scheduled Task as NT AUTHORITY\SYSTEM.
Is this a change in WS2019 PowerShell? For security?
TIA