Last active
May 3, 2021 17:00
-
-
Save Szeraax/841bad43ae3c27f341d3c06162ec8696 to your computer and use it in GitHub Desktop.
MicroCache!
This file contains hidden or 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
function micro { | |
[CmdletBinding()] | |
param( | |
# The file you want to edit | |
[Parameter()] | |
[String] | |
$Path, | |
# The scriptblock to get proper folder on the server | |
[Parameter()] | |
[ScriptBlock] | |
$MicroCache = { $MicroCache = (Get-Item "$ENV:TEMP\micro.txt").FullName }, | |
# The local cache of where to store files while editing | |
[Parameter()] | |
[string] | |
$LocalStorage = "$ENV:TEMP", | |
# You should leave this at stop to be safe | |
[Parameter()] | |
[String] | |
$ErrorActionPreference = "Stop" | |
) | |
Invoke-Command -Session $Session -ScriptBlock $MicroCache | |
$RequestedFile = Invoke-Command -Session $Session { | |
if (Test-Path $MicroCache) { | |
$MicroFiles = @{ } | |
foreach ($PropertySet in (Get-Content $MicroCache | ConvertFrom-Json).PSObject.Properties) { | |
$MicroFiles[$PropertySet.Name] = $PropertySet.Value | |
} | |
Get-Item $MicroFiles[$Using:Path] | |
$MicroFiles.Remove($Using:Path) | |
$MicroFiles | ConvertTo-Json | Out-File $MicroCache | |
} | |
else { | |
return "No cache found! Exiting" | |
} | |
} | |
if ($RequestedFile) { | |
$param = @{ | |
FromSession = $Session | |
Path = $RequestedFile.FullName | |
Destination = "$LocalStorage\$($RequestedFile.Name)" | |
Force = $true | |
} | |
Copy-Item @param | |
$param = @{ | |
ToSession = $param['FromSession'] | |
Destination = $Param['Path'] | |
Path = $Param['Destination'] | |
Force = $true | |
} | |
$LastWriteTime = (Get-Item $param['Path']).LastWriteTime | |
micro.exe $param['Path'] | |
if ($LastWriteTime -ne (Get-Item $param['Path']).LastWriteTime) { | |
Invoke-Command $Session { Remove-Item $Using:param['Destination'] } | |
Copy-Item @param | |
} | |
Remove-Item -Path $param['Path'] | |
Enter-PSSession -Session $Session | |
} | |
else { | |
return "File not found in quick cache. Exiting" | |
} | |
} | |
$RemoteMicro = { | |
function micro { | |
[cmdletBinding()] | |
param( | |
$Path, | |
$MicroCache = "$ENV:TEMP\micro.txt", | |
$ErrorActionPreference = "Stop", | |
[switch]$CleanMicroCache | |
) | |
$FullName = Get-Item $Path -Force | Select-Object -Expand FullName | |
$MicroFiles = @{ } | |
if ((Test-Path $MicroCache) -and -not $CleanMicroCache) { | |
foreach ($PropertySet in (Get-Content $MicroCache | ConvertFrom-Json).PSObject.Properties) { | |
$MicroFiles[$PropertySet.Name] = $PropertySet.Value | |
} | |
} | |
$MicroFiles[$Path] = $FullName | |
$MicroFiles | ConvertTo-Json | Out-File $MicroCache | |
Exit-PSSession | |
} | |
} | |
function Enter-Session { | |
[CmdletBinding()] | |
param( | |
# Name of the computer to access | |
[Parameter(Mandatory)] | |
[String] | |
$ComputerName | |
) | |
$Script:Session = New-PSSession $ComputerName | |
Invoke-Command -Session $Session -ScriptBlock $RemoteMicro | |
Enter-PSSession $Session | |
} | |
# Possible Todos: | |
# Todo: Leave names in the MicroCache for a day. Only delete when a day old. | |
# Add LastAccessTime to each name? | |
# Todo: Better session handling. Multiple servers? Right now only assumes 1 session and | |
# that $Session is to ONLY 1 server. | |
# Todo: Use a proxy function instead of another function name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment