forked from tomtorggler/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpskeybase.ps1
93 lines (81 loc) · 3.11 KB
/
pskeybase.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function Read-KeybasePgpMessage {
<#
.SYNOPSIS
A wrapper for the keybase tool.
.DESCRIPTION
This function provides a wrapper for "keybase pgp decrypt" to make it easier to use.
.EXAMPLE
PS > Read-KeybasePgpMessage -File ./Downloads/mail.txt
This example tries to decrypt the mail.txt file using "keybase pgp decrypt -i mail.txt"
.EXAMPLE
PS > Read-KeybasePgpMessage .\Downloads | Invoke-Keybase
This example first gets all items in the ./Downloads folder and then tries to decrypt each one using "keybase pgp decrypt -i <filename>"
.INPUTS
[System.IO.FileInfo]
.OUTPUTS
[PSObject]
.NOTES
Autor: @torggler
Tested on Desktop and Core.
#>
[CmdletBinding()]
param (
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$Path,
[Parameter(ValueFromPipeline=$True)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$File,
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$Message,
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$OutFile
)
begin {
# Set default value for path parameter only if -Path is not used.
if(-not($PSBoundParameters.Path) -and $PSEdition -eq "Core") {
$Path = "/usr/bin/keybase"
$env:HOMEPATH = $env:HOME
Write-Verbose "Running on PS $PSEdition and using $Path"
} elseif(-not($PSBoundParameters.Path)) {
$Path = "C:\Users\Thomas\AppData\Local\Keybase\keybase.exe"
Write-Verbose "Running on PS $PSEdition and using $Path"
} else {
Write-Verbose "Running on PS $PSEdition and using $Path"
}
}
process {
Write-Verbose "Processing $File"
if ($Message) {
$KBParams = " pgp decrypt -m `"$Message`" "
} elseif ($File){
$KBParams = " pgp decrypt -i $($file.FullName)"
}
#$KBResult = Invoke-Expression -Command ("$Path $KBParams")
$tmpout = Join-Path $env:HOMEPATH -ChildPath keybaseout.txt
$tmperr = Join-Path $env:HOMEPATH -ChildPath keybaseerror.txt
Start-Process -FilePath $Path -ArgumentList $KBParams -NoNewWindow -RedirectStandardOutput $tmpout -RedirectStandardError $tmperr -Wait
$KBResult = Get-Content -Path $tmpout -Encoding UTF8
if((Get-Item $tmperr).Length -ne 0) {
$KBResult = Get-Content -Path $tmperr -Encoding UTF8
}
Remove-Item $tmpout,$tmperr -ErrorAction SilentlyContinue
$output = @{
CommandLine = "$($Path.Name) $KBParams";
InFile = $File.FullName;
Output = $KBResult;
}
if($OutFile) {
$KBResult | Set-Content -Path $OutFile -Force
} else {
Write-Output (New-Object -TypeName psobject -Property $output)
}
}
}