Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hagatorn/7be445c5037ba78b969e966e9ef17276 to your computer and use it in GitHub Desktop.
Save hagatorn/7be445c5037ba78b969e966e9ef17276 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2017-04-25T15:39:00.4152962</Date>
<Author>DOMAIN\User</Author>
<Description>Triggers only on Exam Accounts 01-18 log-off events and clears just the logging off users files.</Description>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Security"&gt;&lt;Select Path="Security"&gt;
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and Task = 12545 and (EventID=4634)]]
and
*[EventData[Data and (
Data='Exam01' or
Data='Exam02' or
Data='Exam03' or
Data='Exam04' or
Data='Exam05'
)]]
&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
<ValueQueries>
<Value name="AccountName">Event/EventData/Data[@Name='TargetUserName']</Value>
</ValueQueries>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>DOMAIN\ServiceAccount</UserId>
<LogonType>S4U</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-ExecutionPolicy Bypass -file "C:\Scripts\Supported Exam File Archiving.ps1" -username $(AccountName)</Arguments>
</Exec>
</Actions>
</Task>
[cmdletbinding(DefaultParameterSetName="All", SupportsShouldProcess=$true)]
param(
[Parameter(ValueFromPipeline=$true,ParameterSetName="SingleAccount")]
[string]$username,
[Parameter(ValueFromPipeline=$true,ParameterSetName="All")]
[switch]$all
)
$logFile = 'C:\scripts\Clear Specific Supported Exam User Files.log'
#Constants
$fileTypes = @("*.docx", "*.odt", "*.dotm", "*.pdf", "*.txt", "*.bak")
$directories = @(".",".\Desktop","\LibreOffice\4\user\backup")
$directoryRoot = "\\server\share"
$archiveDir = "$directoryRoot\Archive"
#Variables
[string[]]$accounts = if($psCmdlet.ParameterSetName -eq "All"){
"Collecting all files for archive" | Out-File $logFile
Get-ChildItem -Path $directoryRoot -Directory -Exclude "Archive" | foreach {$_.FullName}
}
elseif($psCmdlet.ParameterSetName -eq "SingleAccount"){
"Collecting $username's files for archive" | Out-File $logFile
"$username"
}
function Get-ExamDocuments($directories, $accountPath){
[cmdletbinding()]
#Loose Files
$files = foreach($dir in $directories){Get-ChildItem "$accountPath\$dir\*" -include $fileTypes}
#Directories containing auto-save files
$autosave = foreach($dir in $directories){Get-ChildItem $accountPath -Directory -Recurse | ? {(Get-ChildItem $_.FullName).Extension -match ".asd" } }
$files
$autosave
}
function Move-ExamDocuments{
[cmdletbinding(SupportsShouldProcess=$true)]
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[Alias('FullName')]
[String[]]$FilePath
)
process {
foreach($path in $FilePath)
{
#S:\server\share\Archive\{Year.Month}\{Day of Month}\{Original Filename(made unique if required)}
$date = Get-ItemProperty -Path $FilePath -Name LastWriteTime
$dateStr1 = $date.LastWriteTime.ToString("yyyy.MM")
$dateStr2 = $date.LastWriteTime.ToString("dd")
$newPathPart = "$archiveDir\$dateStr1"
if(!(Test-Path $newPathPart)){New-Item $newPathPart -ItemType Directory}
$newPath = "$newPathPart\$dateStr2"
if(!(Test-Path $newPath)){New-Item $newPath -ItemType Directory}
Move-Item $path -Destination $newPath
}
}
}
function ArchiveAllExamDocuments {
[cmdletbinding(SupportsShouldProcess=$true)]
param(
[string[]]$accounts
)
foreach($account in $accounts){
"Moving $account's files to archive" | Out-File $logFile -Append
Get-ExamDocuments -accountPath "$directoryRoot\$account" -directories $directories | Move-ExamDocuments -Verbose 4>&1| Out-File $logFile -Append
}
}
ArchiveAllExamDocuments $accounts -WhatIf:$PSBoundParameters.ContainsKey('WhatIf') -Confirm:$PSBoundParameters.ContainsKey('Confirm')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment