AzureKeyVaultPasswordRepo.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
Function New-Passowrd
{
  [OutputType([System.Security.SecureString])]
  [CmdletBinding()]
  PARAM (
    [Parameter(Mandatory = $true)][int]$Length,
    [Parameter(Mandatory = $true)][int]$NumberOfSpecialCharacters
  )
  Add-Type -AssemblyName System.Web
  $PwString = [Web.Security.Membership]::GeneratePassword($Length,$NumberOfSpecialCharacters)
  $secString = New-Object System.Security.SecureString
  For ($i = 0; $i -lt $PwString.length; $i++)
  {
    $char = $PwString.Substring($i, 1)
    $secString.AppendChar($char)
  }
  $secString
}
Function Get-ExistingCred
{
  [OutputType([System.Collections.ArrayList])]
  [CmdletBinding()]
  PARAM (
    [Parameter(Mandatory = $false)][string]$SearchString = $null
  )
  #Get existing secrets from key vault
  If ($PSBoundParameters.ContainsKey('SearchString'))
  {
    $ExistingSecrets = Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName|
    Where-Object -FilterScript {
      $_.Tags.ContainsKey('CredName')
    } |
    Where-Object -FilterScript {
      $_.Tags['CredName'] -imatch $SearchString
    }
  }
  else 
  {
    $ExistingSecrets = Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName
  }
  
  #Get credential names
  $ExistingCredNames = $ExistingSecrets |
  ForEach-Object -Process {
    $_.tags['CredName']
  } |
  Get-Unique
  #Get credential user names
  $arrExistingCreds = New-Object -TypeName System.Collections.ArrayList
  FOreach ($item in $ExistingCredNames)
  {
    $UserNameSecret = $ExistingSecrets | Where-Object -FilterScript {
      $_.Tags['CredName'] -eq $item -and $_.Tags['Type'] -eq 'UserName'
    }
    $PasswordSecret = $ExistingSecrets | Where-Object -FilterScript {
      $_.Tags['CredName'] -eq $item -and $_.Tags['Type'] -eq 'Password'
    }
    $UserName = (Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $UserNameSecret.Name).SecretValueText
    $objExistingCred = New-Object -TypeName psobject -Property @{
      CredName           = $item
      UserName           = $UserName
      UserNameSecretName = $UserNameSecret.Name
      PasswordSecretName = $PasswordSecret.Name
    }
    [void]$arrExistingCreds.Add($objExistingCred)
  }
  ,$arrExistingCreds
}
Function New-KeyVaultCred
{
  $NewCredName = $null
  $NewCredUserName = $null
  Do
  {
    $NewCredName = Read-Host -Prompt 'Give your new credential a name (only contain alphanumeric characters and dash)'
  }
  while ($NewCredName.Length -eq 0 -or $NewCredName -notmatch '^[0-9a-zA-Z-]+$')
  Do
  {
    $NewCredUserName = Read-Host -Prompt 'Enter user name'
  }
  while ($NewCredUserName.Length -eq 0)
  $NewCredPassword = Read-Host -Prompt "Enter new password or hit enter to generate a random password with $Global:RandomPasswordLength character in length with $Global:RandomPasswordSpecialCharactersCount special characters" -AsSecureString
  $UserNameSecretName = "$NewCredName`-UserName"
  $PasswordSecretName = "$NewCredName`-Password"
  
  #preparing for the tags
  $UTags = @{
    CredName = "$NewCredName"
    Type     = 'UserName'
  }
  $PTags = @{
    CredName = "$NewCredName"
    Type     = 'Password'
  }
        
  #Create secrets
  Write-Output -InputObject 'Creating Credential in key vault. please wait...'
  #user name
  $NewCredUserNameSecString = New-Object System.Security.SecureString
  For ($i = 0; $i -lt $NewCredUserName.length; $i++)
  {
    $char = $NewCredUserName.Substring($i, 1)
    $NewCredUserNameSecString.AppendChar($char)
  }
  $NewUserSecret = Set-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $UserNameSecretName -SecretValue $NewCredUserNameSecString -Tag $UTags -ErrorVariable errCreateNewUserName
  #password secret
  $bNewCopyPassworToClipboard = $false
  If ($NewCredPassword.length -eq 0)
  {
    Write-Output -InputObject 'Password not specified. Generating a new password...'
    $NewCredPassword = New-Passowrd -Length $Global:RandomPasswordLength -NumberOfSpecialCharacters $Global:RandomPasswordSpecialCharactersCount
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewCredPassword)
    $NewCredPasswordClearText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
    $bNewCopyPassworToClipboard = $true
  }
  $NewPasswordSecret = Set-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $PasswordSecretName -SecretValue $NewCredPassword -Tag $PTags -ErrorVariable errCreateNewPassword
  If ($errCreateNewUserName.Count -eq 0 -and $errCreateNewPassword.Count -eq 0)
  {
    Write-Host -Object 'Credential successfully created.' -ForegroundColor Green
    if ($bNewCopyPassworToClipboard -eq $true)
    {
      $NewCredPasswordClearText | clip.exe
      Write-Host -Object 'Password copied to clipboard.' -ForegroundColor Green
      Write-output ""
    }
  }
  Write-Output -InputObject ''
}

