PowerShell PGP Decryption

 In this case, I am taking an PGP encrypted file, decrypting it, creating an archived version of the file, and then moving it to its new location

Gunna be honest, this code is SUPER temperamental.
Sometimes it doesn't want the credentials.
Some times it won't run it with out it.

Use at your own risk. It works, it just...is a diva as far as scripts go.

#Date Variables
$CurrentDate = Get-Date
$Date_Append = Get-Date -Format "yyyyMMdd"
$DatetoDelete = $CurrentDate.AddDays(-182)

##Declared locations, separated for simi reusability
$Key_Location = "YOUR-KEY-HERE"
$Source_Location = 'WHERE-IS-THE-FILE.CSV'
$Destination_Location = 'WHERE-IS-IT-GOING'
$CopySource_Location = $Destination_Location + "FILENAME"

$Archive_Location = "ARCIVE-GO-HERE\"
$Archive_FilePath = $Archive_Location+"MY-fILENAME" + $DateF_Append + ".csv"
$Archive_SourceFilePath = $Archive_Location+"MYFILE.CSV.PGP" + $Date_Append 

$userName = 'UserName'
$userPassword = 'Password'
$secStringPassword = ConvertTo-SecureString $userPassword -AsPlainText -Force
$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)

#Ensure PSPGP Installed
if (Get-Module -ListAvailable -Name PSPGP) {
    Write-Host "PSPGP Module is installed."
} 
else {
    Write-Host "Installing PSPGP module"
    Install-Module PSPGP -Force -Scope CurrentUser
}

#Decrypt File from $Source_Location and place into $Destination_Location
Unprotect-PGP -FilePathPrivate $Key_Location -Credential $credObject -FolderPath $Source_Location -OutputFolderPath $Destination_Location

#Create Backup
Copy-Item  $CopySource_Location  -Destination $Archive_FilePath -Force

#Remove Any backup older than 6 months
Get-ChildItem $Archive_Location -Recurse| Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

#Archive the source pgp file 
Move-Item  $Source_Location  -Destination $Archive_SourceFilePath -Force

Comments

Popular posts from this blog

JavaScript Broadcast Calendar Calculation

Powershell to Create Custom theme for Modern SharePoint Pages