-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRead-GPG.ps1
65 lines (62 loc) · 2.09 KB
/
Read-GPG.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function Read-GPGMessage {
<#
.SYNOPSIS
Invokes 'gpg' command-line tool to decrypt messages.
.DESCRIPTION
This cmdlet is a wrapper for 'gpg --decrypt' to read encrypted messages.
.EXAMPLE
PS C:\> Read-GPGMessage -Message "----BEGIN PGP...."
The -Message parameter accepts a GPG encrypted string, GPG will prompt for a password and the output is written to the pipeline.
.EXAMPLE
PS C:\> Get-Content .\a.txt -Raw | Read-GPGMessage
The -Message parameter accepts a GPG encrypted string, GPG will prompt for a password and the output is written to the pipeline.
.EXAMPLE
PS C:\> Read-GPGMessage -File .\a.txt -Output .\b.txt
The file parameter specifies an input file that contains encrypted data, the output parameter specifies the destination file for the decrypted data.
.INPUTS
[string]
As provided by Get-Content -Raw
.OUTPUTS
[string]
.NOTES
Author: @torggler
#>
[CmdletBinding(
SupportsShouldProcess = $true,
DefaultParameterSetName = "Message"
)]
param (
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ParameterSetName = "Message")]
[string]
$Message,
[Parameter(
Mandatory = $true,
ParameterSetName = "File")]
[System.IO.FileInfo]
$File,
[Parameter(
Mandatory = $true,
ParameterSetName = "File")]
[System.IO.FileInfo]
$Output
)
process {
switch ($PSBoundParameters.Keys) {
"Message" {
$command = " `"$message`" | gpg --decrypt"
}
"File" {
$command = "gpg --decrypt $File"
}
"Output" {
$command = "gpg --output $Output --decrypt $File"
}
}
if($PSCmdlet.ShouldProcess($command, "Executing:")) {
Invoke-Expression -Command $command
}
}
}