Function Get-KeyVaultCred
{
  [CmdletBinding()]
  PARAM (
    [Parameter(Mandatory = $false)][switch]$Search
  )
  If ($Search)
  {
    Write-Host -Object 'Search credential' -ForegroundColor Yellow
    Do
    {
      $SearchString = Read-Host -Prompt 'Enter search string (only contain alphanumeric characters and dash)'
    }
    while ($SearchString.Length -eq 0 -or $SearchString -notmatch '^[0-9a-zA-Z-]+$')
    Write-Output -InputObject ''
    $ExistingCreds = Get-ExistingCred -SearchString $SearchString
  }
  else 
  {
    $ExistingCreds = Get-ExistingCred
  }
  
  If ($ExistingCreds.count -gt 0)
  {
    Write-Output -InputObject "Number of existing credential(s) found in Azure Key Vault: $($ExistingCreds.count)"
    Write-Host -Object 'Select credential:' -ForegroundColor DarkGray
    $i = 0
    Write-Host -Object " $i. Go Back" -ForegroundColor DarkGray
    Foreach ($item in $ExistingCreds)
    {
      $i++
      Write-Host -Object " $i. $($item.CredName) `($($item.UserName)`)" -ForegroundColor Yellow
    }
    Do 
    {
      [int]$ans = Read-Host -Prompt "Enter selection (0 - $i)"
      if ($ans -gt 0 -and $ans -le $i)
      {
        Write-Output -InputObject ''
        #retrieve the cred
        $SelectedCred = $ExistingCreds[$ans-1]
        
        Do 
        {
          Write-Host -Object "Selected credential name: $($SelectedCred.CredName)" -ForegroundColor DarkGray
          Write-Host -Object ' 0. Go Back' -ForegroundColor DarkGray
          Write-Host -Object ' 1. Copy User Name to clipboard' -ForegroundColor Yellow
          Write-Host -Object ' 2. Copy Password to clipboard' -ForegroundColor Yellow
          Write-Host -Object ' 3. Update Credential' -ForegroundColor Yellow
          Write-Host -Object ' 4. Delete Credential' -ForegroundColor Yellow
          Do 
          {
            [int]$GetCredOption = Read-Host -Prompt 'Enter selection (0 - 4)'
          }
          Until ($GetCredOption -ge 0 -and $GetCredOption -le 4)
          Write-Output -InputObject ''
          If ($GetCredOption -eq 1) #Copy username to clipboard
          {
            $ExistingCredUserName = (Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.UserNameSecretName).SecretValueText
            $ExistingCredUserName | clip.exe
            Write-Host -Object 'User Name copied to clipboard.' -ForegroundColor Green
            Write-output ""
            Write-Output -InputObject ''
          }
          elseif ($GetCredOption -eq 2) #Copy password to clipboard
          {
            $ExistingCredPassword = (Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.PasswordSecretName).SecretValueText
            $ExistingCredPassword | clip.exe
            Write-Host -Object 'Password copied to clipboard.' -ForegroundColor Green
            Write-output ""
          }
          elseif ($GetCredOption -eq 3) #Update
          {
            $UpdatedUserName = Read-Host -Prompt 'Enter new user name or hit enter to keep the existing user name'
            $UpdatedPassword = Read-Host -Prompt "Enter new password or hit enter to generate a random password with $Global:RandomPasswordLength character in length with $Global:RandomPasswordSpecialCharactersCount special characters" -AsSecureString
            $bEditCopyPassworToClipboard = $false
            If ($UpdatedPassword.length -eq 0)
            {
              Write-Output -InputObject 'Password not specified. Generating a new password...'
              $UpdatedPassword = New-Passowrd -Length $Global:RandomPasswordLength -NumberOfSpecialCharacters $Global:RandomPasswordSpecialCharactersCount
              $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($UpdatedPassword)
              $UpdatedPasswordClearText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
              $bEditCopyPassworToClipboard = $true
            }

            Write-Output -InputObject 'Updating Credential in key vault. please wait...'
            If ($UpdatedUserName.Length -gt 0)
            {
              #Upate the user name secret
               $UpdatedUserNameSecString = New-Object System.Security.SecureString
              For ($i = 0; $i -lt $UpdatedUserName.length; $i++)
              {
                $char = $UpdatedUserName.Substring($i, 1)
                $UpdatedUserNameSecString.AppendChar($char)
              }
              $ExistingUserNameSecret = Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.UserNameSecretName
              $UpdateUserNameSecret = Set-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $ExistingUserNameSecret.Name -SecretValue $UpdatedUserNameSecString -Tag $($ExistingUserNameSecret.Attributes.Tags) -ErrorVariable errUpdateUserName
            }
            #Update the password secret
            $ExistingPasswordSecret = Get-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.PasswordSecretName
            $UpdatePasswordSecret = Set-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $ExistingPasswordSecret.Name -SecretValue $UpdatedPassword -Tag $($ExistingPasswordSecret.Attributes.Tags) -ErrorVariable errUpdatePassword
            If ($errUpdateUserName.Count -eq 0 -and $errUpdatePassword.Count -eq 0)
            {
              Write-Host -Object 'Credential successfully updated.' -ForegroundColor Green
              if ($bEditCopyPassworToClipboard -eq $true)
              {
                $UpdatedPasswordClearText | clip.exe
                Write-Host -Object 'Password copied to clipboard.' -ForegroundColor Green
                Write-output ""
              }
            }
            Write-Output -InputObject ''
          }
          elseif ($GetCredOption -eq 4) #delete
          {
            #Confirm
            $DeleteConfirmationTitle = 'Confirming Credential Deletion'
            $DeleteConfirmationMessage = "Are you sure you want to delete credential '$($SelectedCred.CredName)'?"
            $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Delete.'
            $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', 'Keep.'
            $DeleteConfirmOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
            $DeleteConfirmed = $host.ui.PromptForChoice($DeleteConfirmationTitle, $DeleteConfirmationMessage, $DeleteConfirmOptions, 0) 
            If ($DeleteConfirmed -eq 0)
            {
              #Yes selected
              #Remove the user name secret
              $DeleteUserNameSecret = Remove-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.UserNameSecretName -Force -Confirm:$false  -ErrorVariable errDeleteUserName
              #Remove the password secret
              $DeletePasswordSecret = Remove-AzureKeyVaultSecret -VaultName $Global:KeyVaultName -Name $SelectedCred.PasswordSecretName -Force -Confirm:$false  -ErrorVariable errDeletePassword
              If ($errDeleteUserName.Count -eq 0 -and $errDeletePassword.Count -eq 0)
              {
                Write-Host -Object 'Credential successfully deleted.' -ForegroundColor Green
                $ExistingCreds.Remove($SelectedCred)
                #Go back to the parent menu
                $GetCredOption = 0
              }
              Write-Output -InputObject ''
            }
          }
        }
        Until ($GetCredOption -eq 0)
      }
    }
    while ($ans -lt 0 -or $ans -gt $i)
    Write-Output -InputObject ''
  }
  else 
  {
    Write-Host -Object 'No credentials are found in the Azure Key Vault.' -ForegroundColor Red
  }
  Write-Output -InputObject ''
}

