Skip to content

Powershell – GET ORGANIZATION PRICES

This script uses PowerShell to fetch the price list for every organization and compare the sales price to the product recommended price, and write all products with a changed sales price to a CSV file.

Edit the variables under “Define constants” and run the script with Microsoft PowerShell.

This is just an example with limited error handling and should not be used for production.

# Define constants
RESELLER_ID = '11111111-2222-3333-4444-555555555555' # Replace with you Broker ID
CLIENT_ID = 'ro.customer.client'
API_SECRET = 'MySecret' # Replace with your API Secret
USERNAME = 'username@broker.com' # Replace with your username
PASSWORD = 'MyPassword' # Replace with your password
$BASE_NAME = 'https://api.cloudmore.com'
$BASE_URL = "$BASE_NAME/api"
$CSV_FILE = 'discrepancy_report.csv'
# Create a Base64 encoded header for auth
$Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($CLIENT_ID):$($API_SECRET)"))
# Get the Bearer Token
$TokenBody = @{
    grant_type = 'password'
    username   = $USERNAME
    password   = $PASSWORD
    scope      = 'api'
}
$Token = Invoke-RestMethod -Uri "$BASE_NAME/connect/token" -Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} -Method Post -Body $TokenBody
$headers = @{
    Accept        = 'application/json'
    Authorization = "Bearer $($Token.access_token)"
}
function Get-Organizations {
    Invoke-RestMethod -Uri "$BASE_URL/resellers/$RESELLER_ID/Organizations" -Headers $headers
}
function Get-OrganizationServices {
    param ($OrgID)
    Invoke-RestMethod -Uri "$BASE_URL/resellers/$RESELLER_ID/organizations/$OrgID/services" -Headers $headers
}
function Get-PricesPerService {
    param ($OrgID, $ServiceID)
    Invoke-RestMethod -Uri "$BASE_URL/resellers/$RESELLER_ID/organizations/$OrgID/services/$ServiceID/pricelist" -Headers $headers
}
# Extract data and write discrepancies to CSV
$organizations = Get-Organizations
$totalOrgs = $organizations.Count
$progressCounter = 0
$organizations | ForEach-Object {
    $org = $_
    $progressCounter++
    Write-Progress -PercentComplete (($progressCounter / $totalOrgs) * 100) -Status "Processing $($org.name)" -Activity "Gathering data"
    $services = Get-OrganizationServices -OrgID $org.id
    $services | ForEach-Object {
        $service = $_
        $prices = Get-PricesPerService -OrgID $org.id -ServiceID $service.id
        $prices | Where-Object { $_.salesPrice -ne $_.recommendedPrice } | ForEach-Object {
            $data = [PSCustomObject]@{
                'Org Name'         = $org.name
                'Service'          = $service.name
                'ProductName'      = $_.name
                'ItemCode'         = $_.itemCode
                'RecommendedPrice' = $_.recommendedPrice
                'SalesPrice'       = $_.salesPrice
                'Margin'           = $_.margin
            }
            $data | Export-Csv -Path $CSV_FILE -NoTypeInformation -Append
        }
    }
}
Write-Output "Processing complete."