Wednesday, October 24, 2018

OAC - PowerShell RestAPI to Upload files from On-Prem to OAC

Today, we are going to see a function which uploads files / data from your On-Prem Server to OAC.

Read my prior blog so that you get the basics on authenticating to OAC and getting the Object handle to invoke the rest commands.

Pre-requisites:

- Create a folder on your local server - D:\Up
- Create a file "MyUploadFile.txt"  within the folder created above. You can create any number of files. 
- Ensure you are running the PowerShell version 4 or above.

Without delay, I'll straight away introduce the code (which has explicit comments on each step to understand it better).

PowerShell Code:

Function UploadingFiles {  

#Parameters passed to the above function
param( [string] $dirToUpload, [string] $upURL )

# This Steps constructs your Rest API Base URL
$baseURL = "http://<your OAC URL>/essbase/rest/v1/" 

#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)"} 

# Construct your upload URL
$uploadURL = $baseURL + $upURL

# Files in the folder which needs to be uploaded
$filesTobeUploaded = Get-ChildItem $dirToUpload 

# Loop through the folder and upload the files to OAC
ForEach($file in $filesTobeUploaded) { 
     $restURL = $uploadURL + $file 
     $restResponse = Invoke-RestMethod -Uri $restURL -Method Put -Header $headers -ContentType "application/octet-stream" -OutFile "$file" 



Use the below command to call the function written above

#Function Call
UploadingFiles -dirToUpload "D:\Up\" -upURL "files/shared/" 

Please try this out and provide your feedback on the comments section.

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!