Function Save-KeyVaultRepoProfile
{
  #confirm
  Write-Output -InputObject 'By Saving the selected Azure subscription and Key Vault in registry, you will no need to select them again.'
  $SaveProfileConfirmationTitle = 'Save Profile'
  $SaveProfileConfirmationMessage = 'Are you sure you want to save selected Azure subscription Id and key vault name in your profile?'
  $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Save.'
  $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', "Don't Save."
  $SaveProfileConfirmOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
  $SaveProfileConfirmed = $host.ui.PromptForChoice($SaveProfileConfirmationTitle, $SaveProfileConfirmationMessage, $SaveProfileConfirmOptions, 0) 
  If ($SaveProfileConfirmed -eq 0)
  {
    #Yes selected
    $context = Get-AzureRmContext 
    #Create a new regkey
    $NewRegKey = New-Item -Path $Global:SettingRegPath -Name $context.account.id -Force
    $ProfilePath = Join-Path -Path $Global:SettingRegPath -ChildPath $context.account.id
    #Save Azure Subscription Id
    $AzureSubIdRegValue = New-ItemProperty -Path $ProfilePath -Name AzureSubscriptionId -Value $context.Subscription.SubscriptionId -PropertyType 'String' -Force
    $AzureKeyVaultNameRegValue = New-ItemProperty -Path $ProfilePath -Name AzureKeyVaultName -Value $Global:KeyVaultName -PropertyType 'String' -Force
    Write-Host -Object 'Profile saved.' -ForegroundColor Green
    Write-Output -InputObject ''
  }
}
Function Get-KeyVaultRepoProfile
{
  $context = Get-AzureRmContext
  $RegKeyPath = Join-Path -Path $Global:SettingRegPath -ChildPath $context.Account.Id
  If (Test-Path $RegKeyPath)
  {
    Try 
    {
      $regvalues = Get-ItemProperty -Path $RegKeyPath -ErrorAction SilentlyContinue
      $AzureSubId = $regvalues.AzureSubscriptionId
      $KeyVaultName = $regvalues.AzureKeyVaultName
    }
    catch 
    {
      $AzureSubId = $null
      $KeyVaultName = $null
    }
  }

  If ($AzureSubId.Length -gt 0 -and $KeyVaultName -gt 0)
  {
    $objProperty = @{
      'AzureSubId' = $AzureSubId
      'KeyVaultName' = $KeyVaultName
    }
    $objProfile = New-Object -TypeName psobject -Property $objProperty
    $objProfile
  }
  else 
  {
    $null
  }
}

