Using our API in PowerShell

Over the years PowerShell has become the main scripting language for Windows-based systems. With PowerShell Core it's even possible to automate tasks and manage other platforms like Linux and macOS.

The example below will help you get started using our API in your own scripts.

The script imports folders and their authorizers in Folderfay based on a CSV file.

The CSV file has a pretty simple structure.
In our example we assume the root folders are already defined in the Folderfay back-end.

FolderName;RootFolder;Path;FirstName;LastName;Email
Mazda;\\FileServer\Data\;Clients\Mazda;Keeva;Hull;[email protected]
Mercedes;\\FileServer\Data\;Clients\Mercedes;Marcel;Murray;[email protected]
Tesla;\\FileServer\Data\;Clients\Tesla;Katherine;Good;[email protected]
Ford;\\FileServer\ExtraData\;Clients\Ford;Katherine;Good;[email protected]
########################################################
# Import existing folders in Folderfay using CSV file.
########################################################

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

#
# Variables
#
$importFile = "folders.csv"
$ffToken = ""
$ffTokenLocation = "folderfayToken"
$authToken = @{
    username = 'api-0c53bb62-xxxx-xxxx-xxxx-7be7020e3491'
    password = 'xxxxx'
}

#
# Check API token freshness and renew if needed
#
if(-Not (Test-Path $ffTokenLocation)){ New-Item $ffTokenLocation }
$line = Get-Content -Path $ffTokenLocation
if($null -eq $line -or $line.Contains(" _ ")){
    if($null -eq $line -or $(Get-Date) -gt $line.Split(" _ ")[0]){
        # Request new token
        $uri = "https://api.folderfay.com/v1/authenticate"
        $getToken = Invoke-WebRequest -Uri $uri -Method Post -Body $authToken
        $tokenInfo = (Get-Date).AddDays(1).toString("yyyy/MM/dd HH:mm:ss")
        $tokenInfo += " _ "+$getToken.Content
        $ffToken = $(ConvertFrom-Json $getToken.Content).Token
        Clear-Content $ffTokenLocation
        Add-content -Path $ffTokenLocation -value $tokenInfo
    } else {
        # Token is still valid
        $ffToken = $(ConvertFrom-Json $line.Split(" _ ")[1]).token
    }
} else {
    # Request new token
    $uri = "https://api.folderfay.com/v1/authenticate"
    $getToken = Invoke-WebRequest -Uri $uri -Method Post -Body $authToken
    $tokenInfo = (Get-Date).AddDays(1).toString("yyyy/MM/dd HH:mm:ss")
    $tokenInfo += " _ "+$getToken.Content
    $ffToken = $(ConvertFrom-Json $getToken.Content).Token
    Clear-Content $ffTokenLocation
    Add-content -Path $ffTokenLocation -value $tokenInfo
}

$headers = @{ Authorization = "Bearer "+$ffToken }

# Find out the available root folders so we can convert path to guid
$uri = "https://api.folderfay.com/v1/rootfolders"
$getRootFolders = (Invoke-WebRequest -Uri $uri -Method Get -Headers $headers) | ConvertFrom-Json

#
# Create owners and folders
#
Import-Csv $importFile -Delimiter ";" | ForEach-Object {
    # Check if owner already exists otherwise create person + use guid during folder creation
    $uri = "https://api.folderfay.com/v1/FolderOwners"
    $getOwners = Invoke-WebRequest -Uri $uri -Method Get -Headers $headers
    $ownerExists = $false
    $ownerGuid = ""

    foreach($owner in ($getOwners | ConvertFrom-Json)){
        If($owner.Email.Contains($_.Email)){
            $ownerExists = $true
            $ownerGuid = $owner.folderOwnerGuid
        }
    }
    if(-not $ownerExists){
        $uri = "https://api.folderfay.com/v1/FolderOwners"
        $createOwnerForm = @{}
        $createOwnerForm.Add("FirstName",$_.FirstName)
        $createOwnerForm.Add("LastName",$_.LastName)
        $createOwnerForm.Add("Email",$_.Email)
        $createOwner = Invoke-WebRequest -Uri $uri -Method Post -Form $createOwnerForm -Headers $headers
        if($createOwner.StatusDescription.Contains("OK")){
            $ownerGuid = ($createOwner.Content | ConvertFrom-Json).folderOwnerGuid
        }
    }

    # Convert root folder path to guid
    $rootFolderGuid = ""
    foreach($rootFolder in $getRootFolders){
        if($rootFolder.networkPath.Contains($_.RootFolder)){
            $rootFolderGuid = $rootFolder.rootFolderGuid
        }
    }

    # Create the folder with the owner
    $createFolderForm = @{}
    $createFolderForm.Add("FolderName",$_.FolderName)
    $createFolderForm.Add("RootFolderGuid",$rootFolderGuid)
    $createFolderForm.Add("Path",$_.Path)
    $createFolderForm.Add("FolderOwners",$ownerGuid) #This can also be a list of guids
    $createFolderForm.Add("allowOverrideApproval",$true)
    $createFolderForm.Add("InitializeFolder",$true)

    $uri = "https://api.folderfay.com/v1/folders"
    $createFolder = Invoke-WebRequest -Uri $uri -Method Post -Form $createFolderForm -Headers $headers

    if($createFolder.StatusDescription.Contains("OK")){
        Write-Host "Folder" $_.FolderName "with owner" $_.Email "created!" -ForegroundColor Green
    }
}