-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.psm1
58 lines (51 loc) · 1.49 KB
/
common.psm1
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
function ExecWithCode
{
[CmdletBinding(
SupportsShouldProcess=$False,
SupportsTransactions=$False,
ConfirmImpact="None",
DefaultParameterSetName="")]
param(
[Parameter(Mandatory = $true)][scriptblock]$cmd,
[Parameter(Mandatory = $false)][string]$errorMessage = "Error executing command: " + $cmd,
[Parameter(Mandatory = $false)][string]$maxExitCode = 0
)
& $cmd
if ($lastexitcode -lt 0)
{
throw $errorMessage
}
if($lastexitcode -gt $maxExitCode)
{
throw $errorMessage
}
}
function New-Secret
{
param(
[Parameter(Mandatory = $true)][string]$secretName,
[Parameter(Mandatory = $true)][string]$vaultName,
[bool]$environmentVariable = $false
)
if ((Get-AzureKeyVaultSecret -VaultName $vaultName -Name $secretName -ErrorAction SilentlyContinue) -ne $null)
{
return
}
if ($environmentVariable)
{
$envVariable = [Environment]::GetEnvironmentVariable($secretName, [EnvironmentVariableTarget]::Process)
if ([string]::IsNullOrWhiteSpace($envVariable))
{
throw "Cannot set environment variable '$secretName' because no environment variable provides a value"
}
$secretValue = ($envVariable | ConvertTo-SecureString -AsPlainText -Force)
}
else
{
$secretValue = (Read-Host -Prompt "Enter a value for '$secretName'" -AsSecureString)
}
Set-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName `
-SecretValue $secretValue
}