UiPath API - change asset

Hi,

Im trying to change an asset with powershell, but it gives me always an “assetDto must not be null.” (the XXXX is our erased organisation + tenant, we also receive a working token)

$headers_changeasset = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers_changeasset.Add(“Authorization”, "Bearer " + $token)
$headers_changeasset.Add(“X-UiPath-OrganizationUnitid”, “1940639”)

$body_changeasset = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$body_changeasset.Add(‘Name’, ‘test’)
$body_changeasset.Add(‘ValueScope’, ‘Global’)
$body_changeasset.Add(‘ValueType’, ‘Text’)
$body_changeasset.Add(‘StringValue’, ‘itsnotblabla’)
$body_changeasset.Add(‘ID’,‘332287’)

$URL_changeasset = “UiPath

$response_changeasset = Invoke-RestMethod -Uri $URL_changeasset -Method ‘PUT’ -Headers $headers_changeasset -Body $body_changeasset -ContentType application/json
$response_changeasset | ConvertTo-Html

If we use the GET function with the same folder/asset ID its works, just seems not to work with PUT. (we also saw every movie and tutorial there is to find about the topic + its not even mentionned PUT for assets onhttps://docs.uipath.com/orchestrator/reference/assets-requests)

Thx,
Fabian

It looks fine for me, The only query I have is about the body object you are passing ?

It is a dictionary but not sure if that should be Json serialized ?

Thanks
#nK

Hi nK,

I get the same results:

$headers_changeasset = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”
$headers_changeasset.Add(“Authorization”, "Bearer " + $token)
$headers_changeasset.Add(“X-UiPath-OrganizationUnitid”, “1940639”)

$body_changeasset = “Name=test&ValueScope=Global&ValueType=Text&StringValue=itsnotblabla&ID=332287”

$URL_changeasset = ‘UiPath

$response_changeasset = Invoke-RestMethod -Uri $URL_changeasset -Method ‘PUT’ -Headers $headers_changeasset -Body $body_changeasset -ContentType application/json
$response_changeasset | ConvertTo-Html

1 Like

Hey @fabian.molleman

Kindly check the sample below which should help,

Thanks
#nK

1st - always add/list/edit what I you need in your Browser + F12, to pinpoint the proper definition of the body.

Note: the AssetDTO differs a little bit depending on what ‘Type’ of asset you are adding. For Credentials I only add a stub. My DevOps team later edit this Asset from Orch and adds the actual password.

I stumble on the [Int] value for CredentialStoreId (yes needs to be an int i json, not String)

In PowerShell I use a Switch statement + a proper Object ($json) in the body.

If you´re adding a Credential assets, you will need to specify the CredentialStoreId, hence this first extra function:

 function uiPath_odata_GetCredentialIdByStoreType
{
 Param
 (
 $Type
 )

$Method = "GET"
$uri = "$global:uiPathBaseUrl/odata/CredentialStores?`$filter=Type eq '$Type'"

$headers = @{Authorization = "Bearer $global:bearer_token"}
$httpsResult = Invoke-WebRequest -Uri $uri -Headers $headers -Method 
$Method -ContentType "application/json" -TimeoutSec 180 -ErrorAction:Stop

$resultObject = $httpsResult.Content | ConvertFrom-Json
$resultObject.value.Id
}

and then actual AddAsset

function uiPath_odata_AssetAdd {
param(
$AssetName,
$AssetType,
$AssetValue,
$AssetDescription,
$OrganizationUnitId
)

switch -Exact ($AssetType) {
Integer {
$json = @"
{
	"IntValue": $AssetValue,
	"ValueType": "$AssetType",
	"Name": "$AssetName",
	"Description": "$AssetDescription",
	"ValueScope": "Global",
	"HasDefaultValue": true,
	"CanBeDeleted": true
}
"@
}
Credential 
{
$CredentialStoreId = uiPath_odata_GetCredentialIdByStoreType -Type 'Database'
$AssetValue = $AssetValue.Replace('\','\\')
$json = @"
{
	"ValueType": "$AssetType",
	"CredentialUsername": "$AssetValue",
	"CredentialPassword": "MyDummyPassword",
	"CredentialStoreId": $CredentialStoreId,
	"Name": "$AssetName",
	"Description": "$AssetDescription",
	"ValueScope": "Global",
	"HasDefaultValue": true,
	"CanBeDeleted": true
}
"@
}
Bool 
{
$json = @"
{
	"BoolValue": "$AssetValue",
	"ValueType": "$AssetType",
	"Name": "$AssetName",
	"Description": "$AssetDescription",
	"ValueScope": "Global",
	"HasDefaultValue": true,
	"CanBeDeleted": true
}
"@
}
WindowsCredential {
throw "Asset not implemented yet"
}
KeyValueList {
throw "Asset not implemented yet"
}
##the Switch 'Else' (covers: DBConnectionString, HttpConnectionString, Text
default {
$json = @"
{
	"StringValue": "$AssetValue",
	"ValueType": "$AssetType",
	"Name": "$AssetName",
	"Description": "$AssetDescription",
	"ValueScope": "Global",
	"HasDefaultValue": true,
	"CanBeDeleted": true
}
"@
}

}
$Method = 'POST'

$uri = "$global:uiPathBaseUrl/odata/Assets"
$headers = @{}
$headers.Add('Authorization',"Bearer $global:bearer_token")
$headers.Add('X-UIPATH-OrganizationUnitId',$OrganizationUnitId)

$httpsResult = Invoke-WebRequest -Uri $uri -Headers $headers -Method $Method -Body $json -ContentType "application/json" -UseBasicParsing -TimeoutSec 180 -ErrorAction:Stop

if ($httpsResult.StatusCode -ne 201) {
    $myerror = "uiPath_odata_AssetAdd failed with Status code: $httpsResult.StatusCode"
    throw $myerror
}

$resultObject = $httpsResult.Content | ConvertFrom-Json
$resultObject
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.