PoshCode Archive  Artifact [92681c7e80]

Artifact 92681c7e807a2760d295da745e41bc19ac174d237600d8449494348be2abe545:

  • File Get-Credential.ps1 — part of check-in [b3296157c9] at 2018-06-10 14:21:11 on branch trunk — An improvement over the default Get-Credential cmdlet (doesn’t take much, eh?) offering, among other things, a -Console switch. (user: Joel Bennett size: 1876)

# encoding: ascii
# api: powershell
# title: Get-Credential
# description: An improvement over the default Get-Credential cmdlet (doesn’t take much, eh?) offering, among other things, a -Console switch.
# version: 1.2
# type: function
# author: Joel Bennett
# license: CC0
# function: Get-Credential
# x-poshcode-id: 681
# x-archived: 2009-01-05T16:58:49
#
#
## Get-Credential 
## An improvement over the default cmdlet which has no options ...
###################################################################################################
## History
## v 1.2 Refactor ShellIds key out to a variable, and wrap lines a bit
## v 1.1 Add -Console switch and set registry values accordingly (ouch)
## v 1.0 Add Title, Message, Domain, and UserName options to the Get-Credential cmdlet
###################################################################################################
function Get-Credential{ 
PARAM([String]$Title, [String]$Message, [String]$Domain, [String]$UserName, [switch]$Console)
   $ShellIdKey = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds"
   ## Carefully EA=SilentlyContinue because by default it's MISSING, not $False
   $cp = (Get-ItemProperty $ShellIdKey ConsolePrompting -ea "SilentlyContinue")
   ## Compare to $True, because by default it's $null ...
   $cp = $cp.ConsolePrompting -eq $True

   if($Console -and !$cp) {
      Set-ItemProperty $ShellIdKey ConsolePrompting $True
   } elseif(!$Console -and $Console.IsPresent -and $cp) {
      Set-ItemProperty $ShellIdKey ConsolePrompting $False
   }

   ## Now call the Host.UI method ... if they don't have one, we'll die, yay.
   $Host.UI.PromptForCredential($Title,$Message,$UserName,$Domain)

   ## BoyScouts: Leave everything better than you found it (I'm tempted to leave it = True)
   Set-ItemProperty $ShellIdKey ConsolePrompting $cp
}