How to use an SCSP token with PowerShell Web Requests

How to use an SCSP token with PowerShell Web Requests

Content Gateway provides two methods of authentication for web requests, basic and token-based. Both types of authentication can be used with PowerShell.

Basic Authentication

Basic authentication is used in PowerShell as follows:

PS C:\Users\Administrator> Invoke-WebRequest -Uri http://tony.demo.sales.local -Credential csadmin -Method Head

This request performs a head of the domain tony.demo.sales.local using the -Credential csadmin. This prompts for a password after hitting Enter.

Token-based Authentication

While there are ways to pass the password to the command too, it is easier and more secure to use a token.

To do this, use a script like below.

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

$Cookie = New-Object System.Net.Cookie
$Cookie.Name="token"
$Cookie.Value="<the token id>"
$Cookie.Domain="<domain which the token is valid for>"
$session.Cookies.Add($Cookie);

$uri = 'example.domainendpoint'
Invoke-RestMethod $uri -WebSession $session

This method  can be used with Invoke-RestMethod and Invoke-Web requests.

Working like this is useful when making large numbers of queries and then pushing an output, such as when using a metrics query script or posting the contents of a folder.

© DataCore Software Corporation. · https://www.datacore.com · All rights reserved.