forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-ScriptCredential.ps1
More file actions
56 lines (53 loc) · 1.75 KB
/
Copy pathAdd-ScriptCredential.ps1
File metadata and controls
56 lines (53 loc) · 1.75 KB
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
<#
.SYNOPSIS
Serializes a an encrypted credential to a PowerShell script using 32-byte random key file.
.INPUTS
System.Management.Automation.PSCredential containing a credential to serialize.
.LINK
ConvertTo-PowerShell.ps1
#>
#Requires -Version 7
[CmdletBinding()] Param(
# The script path to add the serialized credential to.
[Parameter(Position=0,Mandatory=$true)][string] $Path,
# The variable name to assign the credential to within the script.
[ValidatePattern('\A\w+\z',ErrorMessage='An alphanumeric identifier is required')]
[Parameter(Position=1,Mandatory=$true)][string] $Name,
# The credential to serialize.
[Parameter(Position=2,Mandatory=$true,ValueFromPipeline=$true)][PSCredential] $Credential,
# The key file to use, which will be generated and encrypted if it doesn't exist.
[string] $KeyFile = '~/.pskey'
)
Begin
{
$asbytes =
if((Get-Command Get-Content).Parameters.Encoding.ParameterType -eq [Text.Encoding]) {@{AsByteStream=$true}}
else {@{Encoding='Byte'}}
function Register-PSKey
{
[byte[]] $key = Get-RandomBytes.ps1 32
Set-Content $keyfile $key @asbytes
$f = Get-Item $keyfile
$f.Attributes = $f.Attributes -bor 'Hidden'
$f.Encrypt()
$f = $null
$key
}
[byte[]] $key =
if(!(Test-Path $keyfile -Type Leaf)) {Register-PSKey}
else {Get-Content $keyfile -Force @asbytes}
$keycmd = @"
`$asbytes =
if((Get-Command Get-Content).Parameters.Encoding.ParameterType -eq [Text.Encoding]) {@{AsByteStream=`$true}}
else {@{Encoding='Byte'}}
`$key = Get-Content '$keyfile' -Force @asbytes
"@
if(!(Select-String ([regex]::Escape($keyfile)) $Path))
{
$keycmd |Add-Content $Path
}
}
Process
{
"Set-Variable '$Name' ($($Credential |ConvertTo-PowerShell.ps1 -KeyBytes $key))" |Add-Content $Path
}