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
}