Rest API - Powershell

Post Reply
sven912
Posts: 5
Joined: Wed Mar 04, 2015 3:29 pm
OLAP Product: TM1 \ Planning Analytics
Version: PAL + PAW 2.xx TM110.2.xxx
Excel Version: 2016+

Rest API - Powershell

Post by sven912 »

Can anyone help with the syntax for adding parameters to a PS script to execute a process. Works fine with no parameters.

$TM1Run = "Processes('zRestAPITest')/tm1.Execute"

But how to I add say pYear = '2022' and pMonth = 'Feb'

Thanks.
ascheevel
Community Contributor
Posts: 286
Joined: Fri Feb 15, 2013 5:49 pm
OLAP Product: TM1
Version: PA 2.0.9.1
Excel Version: 365
Location: Minneapolis, USA

Re: Rest API - Powershell

Post by ascheevel »

Parameters should be specified as a json object passed to the -body argument of the Invoke-RestMethod POST.

Code: Select all

{
	"Parameters": [{"Name":"pYear", "Value":"2022"}, {"Name":"pMonth", "Value":"Feb"}]
}


edit: grammar
sven912
Posts: 5
Joined: Wed Mar 04, 2015 3:29 pm
OLAP Product: TM1 \ Planning Analytics
Version: PAL + PAW 2.xx TM110.2.xxx
Excel Version: 2016+

Re: Rest API - Powershell

Post by sven912 »

Thanks for that - looks like I need to study more on this - not sure how I get the parameters into the lines of code ? Do I need to add -Parameters $Parmeters or similar into this code.

$TM1RESTAPI = Invoke-RestMethod -Method Post -uri $TM1InstanceURL -WebSession $session -Headers $headers -ContentType "application/json"

Does anyone have a good link that helps with learning the Restapi syntax for PS ?
sven912
Posts: 5
Joined: Wed Mar 04, 2015 3:29 pm
OLAP Product: TM1 \ Planning Analytics
Version: PAL + PAW 2.xx TM110.2.xxx
Excel Version: 2016+

Re: Rest API - Powershell

Post by sven912 »

I'm very much a novice on this! This is what I have - I played around with the parameters entry until the syntax was accepted but still unsure how to get them to be triggered. Can anyone help me understand what I need to do. Thanks.

Code: Select all

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

$headers = @{"Authorization" = 'CAMNamespace xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

$Parameters = @{"Name"='pSleepS';"Value"='5000'}

$TM1Run = "Processes('zRestAPITest')/tm1.Execute"

$TM1InstanceURL = 'https://customer.planning-analytics.ibmcloud.com/tm1/api/Finance/api/v1/' + $TM1Run 

$TM1RESTAPI = Invoke-RestMethod -Method Post -uri $TM1InstanceURL -WebSession $session -Headers $headers -ContentType "application/json"
ascheevel
Community Contributor
Posts: 286
Joined: Fri Feb 15, 2013 5:49 pm
OLAP Product: TM1
Version: PA 2.0.9.1
Excel Version: 365
Location: Minneapolis, USA

Re: Rest API - Powershell

Post by ascheevel »

I modified your code below with following updates:
*added Content-Type to header (not necessary, but reduces clutter in your rest call
*reconfigured $Parameters variable as a JSON object
*added $Parameters to "-Body" argument of rest call (this was a specified requirement in my initial response)

Updated PowerShell from your last:

Code: Select all

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

$headers = @{"Authorization" = 'CAMNamespace xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; "Content-Type" = "application/json"}
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession

$Parameters = @{"Parameters" = @(@{"Name"="pSleepS";"Value"="5000"})} | ConvertTo-JSON

$TM1Run = "Processes('zRestAPITest')/tm1.Execute"

$TM1InstanceURL = 'https://customer.planning-analytics.ibmcloud.com/tm1/api/Finance/api/v1/' + $TM1Run 

$TM1RESTAPI = Invoke-RestMethod -Method Post -uri $TM1InstanceURL -WebSession $session -Headers $headers -Body $Parameters
Here is what the powershell $Parameters statement would look like with the the original parameters you had specified; for some reason in your latest response, you're using pSleepS as a parameter when you originally specified a process with parameters pYear and pMonth.

Code: Select all

$Parameters = @{"Parameters"=@(@{"Name"="pYear";"Value"="2022"};@{"Name"="pMonth";"Value"="Feb"})} | ConvertTo-JSON

edit: I'm not an PowerShell expert, so there may be better ways. One example: you don't need to use the ConverTo-JSON if you write the json correctly to begin with:

Code: Select all

$Parameters = @"
{"Parameters":[{"Name":"pYear","Value":"2022"},{"Name":"pMonth","Value":"Feb"}]}
"@

sven912
Posts: 5
Joined: Wed Mar 04, 2015 3:29 pm
OLAP Product: TM1 \ Planning Analytics
Version: PAL + PAW 2.xx TM110.2.xxx
Excel Version: 2016+

Re: Rest API - Powershell

Post by sven912 »

Thank you so much that worked perfectly - I will now analyse the code so hopefully I can start to get my head around how all this works !
User avatar
macsir
MVP
Posts: 782
Joined: Wed May 30, 2012 6:50 am
OLAP Product: TM1
Version: PAL 2.0.9
Excel Version: Office 365
Contact:

Re: Rest API - Powershell

Post by macsir »

I haven given up Powershell as it is not pretty easy to utilize REST API. Better to choose TM1py. All functions you need from PS are available in Python and even more!
In TM1,the answer is always yes though sometimes with a but....
http://tm1sir.blogspot.com.au/
Post Reply