Tuesday, October 16, 2018

OAC Utility for RestAPI and PowerShell

I've been playing around with RestAPI's through PowerShell, creating various functions for Uploading, Downloading, Listing, Copying, Moving and Deleting files from an OAC folder.

I'm giving you a sneak peek into one of the function, which can help enhance your automation.

Here below is a snippet of code which helps you copy files from one location to another within OAC using PowerShell.

The below code has been Tested and its working fine.

The Invoke-RestMethod Powershell command does the work of connecting to OAC and performing the requested operation.

Step 1: Logging in to OAC - Replace the $username and $password with your specifics.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $password)
$EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization)
$headers = @{"Authorization"="Basic $($EncodedPassword)"}

Step 2: Data Copy happens here!

# Data Copy on OAC using Powershell - Begin
$restURL = "https://<OAC DOMAIN ID>/essbase/rest/v1/files/actions/copy?overwrite=true"

# Creating an Empty object
$body = @{}

# Adding the destination where the file should land
# "$ToPath" - Replace this with your specific path
$body.add("to","$ToPath")

#Source Location from which the file has to be copied
# "$FromPath" - Replace this with your specific path
$body.add("from","$FromPath")

# Converting the object to JSON format
$body = $body | ConvertTo-Json

#Copy it now!
$restResponse = Invoke-RestMethod -Uri $restURL -Method Post -Header $headers -ContentType "application/json" -Body $body

# Data Copy on OAC using Powershell - End

Code:

Throw the below Code Snippet into a file and Save it as a PowerShell (.ps1) application

CopyUtility.ps1

# Login to OAC
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $password)
$EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization)
$headers = @{"Authorization"="Basic $($EncodedPassword)"}

# Data Copy on OAC using Powershell - Begin
$restURL = "https://<OAC DOMAIN ID>/essbase/rest/v1/files/actions/copy?overwrite=true"
$body = @{}
$body.add("to","$ToPath")
$body.add("from","$FromPath")
$body = $body | ConvertTo-Json
$restResponse = Invoke-RestMethod -Uri $restURL -Method Post -Header $headers -ContentType "application/json" -Body $body
# Data Copy on OAC using Powershell - End

Step 3: That's it!!!

See you soon with my next post! 

No comments:

Post a Comment