Powershell – Cherwell Login Using Rest API and Windows Authentication

This is  a powershell function to enable you to login to Cherwell using the Rest API with transparent windows authentication (the credentials you are running the powershell session with). But there is some work you need to do first. 

See http://britv8.com/using-powershell-to-access-the-rest-api-in-cherwell/ for how to set things up first in Cherwell

function Post-CherwellWindowsAuth{
    <#
    .SYNOPSIS
        Logs into Cherwell using Transparent Windows Authentication
    .PARAMETER 
    Computername = Webserver you are connecting to 
    apiKey = An API client key is required.Your API client id is setup like this: go to CSM Admin (orange pill) > login > click sercurity > click 'Edit REST API client settings > click 'create new client' = green button (+) > give it a name and save. Your client key is auto-generated.
    .INPUTS
        None
    .OUTPUTS
         System.Management.Automation.PSCustomObject
    .EXAMPLE
        Post-CherwellWindowsAuth
        You will be prompted for all parameters
    .EXAMPLE
        Post-CherwellWindowsAuth -Computername $Computername -apiKey $apiKey
    .EXAMPLE
        Post-CherwellWindowsAuth -Computername Server1 -apiKey "31933b89-211b-39b5-81d4-e0bbe7bafd15a"
    .NOTES
        Author:             BritV8.com
        Date:               12/06/2018
        Based on https://help.cherwell.com/bundle/cherwell_rest_api_930_en_help_only/page/oxy_ex-1/content/system_administration/rest_api/csm_rest_powershell_create_user.html

    #>
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true)]
        [String]$Computername,

        [Parameter(Mandatory=$true)]
        [String]$apiKey
    )

# Set server login variables
$baseUri = "http://${serverName}/CherwellAPI/"

# Get an access token
$tokenUri = $baseUri + "token"
$authMode = "Windows"
$tokenRequestBody =
@{
    "Accept" = "application/json"
    "grant_type" = "password"
    "client_id" = $apiKey


}


Invoke-RestMethod -Method POST -Uri "${tokenUri}?auth_mode=${authMode}&api_key=${apiKey}" -Body $tokenRequestBody -UseDefaultCredentials
} 


 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.