forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialCommands.cs
More file actions
114 lines (102 loc) · 3.92 KB
/
Copy pathCredentialCommands.cs
File metadata and controls
114 lines (102 loc) · 3.92 KB
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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Defines the implementation of the 'get-credential' cmdlet.
/// The get-credential Cmdlet establishes a credential object called a
/// Msh credential, by pairing a given username with
/// a prompted password. That credential object can then be used for other
/// operations involving security.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Credential", DefaultParameterSetName = GetCredentialCommand.credentialSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113311")]
[OutputType(typeof(PSCredential), ParameterSetName = new string[] { GetCredentialCommand.credentialSet, GetCredentialCommand.messageSet })]
public sealed class GetCredentialCommand : PSCmdlet
{
/// <summary>
/// The Credential parameter set name.
/// </summary>
private const string credentialSet = "CredentialSet";
/// <summary>
/// The Message parameter set name.
/// </summary>
private const string messageSet = "MessageSet";
/// <summary>
/// Gets or sets the underlying PSCredential of
/// the instance.
/// </summary>
///
[Parameter(Position = 0, ParameterSetName = credentialSet)]
[ValidateNotNull]
[Credential()]
public PSCredential Credential { get; set; }
/// <summary>
/// Gets and sets the user supplied message providing description about which script/function is
/// requesting the PSCredential from the user.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = messageSet)]
[ValidateNotNullOrEmpty]
public string Message
{
get { return _message; }
set { _message = value; }
}
private string _message = UtilsStrings.PromptForCredential_DefaultMessage;
/// <summary>
/// Gets and sets the user supplied username to be used while creating the PSCredential.
/// </summary>
[Parameter(Position = 0, Mandatory = false, ParameterSetName = messageSet)]
[ValidateNotNullOrEmpty()]
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _userName = null;
/// <summary>
/// Gets and sets the title on the window prompt.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = messageSet)]
[ValidateNotNullOrEmpty]
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _title = UtilsStrings.PromptForCredential_DefaultCaption;
/// <summary>
/// Initializes a new instance of the GetCredentialCommand
/// class
/// </summary>
public GetCredentialCommand() : base()
{
}
/// <summary>
/// The command outputs the stored PSCredential.
/// </summary>
protected override void BeginProcessing()
{
if (Credential != null)
{
WriteObject(Credential);
return;
}
try
{
Credential = this.Host.UI.PromptForCredential(_title, _message, _userName, string.Empty);
}
catch (ArgumentException exception)
{
ErrorRecord errorRecord = new ErrorRecord(exception, "CouldNotPromptForCredential", ErrorCategory.InvalidOperation, null);
WriteError(errorRecord);
}
if (Credential != null)
{
WriteObject(Credential);
}
}
}
}