Last active
May 4, 2023 09:59
-
-
Save ReinforceZwei/d1ed4fc835255b3ddf4ae34fd9f54843 to your computer and use it in GitHub Desktop.
Create temporary drive that will clean up itself on every start up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -AssemblyName System.Windows.Forms | |
$workingDirectory = 'D:\v_ram' | |
$daysBeforeDelete = 3 | |
$driveLetter = 'S:' | |
$workFolder = 'current' | |
$backupFolder = 'backup' | |
$dateFormat = 'yyyy_MM_dd' | |
function Is-Empty { | |
param ( | |
[string] $Path | |
) | |
return (Get-ChildItem $Path | Measure-Object).count -eq 0 | |
} | |
function Show-Notify { | |
param ( | |
[string] $Title, | |
[string] $Message, | |
[int] $Timeout = 10000 | |
) | |
$path = (Get-Process -id $pid).Path | |
$notify = New-Object System.Windows.Forms.NotifyIcon | |
$notify.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path) | |
#$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning | |
$notify.BalloonTipTitle = $Title | |
$notify.BalloonTipText = $Message | |
$notify.Visible = $true | |
$e_h = { | |
param([object]$sender, [System.EventArgs]$e) | |
$notify.Dispose() | |
} | |
$notify.add_BalloonTipClosed($e_h) | |
$notify.ShowBalloonTip($Timeout) | |
} | |
# Make sure working directory exist | |
mkdir $workingDirectory -force | |
mkdir (Join-Path $workingDirectory $workFolder) -force | |
mkdir (Join-Path $workingDirectory $backupFolder) -force | |
# Move everything in work folder to backup folder | |
if (-not (Is-Empty (Join-Path $workingDirectory $workFolder))){ | |
$todayFolder = [IO.Path]::Combine($workingDirectory, $backupFolder, (get-date -f $dateFormat)) | |
mkdir $todayFolder -force | |
Move-Item -Path ([IO.Path]::Combine($workingDirectory, $workFolder, '*')) -Destination $todayFolder | |
} | |
$deleteCount = 0 | |
# Delete old backup | |
Get-ChildItem (Join-Path $workingDirectory $backupFolder) | foreach-object { | |
# Get folder name and check is older enough to be deleted | |
if ((new-timespan ([DateTime]::ParseExact($_.Name, 'yyyy_MM_dd', $null)) (get-date)).Days -gt $daysBeforeDelete) { | |
rm -R -Force ([IO.Path]::Combine($workingDirectory, $backupFolder, $_.Name)) | |
$deleteCount++ | |
} | |
} | |
if ($deleteCount -gt 0) { | |
Show-Notify "VRamdisk" "Permanently deleted $deleteCount backups" | |
} | |
# Mount folder as dirve | |
subst $driveLetter (Join-Path $workingDirectory $workFolder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$workingDirectory = 'D:\v_ram' | |
$taskName = 'VRAMDISK' | |
$taskPath = '\' | |
$scriptPath = (Join-Path $workingDirectory 'v_ramdisk.ps1') | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Write-Host "Running with administrator privileges." | |
$taskExists = Get-ScheduledTask -TaskName $taskName -TaskPath $taskPath -ErrorAction SilentlyContinue | |
if ($taskExists) { | |
Unregister-ScheduledTask -TaskName $taskName -TaskPath $taskPath -Confirm:$false | |
Write-Host "Task $taskName deleted." | |
} | |
$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-File `"$scriptPath`"" | |
$Trigger = New-ScheduledTaskTrigger -AtLogon | |
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -Hidden | |
Register-ScheduledTask -TaskName $taskName -Action $Action -Trigger $Trigger -Settings $Settings -User "SYSTEM" -TaskPath $taskPath | |
Write-Host "Task $taskName created successfully." | |
} else { | |
Write-Host "Not running with administrator privileges." | |
Write-Host "Restart the script with administrator privileges." | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$taskName = 'VRAMDISK' | |
$taskPath = '\' | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Write-Host "Running with administrator privileges." | |
$taskExists = Get-ScheduledTask -TaskName $taskName -TaskPath $taskPath -ErrorAction SilentlyContinue | |
if ($taskExists) { | |
Unregister-ScheduledTask -TaskName $taskName -TaskPath $taskPath -Confirm:$false | |
Write-Host "Task $taskName deleted." | |
} | |
} else { | |
Write-Host "Not running with administrator privileges." | |
Write-Host "Restart the script with administrator privileges." | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment