Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fa0642

Browse files
committed
Get-UiPathPackageDependencies example script
- require token suport (`-CurrentSession`, `$_.TenantName` etc) - required GetSettings support (for deployment URL) - example that download nuspec, parses dependencies
1 parent 040cdd0 commit 6fa0642

13 files changed

+317
-18
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<#
2+
.SYNOPSIS
3+
Extracts the declared dependencies for an Orchestrator package
4+
.DESCRIPTION
5+
You should first import the UiPath.PowerShell module and authenticate yourself with your Orchestrator using Get-UiPathAuthToken before running this script.
6+
This is a an example script provided as-is.
7+
.PARAMETER Package
8+
The Orchestrator package. Use Get-UiPathPackage to retrive this object.
9+
.EXAMPLE
10+
$package = Get-UiPathPackage -Id <my package name>
11+
.\Get-UiPathPackageDependencies.ps1 $package
12+
Lists the package dependencies of a specific package
13+
.EXAMPLE
14+
Get-UiPathPackage | ForEach-Object { .\Examples\Get-UiPathPackageDependencies.ps1 $_ | Add-Member Parent $_.Id -PassThru}
15+
Lists the package dependencies for all packages in Orchestrator
16+
#>
17+
18+
param(
19+
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline)]
20+
[object] $Package
21+
)
22+
23+
$ErrorActionPreference = "Stop"
24+
25+
26+
function Get-UiPathDeploymentUrl {
27+
param(
28+
[object] $token
29+
)
30+
31+
$deploymentUrlSetting = Get-UiPathSetting | where {$_.Name -eq 'DeploymentUrl'}
32+
33+
if ([string]::IsNullOrWhiteSpace($deploymentUrlSetting.Value))
34+
{
35+
$url = "$($token.URL)/nuget/feed/$($token.TenantName.ToLowerInvariant())"
36+
37+
}
38+
else
39+
{
40+
$url = $deploymentUrlSetting.Value
41+
}
42+
Write-Verbose "NuGet Feed: $url"
43+
return $url
44+
}
45+
46+
$token = Get-UiPathAuthToken -CurrentSession
47+
48+
49+
$deploymentUrl = Get-UiPathDeploymentUrl $token
50+
51+
$specUrl = "$deploymentUrl/Packages(Id='$($Package.Id.ToLowerInvariant())',Version='$($Package.Version.ToLowerInvariant())')"
52+
53+
if ($token.WindowsCredentials) {
54+
Write-Verbose "Invoke-WebRequest $specUrl -UseDefaultCredentials"
55+
$response = Invoke-WebRequest $specUrl -UseDefaultCredentials
56+
} else {
57+
Write-Verbose "Invoke-WebRequest $specUrl -Headers @{Authorization = 'Bearer ...'"
58+
$response = Invoke-WebRequest $specUrl -Headers @{Authorization = "Bearer $($token.Token)"}
59+
}
60+
61+
62+
$content = [XML] $response.Content
63+
64+
$dependencies = $content.entry.properties.Dependencies
65+
66+
67+
if (-not [string]::IsNullOrWhiteSpace($dependencies))
68+
{
69+
$splits = $dependencies.split("|")
70+
foreach($split in $splits)
71+
{
72+
$parts = $split.split(":");
73+
$lib = New-Object -TypeName psobject
74+
$lib | Add-Member -MemberType NoteProperty -Name Id $parts[0]
75+
$lib | Add-Member -MemberType NoteProperty -Name Version $parts[1]
76+
Write-Output $lib
77+
}
78+
}

UiPath.Orchestrator.Powershell.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UiPath.PowerShell.Tests", "
2424
EndProject
2525
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{BBCF7871-E3BE-491E-B522-8616CFACB4E8}"
2626
ProjectSection(SolutionItems) = preProject
27+
Examples\Get-UiPathPackageDependencies.ps1 = Examples\Get-UiPathPackageDependencies.ps1
2728
Examples\Sync-UiPathADUsers.ps1 = Examples\Sync-UiPathADUsers.ps1
2829
Examples\Update-UiPathADRobotPasswords.ps1 = Examples\Update-UiPathADRobotPasswords.ps1
2930
EndProjectSection

UiPath.PowerShell/Cmdlets/GetAuthToken.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ public class GetAuthToken: UiPathCmdlet
3838
private const string WindowsCredentialsSet = "WindowsCredentials";
3939
private const string UnauthenticatedSet = "Unauthenticated";
4040

41-
[Parameter(Mandatory = true, Position = 0)]
42-
public string URL { get; set; }
41+
private const string CurrentSessionSet = "CurrentSession";
4342

44-
[Parameter(Mandatory = false)]
45-
public string TenantName { get; set; }
43+
[Parameter(Mandatory = true, ParameterSetName =CurrentSessionSet)]
44+
public SwitchParameter CurrentSession { get; set; }
45+
46+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = UserPasswordSet)]
47+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = WindowsCredentialsSet)]
48+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = UnauthenticatedSet)]
49+
public string URL { get; set; }
4650

4751
[Parameter(Mandatory = true, ParameterSetName = UserPasswordSet)]
4852
public string Username { get; set; }
@@ -56,19 +60,32 @@ public class GetAuthToken: UiPathCmdlet
5660
[Parameter(Mandatory = true, ParameterSetName = UnauthenticatedSet)]
5761
public SwitchParameter Unauthenticated { get; set; }
5862

63+
[Parameter(Mandatory = false, ParameterSetName = UserPasswordSet)]
64+
[Parameter(Mandatory = false, ParameterSetName = WindowsCredentialsSet)]
65+
[Parameter(Mandatory = false, ParameterSetName = UnauthenticatedSet)]
66+
public string TenantName { get; set; }
67+
5968
/// <summary>
6069
/// Sets the current Organization Unit for the authentication token.
6170
/// This parameter is only valid for ORchestrator deployments with Organization Units feature enabled.
6271
/// </summary>
63-
[Parameter]
72+
[Parameter(Mandatory = false, ParameterSetName = UserPasswordSet)]
73+
[Parameter(Mandatory = false, ParameterSetName = WindowsCredentialsSet)]
74+
[Parameter(Mandatory = false, ParameterSetName = UnauthenticatedSet)]
6475
public string OrganizationUnit { get; set; }
6576

66-
[Parameter]
77+
[Parameter(Mandatory = false, ParameterSetName = UserPasswordSet)]
78+
[Parameter(Mandatory = false, ParameterSetName = WindowsCredentialsSet)]
79+
[Parameter(Mandatory = false, ParameterSetName = UnauthenticatedSet)]
6780
public SwitchParameter Session { get; set; }
6881

6982
protected override void ProcessRecord()
7083
{
71-
try
84+
if (ParameterSetName == CurrentSessionSet)
85+
{
86+
WriteObject(AuthenticatedCmdlet.SessionAuthToken);
87+
}
88+
else
7289
{
7390
AuthToken authToken = null;
7491
if (ParameterSetName == UserPasswordSet)
@@ -91,17 +108,15 @@ protected override void ProcessRecord()
91108
SetOrganizationUnit(authToken, OrganizationUnit);
92109
}
93110

111+
authToken.TenantName = TenantName ?? "Default";
112+
94113
if (Session.IsPresent)
95114
{
96115
AuthenticatedCmdlet.SetAuthToken(authToken);
97116
}
98117

99118
WriteObject(authToken);
100119
}
101-
catch(Exception e)
102-
{
103-
WriteVerbose(e.ToString());
104-
}
105120
}
106121

107122
private void SetOrganizationUnit(AuthToken authToken, string organizationUnit)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Management.Automation;
3+
using UiPath.PowerShell.Models;
4+
using UiPath.PowerShell.Util;
5+
using UiPath.Web.Client20181;
6+
using UiPath.Web.Client20181.Models;
7+
8+
namespace UiPath.PowerShell.Cmdlets
9+
{
10+
[Cmdlet(VerbsCommon.Get, Nouns.Setting)]
11+
public class GetSettings : FilteredBaseCmdlet
12+
{
13+
public enum SettingsType
14+
{
15+
General,
16+
Authentication,
17+
Web
18+
}
19+
20+
[Parameter]
21+
[ValidateEnum(typeof(SettingsType))]
22+
public string Type { get; set; }
23+
24+
protected override void ProcessRecord()
25+
{
26+
SettingsType type = SettingsType.General;
27+
Enum.TryParse<SettingsType>(Type, out type);
28+
29+
switch (type)
30+
{
31+
case SettingsType.General:
32+
ProcessImpl(
33+
filter => Api.Settings.GetSettings(filter: filter).Value,
34+
dto => Setting.FromDto(dto));
35+
break;
36+
case SettingsType.Authentication:
37+
ProcessImpl(
38+
filter => Api.Settings.GetAuthenticationSettings().Value,
39+
dto => Setting.FromDto(dto));
40+
break;
41+
case SettingsType.Web:
42+
ProcessImpl(
43+
filter => Api.Settings.GetWebSettings().Value,
44+
dto => Setting.FromDto(dto));
45+
break;
46+
}
47+
}
48+
}
49+
}

UiPath.PowerShell/Cmdlets/Nouns.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal static class Nouns
2727
internal const string RolePermission = UiPath + "RolePermission";
2828
internal const string QueueDefinition = UiPath + "QueueDefinition";
2929
internal const string QueueItem = UiPath + "QueuItem";
30+
internal const string Setting = UiPath + "Setting";
3031
internal const string Tenant = UiPath + "Tenant";
3132
internal const string TimeZones = UiPath + "TimeZones";
3233
internal const string User = UiPath + "User";

UiPath.PowerShell/Models/AuthToken.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AuthToken
1212
public string Token { get; internal set; }
1313

1414
public bool WindowsCredentials { get; internal set; }
15+
1516
public bool Authenticated { get; internal set; }
1617

1718
public Version ApiVersion { get; internal set; }
@@ -20,6 +21,8 @@ public class AuthToken
2021

2122
public string OrganizationUnit { get; internal set; }
2223

24+
public string TenantName { get; internal set; }
25+
2326
internal long? OrganizationUnitId { get; set; }
2427
}
2528
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UiPath.Web.Client20181.Models;
7+
8+
namespace UiPath.PowerShell.Models
9+
{
10+
public class Setting
11+
{
12+
public string Name { get; private set; }
13+
public SettingsDtoScope? Scope { get; private set; }
14+
public string Value { get; private set; }
15+
16+
internal static Setting FromDto(SettingsDto dto)
17+
{
18+
return new Setting
19+
{
20+
Name = dto.Name,
21+
Scope = dto.Scope,
22+
Value = dto.Value
23+
};
24+
}
25+
26+
internal static object FromDto(KeyValuePairStringString dto, SettingsDtoScope? scope = null)
27+
{
28+
return new Setting
29+
{
30+
Name = dto.Key,
31+
Value = dto.Value,
32+
Scope = scope
33+
};
34+
}
35+
}
36+
}

UiPath.PowerShell/UiPath.PowerShell.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Cmdlets\GetPermission.cs" />
104104
<Compile Include="Cmdlets\GetRole.cs" />
105105
<Compile Include="Cmdlets\GetProcessSchedule.cs" />
106+
<Compile Include="Cmdlets\GetSettings.cs" />
106107
<Compile Include="Cmdlets\GetTimeZones.cs" />
107108
<Compile Include="Cmdlets\GetWebhook.cs" />
108109
<Compile Include="Cmdlets\GrantRolePermission.cs" />
@@ -151,6 +152,7 @@
151152
<Compile Include="Models\Role.cs" />
152153
<Compile Include="Cmdlets\RevokeRolePermission.cs" />
153154
<Compile Include="Models\ProcessSchedule.cs" />
155+
<Compile Include="Models\Setting.cs" />
154156
<Compile Include="Models\Timezone.cs" />
155157
<Compile Include="Models\Webhook.cs" />
156158
<Compile Include="Util\AuthenticatedCmdlet.cs" />
@@ -160,6 +162,7 @@
160162
<Compile Include="Util\FilteredIdCmdlet.cs" />
161163
<Compile Include="Util\EditCmdlet.cs" />
162164
<Compile Include="Util\HashtableExtenssions.cs" />
165+
<Compile Include="Util\KeyValuePairConverter.cs" />
163166
<Compile Include="Util\NetworkAuthenticationCredentials.cs" />
164167
<Compile Include="Util\OrchestratorProtocolVersion.cs" />
165168
<Compile Include="Util\PSCredentialExtenssions.cs" />

UiPath.PowerShell/Util/AuthenticatedCmdlet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class AuthenticatedCmdlet: UiPathCmdlet
2222
private UiPathWebApi_18_3 _api_18_3;
2323
private UiPathWebApi_18_4 _api_18_4;
2424

25-
private static AuthToken SessionAuthToken { get; set; }
25+
internal static AuthToken SessionAuthToken { get; set; }
2626

2727
protected AuthToken InternalAuthToken
2828
{
@@ -126,6 +126,7 @@ internal static T MakeApi<T>(AuthToken authToken, Func<ServiceClientCredentials,
126126
var api = ctor(creds, new Uri(authToken.URL));
127127
api.SetRetryPolicy(null);
128128
api.SerializationSettings.Converters.Add(new SpecificItemDtoConverter());
129+
api.DeserializationSettings.Converters.Add(new KeyValuePairConverter());
129130
if (authToken.OrganizationUnitId.HasValue)
130131
{
131132
api.HttpClient.DefaultRequestHeaders.Add("X-UIPATH-OrganizationUnitId", authToken.OrganizationUnitId.Value.ToString());
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using UiPath.Web.Client20181.Models;
7+
8+
namespace UiPath.PowerShell.Util
9+
{
10+
internal class KeyValuePairConverter: JsonConverter
11+
{
12+
public override bool CanConvert(Type objectType)
13+
{
14+
return objectType.IsAssignableFrom(typeof(ODataResponseListKeyValuePairStringString));
15+
}
16+
17+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
18+
{
19+
var jObject = JObject.Load(reader);
20+
21+
var jContext = jObject.GetValue("@odata.context", StringComparison.InvariantCultureIgnoreCase);
22+
var jKeys = jObject.GetValue("Keys", StringComparison.InvariantCultureIgnoreCase);
23+
var jValues = jObject.GetValue("Values", StringComparison.InvariantCultureIgnoreCase);
24+
25+
var keys = jKeys as JArray;
26+
var values = jValues as JArray;
27+
28+
if (keys == null || values == null || keys.Count != values.Count)
29+
{
30+
return null;
31+
}
32+
33+
var kv = new List<KeyValuePairStringString>();
34+
for (int i=0; i< keys.Count; ++i)
35+
{
36+
kv.Add(new KeyValuePairStringString(
37+
keys[i].Value<string>(),
38+
values[i].Value<string>()));
39+
}
40+
41+
var resp = new ODataResponseListKeyValuePairStringString(
42+
jContext.Value<string>(),
43+
kv);
44+
serializer.Populate(jObject.CreateReader(), resp);
45+
return resp;
46+
}
47+
48+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
49+
{
50+
throw new NotImplementedException();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)