Jump to content

Capture File into PS1 Script 1.0.0

   (0 reviews)

1 Screenshot

About This File

This script takes a filename to capture as a parameter. The output is a PowerShell script capable of restoring the original file. The output will be no more than 70 characters wide and is Base64 encoded. The original filename and path is also Base64 encoded Unicode, so any valid file (even with international or Emoji characters) can be captured. The file timestamp is also captured and restored. This is meant as an alternative way to capture binary content that can be transmitted as plain text and used to easily re-create the file just by running a script. The capture and restore has been tested with PoSH2/Server2003 and Windows 10.

Example to Capture: powershell.exe -file captureportablefile.ps1 "c:\filetoretrieve.dat" > restorefile.ps1

The contents can also be placed in the "Execute Script" function with a filename specified as a parameter. (Include quotes around the path if it has spaces).

The output script will save the file with the original name\path, or it will accepts a filename parameter for the new file to save to. The output can be used directly in "Execute Script" to re-create the file, on a different agent for example. The script will indicate success or failure restoring the file.

Example to restore: powershell.exe -file restorefile.ps1
Example to restore to a different path: powershell.exe -file restorefile.ps1 "c:\newfiletosave.dat"

Here is en example of the script output. Running the script below will create a file named "C:\Windows\Temp\Have a day.zip"

$Base64FileName = @'
QwA6AFwAVwBpAG4AZABvAHcAcwBcAFQAZQBtAHAAXABIAGEAdgBlACAAYQAgADomIABkAGEAeQA
uAHoAaQBwAA==
'@
$TimeStamp=[DateTime]636604097389163155
$Base64Contents = @'
UEsDBAoAAAAAAP0Am0zn48F+HAAAABwAAAAZAAAASGF2ZSBhIGhhcHB5IGRheS1BTlNJLnR4dEp
1c3Qgc29tZSA/pD8/Pz8/Pz8gc3R1ZmYuDQpQSwMECgAAAAAA6QCbTORcBzs6AAAAOgAAABwAAA
BIYXZlIGEgaGFwcHkgZGF5LVVuaWNvZGUudHh0//5KAHUAcwB0ACAAcwBvAG0AZQAgADomPCZrJ
jsmQiZqJmAmYyZAJiAAcwB0AHUAZgBmAC4ADQAKAFBLAwQKAAAAAADxAJtMeDSP5jEAAAAxAAAA
GQAAAEhhdmUgYSBoYXBweSBkYXktVVRGOC50eHTvu79KdXN0IHNvbWUg4pi64pi84pmr4pi74pm
C4pmq4pmg4pmj4pmAIHN0dWZmLg0KUEsBAj8ACgAAAAAA/QCbTOfjwX4cAAAAHAAAABkAJAAAAA
AAAAAgAAAAAAAAAEhhdmUgYSBoYXBweSBkYXktQU5TSS50eHQKACAAAAAAAAEAGACFbKN39t3TA
UCsDnT23dMB8s/tc/bd0wFQSwECPwAKAAAAAADpAJtM5FwHOzoAAAA6AAAAHAAkAAAAAAAAACAA
AABTAAAASGF2ZSBhIGhhcHB5IGRheS1Vbmljb2RlLnR4dAoAIAAAAAAAAQAYANODVGD23dMB0T1
GLPbd0wHRPUYs9t3TAVBLAQI/AAoAAAAAAPEAm0x4NI/mMQAAADEAAAAZACQAAAAAAAAAIAAAAM
cAAABIYXZlIGEgaGFwcHkgZGF5LVVURjgudHh0CgAgAAAAAAABABgAYx1uafbd0wFjHW5p9t3TA
YN/S2n23dMBUEsFBgAAAAADAAMARAEAAC8BAAAAAA==
'@
$FileName=[System.Convert]::FromBase64String($Base64FileName)
$FileName=[System.Text.Encoding]::Unicode.GetString($FileName)
If(($Args) -ne $Null) {
If($Args[0] -match '^[^\*\?]+$') {$FileName=$Args[0]} 
Else {Write-Output 'Invalid Filename.';break}
}
Try {
$FileContents = [System.Convert]::FromBase64String($Base64Contents)
$Null=Mkdir $(Split-Path -Path $FileName) -EA 0
Set-Content -Literal $FileName -Value $FileContents -Encoding Byte
(Get-Item $($FileName)).LastWriteTimeUtc=$TimeStamp
Write-Output "File restore complete: $($FileName)"
}
Catch {Write-Output "File restore failed: $($FileName)"}

 


User Feedback

You may only provide a review once you have downloaded the file.

There are no reviews to display.

×
×
  • Create New...