PowerShell script to Add Accounts via the API and experiencing some

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

Welcome Idira to LIVEcommunity!

Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.

Meet Idira

PowerShell script to Add Accounts via the API and experiencing some

L4 Transporter

To speed up onboarding we are looking to the create a PowerShell script and have started with the basic shell below and using a csv file to read data in from to deed the script.:

 

# This section will get the token required for all future API calls, it will prompt the user for a username and password

 

$logonurl= "https://<PVWA ADDRESS>/PasswordVault/API/auth/Cyberark/Logon/"

$username = Read-Host 'Username'

$pass = Read-Host 'Password' -AsSecureString

$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)

$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)

$formData = @{

  'Username' = $username

  'Password' = $password}

$formUrlEncoded = ($formData.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join '&'

$token = Invoke-RestMethod -ContentType "application/x-www-form-urlencoded" -Uri $logonurl -Method Post -Body $formUrlEncoded

 

# This section will create accounts based upon the content input into the associated AddAccounts.csv

 

Import-CSV AddAccounts.csv | Foreach-Object{

$safename = $_.safename

$username = $_.paccount

$address = $_.address

$name = $_.name

$platformID = $_.platform

# $employee = $_.employee

$logondomain = $_.logonto

$accountData = @{

 name = "$name"

 address = "$address"

 userName = "$username"

 platformId = "$platformID"

 safeName = "$safename"

 secretType = "Password"

 secret = ""

 platformAccountProperties = @{

 # Employee = "$employee";

 LogonDomain = $logondomain

 }

 secretManagement = @{

 automaticManagementEnabled = "false";

 manualManagementReason = ""

 }

 remoteMachinesAccess = @{

 remoteMachines = "";

 accessRestrictedToRemoteMachines = "false"

 }

}

 

$accountjson = $accountData | ConvertTo-Json -Depth 99

$accountsurl= "https://<PVWA ADDRESS>/PasswordVault/API/Accounts/"

$accounts = Invoke-RestMethod -ContentType "application/json" -Uri $accountsurl -Method Post -H @{"Authorization" = "$token"} -B $accountjson}

 

"Logging Off"

 

# This section will log the current user off the Vault

 

$logoffurl = "https://<PVWA ADDRESS>/PasswordVault/API/Auth/Logoff/"

$logoff = Invoke-RestMethod -ContentType "application/json" -Uri $logoffurl -Method Post -H @{"Authorization" = "$token"}

 

"Script Finished"

 

We are currently experiencing the error below and wondered if we were missing somethign in the basic script above?

 

Username: Admin1

Invoke-RestMethod : {"Details":[{"ErrorCode":"PASWS011E","ErrorMessage":"Missing mandatory parameter 

[platformId]","ParameterName":"platformId"}],"ErrorCode":"PASWS167E","ErrorMessage":"There are some invalid parameters"}

At C:\scripts\AddAccountsStc.ps1:49 char:13

+ $accounts = Invoke-RestMethod -ContentType "application/json" -Uri $a ...

+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   + CategoryInfo         : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

   + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Logging Off

Script Finished

 

We have tried with a blank and a known PlatformId and same result.

 

Thanks in advance for any suggestions.,

 

 

4 REPLIES 4

L4 Transporter

Hi!

 

You can try in this way without quotes :

 

Remove the single quotes around the variable names:

powershell

$accountData = @{

name = $name # ← No quotes

address = $address

userName = $username

platformId = $platformID # ← This will now pass the actual value

safeName = $safename

secretType = 'Password' # ← Keep quotes for literal strings

secret = $secret # Assuming you'll add this

platformAccountProperties = @{

Employee = $employee

LogonDomain = $logondomain

}

secretManagement = @{

automaticManagementEnabled = 'false'

manualManagementReason = ''

}

remoteMachinesAccess = @{

remoteMachines = ""

accessRestrictedToRemoteMachines = 'false'

}

}

 

L4 Transporter

Hi, thanks for the reply we did try the suggestion and error remains the same:

 

Username: Admin1

 

Invoke-RestMethod : {"Details":[{"ErrorCode":"PASWS011E","ErrorMessage":"Missing mandatory parameter 

 

[platformId]","ParameterName":"platformId"}],"ErrorCode":"PASWS167E","ErrorMessage":"There are some invalid parameters"}

 

At C:\scripts\AddAccountsStc.ps1:49 char:13

 

+ $accounts = Invoke-RestMethod -ContentType "application/json" -Uri $a ...

 

+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

 

    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

Logging Off

 

Script Finished

 

When I used the Password Upload Utility, the platform field was PolicyID. According to this link https://docs.cyberark.com/privilege-cloud-standard/latest/en/content/pasref/automatic%20password%20m..., the PolicyID is 'the unique name/ID of the platform. If you modify your platformID for your script this can go like as PasswordUploadUtility

L4 Transporter

Ok, so progress the issue for us was the platformId within the CSV feed data we had in csv Windows Domain a platformId info which fails and name with no spaces e.g. WinDomain and script is fine and adds an account.

 

Clue on issue came from the API docs

PlatformId

The platform assigned to this account.

Type: String

Mandatory: Yes

Valid value: Platform ID, for example, WinServerLocal

This parameter is case sensitive.

 

  • 111 Views
  • 4 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!