@@ -11,27 +11,73 @@ namespace UiPath.PowerShell.Cmdlets
11
11
/// <summary>
12
12
/// <para type="synopsis">Obtains an UiPath authentication token</para>
13
13
/// <para type="description">The authentication token is needed for other UiPath Powershell cmdlets.</para>
14
+ /// <example>
15
+ /// <code>Get-UiPathAuthToken -URL https://platform.uipath.com -Username <myuser> -Password <mypassword></code>
16
+ /// <para>Connect to UiPath public platform Orchestrator, using user name and password.</para>
17
+ /// </example>
18
+ /// <example>
19
+ /// <code>Get-UiPathAuthToken -URL https://platform.uipath.com -Username <myuser> -Password <mypassword> -Session</code>
20
+ /// <para>Connect to UiPath public platform Orchestrator, using user name and password and save the token for the current session.</para>
21
+ /// </example>
22
+ /// <example>
23
+ /// <code>Get-UiPathAuthToken -URL https://uipath.corpnet -WindowsCredentials -Session</code>
24
+ /// <para>Connect to a private Orchestrator with Windows enabled, using current Windows credentials and save the token for current session.</para>
25
+ /// </example>
14
26
/// </summary>
15
27
[ Cmdlet ( VerbsCommon . Get , Nouns . AuthToken ) ]
16
28
public class GetAuthToken : UiPathCmdlet
17
29
{
30
+ private const string UserPasswordSet = "UserPassword" ;
31
+ private const string WindowsCredentialsSet = "WindowsCredentials" ;
32
+
18
33
[ Parameter ( Mandatory = true , Position = 0 ) ]
19
34
public string URL { get ; set ; }
20
35
21
36
[ Parameter ( Mandatory = false ) ]
22
37
public string TenantName { get ; set ; }
23
38
24
- [ Parameter ( Mandatory = true ) ]
39
+ [ Parameter ( Mandatory = true , ParameterSetName = UserPasswordSet ) ]
25
40
public string Username { get ; set ; }
26
41
27
- [ Parameter ( Mandatory = true ) ]
42
+ [ Parameter ( Mandatory = true , ParameterSetName = UserPasswordSet ) ]
28
43
public string Password { get ; set ; }
29
44
45
+ [ Parameter ( Mandatory = true , ParameterSetName = WindowsCredentialsSet ) ]
46
+ public SwitchParameter WindowsCredentials { get ; set ; }
47
+
30
48
[ Parameter ]
31
49
public SwitchParameter Session { get ; set ; }
32
50
33
51
protected override void ProcessRecord ( )
34
52
{
53
+ AuthToken authToken = null ;
54
+ if ( ParameterSetName == UserPasswordSet )
55
+ {
56
+ authToken = GetUserToken ( ) ;
57
+ }
58
+ else if ( ParameterSetName == WindowsCredentialsSet )
59
+ {
60
+ authToken = GetWindowsToken ( ) ;
61
+ }
62
+ if ( Session . IsPresent )
63
+ {
64
+ AuthenticatedCmdlet . SetAuthToken ( authToken ) ;
65
+ }
66
+
67
+ WriteObject ( authToken ) ;
68
+ }
69
+
70
+ private AuthToken GetWindowsToken ( )
71
+ {
72
+ return new AuthToken
73
+ {
74
+ URL = URL ,
75
+ WindowsCredentials = true
76
+ } ;
77
+ }
78
+
79
+ private AuthToken GetUserToken ( )
80
+ {
35
81
var creds = new BasicAuthenticationCredentials ( ) ;
36
82
using ( var client = new UiPathWebApi ( creds )
37
83
{
@@ -47,18 +93,13 @@ protected override void ProcessRecord()
47
93
var response = HandleHttpOperationException ( ( ) => client . Account . Authenticate ( loginModel ) ) ;
48
94
var token = ( string ) response . Result ;
49
95
50
- var authToken = new AuthToken
96
+ return new AuthToken
51
97
{
52
98
Token = token ,
53
- URL = URL
99
+ URL = URL ,
100
+ WindowsCredentials = false
54
101
} ;
55
102
56
- if ( Session . IsPresent )
57
- {
58
- AuthenticatedCmdlet . SetAuthToken ( authToken ) ;
59
- }
60
-
61
- WriteObject ( authToken ) ;
62
103
}
63
104
}
64
105
}
0 commit comments