Function Remove-KeyVaultRepoProfile
{
  $context = Get-AzureRmContext
  $RegKeyPath = Join-Path -Path $Global:SettingRegPath -ChildPath $context.Account.Id
  If (Test-Path $RegKeyPath)
  {
    #delete the registry
    #confirm
    Write-Output -InputObject 'By Deleting the Key Vault Password Repository Profile from registry, you will need to manually select Azure Subscription and key vault when you use it next time.'
    $DeleteProfileConfirmationTitle = 'Delete Profile'
    $DeleteProfileConfirmationMessage = 'Are you sure you want to delete your profile?'
    $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Delete.'
    $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', "Don't Delete."
    $DeleteProfileConfirmOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $DeleteProfileConfirmed = $host.ui.PromptForChoice($DeleteProfileConfirmationTitle, $DeleteProfileConfirmationMessage, $DeleteProfileConfirmOptions, 0) 
    If ($DeleteProfileConfirmed -eq 0)
    {
      #Yes selected
      $DeleteKey = Remove-Item -Path $RegKeyPath -Recurse -Force -ErrorVariable errDeleteProfile
      If ($errDeleteProfile.Count -eq 0)
      {
        Write-Host -Object 'Profile Deleted.' -ForegroundColor Green
        Write-Output -InputObject ''
      }
      else 
      {
        Write-Error -Message 'Failed to delete the profile.'
      }
    }
  }
  else 
  {
    Write-Host -Object "No profile exists for the current user Id '$($context.Account.Id)'." -ForegroundColor Red
  }
}

