Jump to content

Using the API to build a Repetitive Folder structure for an MSP type business


Recommended Posts


If you are an MSP, typically you would like to have a folder and Password Lost structure that is exactly the same for all customers.  This Forum post describes how you can achieve this using the API, which is much quicker that cloning a folder one at a time.  This will also set the Advanced Permission model which means all Folders and Password Lists will have the same permissions set from the top level down, so you must be running Passwordstate 9 for this to work.



First, manually create a Folder, call it anything you like, and set the Advanced Permission Model.  The create as many password lists as you like beneath this Folder.  Take note of the Password List IDs for each List you have created:


Step 1:



Step 2:

Take note of your System Wide API Key, generate one if needed. This can be achieved under Administration -> System Settings-> API tab.


Step 3:

Create a csv file with your customer names, Save this to disk.  Ensure formatting is like this screenshot below:



Step 4: 

It's always a good idea to back up your database just prior to doing any operation like this.  This way, you can quickly restore your database if something doesn't work out as you expect.  This forum post explains how to quickly backup, and restore your database if needed: https://forums.clickstudios.com.au/topic/2480-sql-script-to-quickly-backup-and-restore-passwordstate-database/



Running The Script:

Copy the contents below and paste it into a Powershell ISE shell. Change the top 4 lines wheer it is bold to suit your Passwordstate environment, and run the script:



$PasswordstateURL = "https://Passwordstate.demo.com"
$SystemWideAPIKey = "6ab8dc9437f532eeb36d2f54c38a7948"
$Customers = Import-csv -Path "c:\data\local scripts\customers.csv"
$PasswordListIDs = @('7429','7430','7431')


#No Need to Modify these
$FolderAPIURL = "$PasswordstateURL/api/folders"
$PasswordListAPIURL = "$PasswordstateURL/api/passwordlists"
$Permissions = Get-Random $PasswordListIDs


#Loop through all customers in CSV file and create Folder for each, then create all Password Lists nested beneath that Folder.
foreach ($Customer in $Customers)
#Set the customer name to a variable
$CustomerName = $Customer.customername

#Create the Folder in the root of Passwords Home. Set to Advanced and copy permissions from a nested Password List
$jsonString = '
        "FolderName":"' + $CustomerName + '",
        "CopyPermissionsFromPasswordListID":"' + $Permissions + '",
        "APIKey":"' + $SystemWideAPIKey + '"
$result = Invoke-Restmethod -Method POST -Uri $FolderAPIURL -ContentType "application/json" -Body $jsonString
$FolderID = $result.FolderID


#Now loop through $PasswordListIDs array and create a password List for each. First, we search to obtain the Name of the List
foreach ($ID in $PasswordListIDs)
$URL = "$PasswordListAPIURL/$ID"
$Search = Invoke-Restmethod -Method GET -Uri $URL -Header @{ "APIKey" = $SystemWideAPIKey }  
$PasswordListName = $Search.PasswordList


#Now we have the name, begin creating lists
$jsonString = '
        "PasswordList":"' + $PasswordListName + '",
        "CopySettingsFromPasswordListID":"' + $ID + '",
        "NestUnderFolderID":"' + $FolderID + '",
        "APIKey":"' + $SystemWideAPIKey + '"

    Write-Host $jsonString
$result2 = Invoke-Restmethod -Method POST -Uri $PasswordListAPIURL -ContentType "application/json" -Body $jsonString




If the script runs without errors, you should now see a new folder for each customer, along with a mirror of all the Password Lists that you created:








Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...