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 = @'
$Base64Contents = @'
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...