Function Add-KeyVaultFullAccessPolicy
{
  
  Do
  {
    $AADSearchString = Read-Host -Prompt 'Enter a search string for the Azure AD user to search Azure Active Directory (i.e. name)'
    $AADUsers = Get-AzureRmADUser -SearchString $AADSearchString
    If ($AADUsers.count -eq 0)
    {
      Write-Host -Object 'No Azure AD users found that matches the search string. Please enter another search string' -ForegroundColor Red
    }
  }
  until ($AADUsers.count -gt 0)
  for ($i = 1;$i -le $AADUsers.count; $i++) 
  {
    Write-Host -Object "$i. $($AADUsers[$i-1].DisplayName) `| UPN: $($AADUsers[$i-1].UserPrincipalName) `| Object Id: $($AADUsers[$i-1].Id.ToString())" -ForegroundColor Yellow
  }
  Write-Host -Object 'Select the User Accountccount' -ForegroundColor Yellow
  [int]$ans = Read-Host -Prompt 'Enter selection'
  $VaultAdmin = $AADUsers[$ans-1]
  $VaultAdminObjectId = $VaultAdmin.Id.ToString()
  Write-Output -InputObject "Configuring Key Vault Access Policy for '$($VaultAdmin.UserPrincipalName)'."
  Set-AzureRmKeyVaultAccessPolicy -VaultName $Global:KeyVaultName -ObjectId $VaultAdminObjectId -PermissionsToKeys all -PermissionsToSecrets all -ErrorAction errAddAccess
  If ($errAddAccess.Count -eq 0)
  {
    Write-Host -Object 'Key Vault access granted.' -ForegroundColor Green
    Write-Output -InputObject ''
  }
  else 
  {
    Write-Error -Message 'Failed to grant access to the Key Vault.'
    Write-Output -InputObject ''
  }
}

Function Remove-KeyVaultFullAccessPolicy
{
  #Get keyvault
  $KeyVault = Get-AzureRmKeyVault -VaultName $Global:KeyVaultName
  #Get Access policies
  $AccessPolicies = $KeyVault.AccessPolicies
  Write-Host -Object 'Select Account to Remove:' -ForegroundColor DarkGray
  $i = 0
  Write-Host -Object " $i. Go Back" -ForegroundColor DarkGray
  Foreach ($item in $AccessPolicies)
  {
    $i++
    Write-Host -Object " $i. $($item.DisplayName) `(Object Id: $($item.ObjectId)`)" -ForegroundColor Yellow
  }
   Do 
    {
      [int]$ans = Read-Host -Prompt "Enter selection (0 - $i)"
      if ($ans -gt 0 -and $ans -le $i)
      {
        $AccessPolicyToRemove = $AccessPolicies[$i-1]
        #Confirm
        Write-Output -InputObject 'By removing the access to Key Vault, the selected user will no longer have access to the credentials saved in this key vault.'
        $DeleteAccessConfirmationTitle = 'Remove Access'
        $DeleteAccessConfirmationMessage = "Are you sure you want to remove Key Vault access for '$($item.DisplayName)'?"
        $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Remove.'
        $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', "Don't Remove."
        $DeleteAccessConfirmOptions = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
        $DeleteAccessConfirmed = $host.ui.PromptForChoice($DeleteAccessConfirmationTitle, $DeleteAccessConfirmationMessage, $DeleteAccessConfirmOptions, 0) 
        If ($DeleteAccessConfirmed -eq 0)
        {
          #Yes selected
          Write-Output "Removing Key Vault access for '$($AccessPolicyToRemove.DisplayName)'"
          $RemoveJob = Remove-AzureRmKeyVaultAccessPolicy -VaultName $Global:KeyVaultName -ObjectId $AccessPolicyToRemove.ObjectId -ErrorVariable errRemoveAccess
          If ($errRemoveAccess.Count -eq 0)
          {
            Write-Host -Object 'Key Vault access removed.' -ForegroundColor Green
            Write-Output -InputObject ''
          }
          else 
          {
            Write-Error -Message 'Failed to remove access to the Key Vault.'
            Write-Output -InputObject ''
          }
        } else {
          Write-Output "Key Vault access removal cancelled for '$($AccessPolicyToRemove.DisplayName)'."
          Write-Output -InputObject ''
        }
      }
    }
    while ($ans -lt 0 -or $ans -gt $i)
}

