-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzureCredential.ps1
92 lines (86 loc) · 3.03 KB
/
Get-AzureCredential.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
function Get-AzureCredential
{
<#
.Synopsis
Gets a credential object from Azure Keyvault
.DESCRIPTION
Gets a credential object to Azure Vault based on the guidelines in Test-VaultAccess
.EXAMPLE
Get-AzureCredential -UserName AdminTest
UserName Password
-------- --------
AdminTest System.Security.SecureString
.EXAMPLE
Get-AzureCredential -UserName AdminTest -ClearTextPassword
WowThatWasStupid
.EXAMPLE
Get-AzureCredential -UserName AdminTest -ClearTextPassword | clip
(Now the password is in the clipboard - ready for pasting)
#>
[CmdletBinding(DefaultParameterSetName = 'Specific')]
[Alias()]
Param
(
# Param1 help description
[Parameter(Mandatory=$true, Position=2,HelpMessage="Supply the resource group name to use")]
$ResourceGroupName,
[Parameter(Mandatory=$true, Position=3,HelpMessage="Supply the storage account name to use")]
$StorageAccountName,
[Parameter(Mandatory=$true, Position=4,HelpMessage="Supply the vault name")]
$VaultName,
[Parameter(ParameterSetName = 'Specific',Mandatory=$true, Position=0)]
$UserName,
[Parameter(ParameterSetName = 'Specific',Mandatory=$False, Position=1)]
[switch]$ClearTextPassword,
[Parameter(ParameterSetName = 'AllCredentials',Mandatory=$true, Position=0)]
[Switch]$All,
[Parameter(ParameterSetName = 'AllUserNames',Mandatory=$true, Position=0)]
[Switch]$ListUserNames,
$TableName,
$PartitionKey
)
Begin
{
if (!$Global:VaultSA)
{
Connect-AzureCredentialVault -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -VaultName $VaultName
}
$table = Get-AzureStorageTable -Name $TableName -Context $Global:VaultSA.Context
}
Process
{
$UserEntries = Get-AzureStorageTableRowAll -table $Table
if ($UserName)
{
$Users = $UserEntries | Where-Object {$_.UserName -eq $UserName}
if ($UserName -and $Users){Write-Verbose "Table entry found for $UserName."}
else {Write-Warning "No table entry found for $UserName"}
}
if ($All)
{
$Users = $UserEntries
}
if ($ListUserNames){
$UserEntries.UserName
continue
}
Foreach ($User in $Users)
{
$Secret = Get-AzureKeyVaultSecret -VaultName $VaultName -Name $User.RowKey
if ($Secret)
{
if ($ClearTextPassword)
{
$Secret.SecretValueText
}
else
{
New-Object -typename System.Management.Automation.PSCredential -argumentlist $User.UserName,$Secret.SecretValue
}
}
Else {Write-Warning "No Secret found for $($User.UserName)"}
}
}
end {
}
}