MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation;6 7namespace Microsoft.PowerShell.Commands8{9 /// <summary>10 /// Defines the implementation of the 'get-credential' cmdlet.11 /// The get-credential Cmdlet establishes a credential object called a12 /// PSCredential, by pairing a given username with13 /// a prompted password. That credential object can then be used for other14 /// operations involving security.15 /// </summary>16 [Cmdlet(VerbsCommon.Get, "Credential", DefaultParameterSetName = GetCredentialCommand.credentialSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096824")]17 [OutputType(typeof(PSCredential), ParameterSetName = new string[] { GetCredentialCommand.credentialSet, GetCredentialCommand.messageSet })]18 public sealed class GetCredentialCommand : PSCmdlet19 {20 /// <summary>21 /// The Credential parameter set name.22 /// </summary>23 private const string credentialSet = "CredentialSet";24 25 /// <summary>26 /// The Message parameter set name.27 /// </summary>28 private const string messageSet = "MessageSet";29 30 /// <summary>31 /// Gets or sets the underlying PSCredential of32 /// the instance.33 /// </summary>34 [Parameter(Position = 0, ParameterSetName = credentialSet)]35 [ValidateNotNull]36 [Credential]37 public PSCredential Credential { get; set; }38 39 /// <summary>40 /// Gets and sets the user supplied message providing description about which script/function is41 /// requesting the PSCredential from the user.42 /// </summary>43 [Parameter(Mandatory = false, ParameterSetName = messageSet)]44 [ValidateNotNullOrEmpty]45 public string Message46 {47 get { return _message; }48 49 set { _message = value; }50 }51 52 private string _message = UtilsStrings.PromptForCredential_DefaultMessage;53 54 /// <summary>55 /// Gets and sets the user supplied username to be used while creating the PSCredential.56 /// </summary>57 [Parameter(Position = 0, Mandatory = false, ParameterSetName = messageSet)]58 [ValidateNotNullOrEmpty]59 public string UserName60 {61 get { return _userName; }62 63 set { _userName = value; }64 }65 66 private string _userName = null;67 68 /// <summary>69 /// Gets and sets the title on the window prompt.70 /// </summary>71 [Parameter(Mandatory = false, ParameterSetName = messageSet)]72 [ValidateNotNullOrEmpty]73 public string Title74 {75 get { return _title; }76 77 set { _title = value; }78 }79 80 private string _title = UtilsStrings.PromptForCredential_DefaultCaption;81 82 /// <summary>83 /// Initializes a new instance of the GetCredentialCommand84 /// class.85 /// </summary>86 public GetCredentialCommand() : base()87 {88 }89 90 /// <summary>91 /// The command outputs the stored PSCredential.92 /// </summary>93 protected override void BeginProcessing()94 {95 if (Credential != null)96 {97 WriteObject(Credential);98 return;99 }100 101 try102 {103 Credential = this.Host.UI.PromptForCredential(_title, _message, _userName, string.Empty);104 }105 catch (ArgumentException exception)106 {107 ErrorRecord errorRecord = new(108 exception,109 "CouldNotPromptForCredential",110 ErrorCategory.InvalidOperation,111 targetObject: null);112 WriteError(errorRecord);113 }114 115 if (Credential != null)116 {117 WriteObject(Credential);118 }119 }120 }121}122 