# .EXTERNALHELP AzureKeyVaultPasswordRepo.psm1-Help.xml
Function Invoke-AzureKeyVaultPasswordRepository
{
  Clear-Host
  #region variables
  $IdentifyingTagName = 'Purpose'
  $IdentifyingTagValue = 'PersonalPasswordRepo'
  $Global:RandomPasswordLength = 20
  $Global:RandomPasswordSpecialCharactersCount = 3
  $TopMenuTitleLine1 = 'Azure Key Vault Personal Password Repository'
  $TopMenuTitleLine2 = '============================================'

  $Global:SettingRegPath = 'HKCU:\Software\TYConsulting\AzureKeyVaultPasswordRepo\Profiles'
  #endregion

  #region retrieve Azure resources
  #Logging in to Azure
  Write-Output -InputObject 'Checking for existing Azure login session.'
  Do
  {
    Try
    {
      $context = Get-AzureRmContext
      $CurrentAccount = $context.Account.Id
      If ($CurrentAccount -ne $null)
      {
        $AzureContextTitle = 'Existing Azure Credential'
        $ExistingAccountMessage = "You are currently logged in to Azure using account $CurrentAccount. Do you want to keep using this account?"
        $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Keep using this Id.'
        $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', 'Login to Azure using another Id.'
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $UserSelected = $host.ui.PromptForChoice($AzureContextTitle, $ExistingAccountMessage, $options, 0) 
        If ($UserSelected -eq 1)
        {
          Write-Output -InputObject 'Login to Auzre'
          $null = Add-AzureRmAccount
          $context = Get-AzureRmContext
          $CurrentAccount = $context.Account.Id
        }
      }
      else 
      {
        Write-Host -Object 'You are currently not logged in to Azure. Please login.' -ForegroundColor Red
        $null = Add-AzureRmAccount
        $context = Get-AzureRmContext
        $CurrentAccount = $context.Account.Id
      }
    }
    Catch 
    {
      Write-Host -Object 'You are currently not logged in to Azure. Please login.' -ForegroundColor Red
      $null = Add-AzureRmAccount
      $context = Get-AzureRmContext
      $CurrentAccount = $context.Account.Id
    }
  }
  Until ($CurrentAccount -gt 0)
  $CurrentSubName = $context.Subscription.SubscriptionName
  $CurrentSubId = $context.Subscription.SubscriptionId

  #Check for existing profile
  Write-Output -InputObject 'Looking for existing profile'
  $ExistingProfile = Get-KeyVaultRepoProfile
  If ($ExistingProfile)
  {
    Write-Output -InputObject "Existing Profile is found for user '$($context.Account.Id)'. Loading profile..."
    if ($CurrentSubId -ine $ExistingProfile.AzureSubId)
    {
      Write-Output "Setting Azure Subscription '$($ExistingProfile.AzureSubId)' to the context."
      $null = Set-AzureRmContext -SubscriptionId $ExistingProfile.AzureSubId
    }
    Write-Output "Connecting to key vault '$($ExistingProfile.KeyVaultName)'"
    $KeyVault = Get-AzureRmKeyVault -VaultName $ExistingProfile.KeyVaultName
    $Global:KeyVaultName = $KeyVault.VaultName
  }
  else 
  {
    Write-Host -Object "No Profile is found for user '$($context.Account.Id)'" -ForegroundColor Red
    #Select Azure subscription
    If ($CurrentSubName -ne $null)
    {
      $SelectSubTitle = 'Select Azure Subscription'
      $SelectSubMessage = "Currently the Azure subscription '$CurrentSubName (Id: $CurrentSubId)' is selected. Do you want to use this subscription?"
      $yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Use the current subscription.'
      $no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', 'Select another subscription.'
      $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

      $UserSelected = $host.ui.PromptForChoice($SelectSubTitle, $SelectSubMessage, $options, 0)
    }
    If($UserSelected -eq 1)
    {
      Write-Output -InputObject 'Getting Azure subscriptions...'
      $subscriptions = Get-AzureRmSubscription -WarningAction SilentlyContinue
      if ($subscriptions.count -gt 0)
      {
        Write-Host -Object 'Select Azure Subscription of which the Azure Key Vault is located' -ForegroundColor DarkGray

        $menu = @{}
        for ($i = 1;$i -le $subscriptions.count; $i++) 
        {
          Write-Host -Object "$i. $($subscriptions[$i-1].SubscriptionName)" -ForegroundColor Yellow
          $menu.Add($i,($subscriptions[$i-1].SubscriptionId))
        }
        Do 
        {
          [int]$ans = Read-Host -Prompt "Enter selection (1 - $($i -1))"
        }
        while ($ans -le 0 -or $ans -gt $($i -1))
        Write-Output -InputObject ''
        $subscriptionID = $menu.Item($ans)
        $null = Set-AzureRmContext -SubscriptionId $subscriptionID
      }
      else 
      {
        Write-Error -Message 'No Azure Subscription found. Unable to continue!'
        Exit -1
      }
    }
    $context = Get-AzureRmContext
    $subscriptionID = $context.Subscription.SubscriptionId
    #Check for existing key vault configured for AaaS solutions
    $ExistingKeyVaults = Get-AzureRmKeyVault |
    Where-Object -FilterScript {
      $_.Tags.ContainsKey($IdentifyingTagName)
    } |
    Where-Object -FilterScript {
      $_.Tags["$IdentifyingTagName"] -eq $IdentifyingTagValue
    }
    Write-Output -InputObject ''
    if ($ExistingKeyVaults.count -eq 0)
    {
      Write-Host -Object 'No existing AaaS key vault detected. select 0 to create a new key vault.' -ForegroundColor Red
    }
    else
    {
      Write-Host -Object 'Select a Key Vault' -ForegroundColor DarkGray
    }
    Write-Host -Object '0. [Create New Key Vault]' -ForegroundColor DarkGray
    $i = 0
    Foreach ($KV in $ExistingKeyVaults)
    {
      $i++
      Write-Host -Object "$i. $($KV.VaultName)" -ForegroundColor Yellow
    }
    Do 
    {
      [int]$KVAnswer = Read-Host -Prompt "Enter selection (0 - $i)"
    }
    while ($KVAnswer -lt 0 -or $KVAnswer -gt $i)
    Write-Output -InputObject ''
    If ($KVAnswer -eq 0)
    {
      #Create new key vault
      $AzureLocations = Get-AzureRmLocation
      $ExistingResourceGroups = @()
      #get resource groups
      Foreach ($item in Get-AzureRmResourceGroup)
      {
        $RGLoc = $AzureLocations | Where-Object -FilterScript {
          $item.Location -eq $_.Location
        }
        if ($RGLoc.Providers -contains 'Microsoft.KeyVault')
        {
          $ExistingResourceGroups += $item
        }
      }

      Write-Host -Object 'Select a resource group' -ForegroundColor DarkGray
      Write-Host -Object '0. [Create New Resource Group]' -ForegroundColor DarkGray
      $i = 0
      Foreach ($RG in $ExistingResourceGroups)
      {
        $i++
        Write-Host -Object "$i. $($RG.ResourceGroupName)"  -ForegroundColor Yellow
      }
      Do 
      {
        [int]$RGAnswer = Read-Host -Prompt "Enter selection (1 - $i)"
      }
      while ($RGAnswer -lt 0 -or $RGAnswer -gt $i)
      Write-Output -InputObject ''

      if ($RGAnswer -eq 0)
      {
        #Create a new resource group
        #resource group name
        Do
        {
          $ResourceGroupName = Read-Host -Prompt 'Enter the name for the new Resource Group (only include alphanumeric characters, periods, underscores, hyphens and parenthesis and cannot end in a period.)'
        }
        while ($ResourceGroupName.Length -eq 0 -or $ResourceGroupName -notmatch '^[-\w\._\(\)]+$')
        #location
        $AvailableLocations = $AzureLocations |
        Where-Object -FilterScript {
          $_.Providers -contains 'Microsoft.Resources' -and $_.Providers -contains 'Microsoft.KeyVault'
        } |
        Sort-Object -Property DisplayName
        Write-Host -Object 'Select Azure Region:' -ForegroundColor DarkGray
        $i = 0
        foreach ($loc in $AvailableLocations)
        {
          $i++
          Write-Host -Object "$i. $($loc.DisplayName)" -ForegroundColor Yellow
        }
        Do 
        {
          [int]$ans = Read-Host -Prompt "Enter selection (1 - $i)"
        }
        while ($ans -le 0 -or $ans -gt $i)
        Write-Output -InputObject ''
        $Location = $AvailableLocations[$ans-1]
        $strLocation = $Location.Location
        Write-Output -InputObject "Creating new Resource Group '$ResourceGroupName' in Azure region '$($Location.DisplayName)'."
        $NewRG = New-AzureRmResourceGroup -Name $ResourceGroupName -Location $strLocation
        If ($NewRG.ProvisioningState -eq 'Succeeded')
        {
          Write-Host -Object "Resource group '$ResourceGroupName' successfully created." -ForegroundColor Green
        }
        else 
        {
          Write-Host -Object "Resource Group creation failed. provisioning state: '$($NewRG.ProvisioningState)'." -ForegroundColor Red
        }
        Write-Output -InputObject ''
      }
      else 
      {
        #choose existing Resource Group
        $ResourceGroupName = $ExistingResourceGroups[$RGAnswer -1].ResourceGroupName
        $strLocation = $ExistingResourceGroups[$RGAnswer -1].Location
        Write-Output -InputObject ''
      }

      #Create Key Vault
      #Key Vault name
      Do
      {
        $Global:KeyVaultName = Read-Host -Prompt 'Enter the name for the new Key Vault (only include alphanumeric characters and dashes and cannot start with a number.)'
      }
      while ($Global:KeyVaultName.Length -eq 0 -or $Global:KeyVaultName -notmatch '^[a-zA-Z0-9-]{3,24}$')
      Write-Output -InputObject 'Creating Azure Key Vault'
      $KeyVault = New-AzureRmKeyVault -VaultName $Global:KeyVaultName -ResourceGroupName $ResourceGroupName -Location $strLocation -EnabledForDeployment -EnabledForTemplateDeployment -EnabledForDiskEncryption -Sku Standard -Tag @{
        $IdentifyingTagName = $IdentifyingTagValue
      } -Confirm:$false
      $Global:KeyVaultName = $KeyVault.VaultName

      #Give someone access to the key vault
      Write-Output -InputObject 'Assigning Azure Key Vault permission to an Azure AD user. Searching Azure AD to get the AD user.'
      Add-KeyVaultFullAccessPolicy
    }
    else 
    {
      $KeyVault = $ExistingKeyVaults[$KVAnswer-1]
      $Global:KeyVaultName = $KeyVault.VaultName
    }
  }

  #endregion

  #region manage key vault secrets
  Clear-Host
  Do
  {
    Write-Host -Object $TopMenuTitleLine1 -ForegroundColor Cyan
    Write-Host -Object $TopMenuTitleLine2 -ForegroundColor Cyan
    Write-Host -Object " Selected Key Vault: '$Global:KeyVaultName'" -ForegroundColor Cyan
    Write-Host -Object "Select what you'd like to do:" -ForegroundColor DarkGray
    Write-Host -Object '1. Create new credential' -ForegroundColor Yellow
    Write-Host -Object '2. Choose existing credentials from the list' -ForegroundColor Yellow
    Write-Host -Object '3. Search existing credentials' -ForegroundColor Yellow
    Write-Host -Object '4. Save Azure subscription and Key Vault selection in Key Vault Password Repository profile' -ForegroundColor Yellow
    Write-Host -Object '5. Delete Key Vault Password Repository profile' -ForegroundColor Yellow
    Write-Host -Object '6. Grant Key Vault Access' -ForegroundColor Yellow
    Write-Host -Object '7. Remove Key Vault Access' -ForegroundColor Yellow
    Write-Host -Object '8. Exit' -ForegroundColor DarkGray
  
    Do 
    {
      [int]$option = Read-Host -Prompt 'Enter selection (1 - 8)'
    }
    while ($option -le 0 -or $option -gt 8)
    Write-Output -InputObject ''
    SWITCH ($option)
    {
      1 
      {
        #Create new
        New-KeyVaultCred
        Write-Output -InputObject ''
      }
      2 
      {
        #list existing
        Get-KeyVaultCred
      }
      3 
      {
        #search existing
        Get-KeyVaultCred -Search
      }
      4 
      {
        #Save profile
        Save-KeyVaultRepoProfile
      }
      5 
      {
        #Delete profile
        Remove-KeyVaultRepoProfile
      }
      6
      {
        #Grant access
        Add-KeyVaultFullAccessPolicy
      }
      7
      {
        #Grant access
        Remove-KeyVaultFullAccessPolicy
      }
      8 
      {
        Write-Output -InputObject 'See you next time!'
      }
    }
  }
  Until ($option -eq 8)
}

New-Alias -Name ipr -Value Invoke-AzureKeyVaultPasswordRepository
New-Alias -Name Start-PasswordRepo -Value Invoke-AzureKeyVaultPasswordRepository
Export-ModuleMember -Alias * -Function *