Get-Credential 1.2 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/681"></script>download | new post
An improvement over the default Get-Credential cmdlet (doesn’t take much, eh?) offering, among other things, a -Console switch.
- ## 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
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
PowerShell Code Repository