Skip to content

Instantly share code, notes, and snippets.

@Jackbennett
Created June 14, 2018 09:25
Show Gist options
  • Save Jackbennett/3ea99b7570b8ac2ad91b88e1f7ed6099 to your computer and use it in GitHub Desktop.
Save Jackbennett/3ea99b7570b8ac2ad91b88e1f7ed6099 to your computer and use it in GitHub Desktop.
function Clear-CCTV{
Param(
# Directory to clear
[parameter(mandatory=$true)]
[string]$Path
, # Archive Directory Path
[parameter(mandatory=$true)]
[string]$ArchivePath
)
Begin{
function Archive {
# Expand the current date to a folder path.
$DatePath = (get-date).toString('yyyy/MM/dd')
# Create the target date folder
$storePath = join-path $ArchivePath $DatePath
try{
new-item -ItemType Directory -Path $storePath -ErrorAction Stop
} catch [System.IO.IOException]{
# Directory already exists, which is fine so carry on.
} catch {
# All other errors, stop the script.
Throw $psitem
}
$moveList = Get-childItem $Path
move-item -Path $moveList.fullname -Destination $storePath
}
}
Process{
# Test there actually is anything to archive before creating a folder.
if((Get-childItem $Path).length){
Archive
}
}
}
# .date to zero out the hours from the calculated day.
$script:PolicyDeleteDate = (Get-date).addDays(-28).date
function Request-CCTVCompliance{
Param(
# Target folder containing files to check
$Directory
)
Begin{
$contents = Get-childItem -Recurse $Directory -File
}
Process {
$PendingDelete = $contents |
Select-Object BaseName, CreationTimeUtc, DirectoryName, FullName |
Where-Object CreationTimeUtc -le $script:PolicyDeleteDate
$CurrentFiles = $contents |
Select-Object BaseName, CreationTimeUtc, DirectoryName, FullName |
Where-Object CreationTimeUtc -gt $script:PolicyDeleteDate
$output = [pscustomobject]@{
"Delete" = $PendingDelete | sort-Object CreationTimeUtc
"Current" = $CurrentFiles | sort-Object CreationTimeUtc
}
Write-Output $output
}
}
@Jackbennett
Copy link
Author

line:39 This is now a bug, the compliance date is only created at script import, what if you don't re-import it all week?

It should have been a default parameter anyway, which would fix above, make it more flexible for other uses/users and avoided mudying scopes

...mental

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment