PowerShell Rest Call - Help Needed

I am attempting to make a rest call to a baq using PowerShell. Current state credit to the below post for getting me to the point I’m at now:

Current code state:

#Epicor username
$user = “XXX”
#Epicor password
$pass = “XXX”

#BAQ End point URL
$uri = “https://gccdtapp02.epicorsaas.com/saasXXXX/api/v1/BaqSvc/BAQ/”

#Create encoded password
$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force

#Create Credential
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)

#Rest Call
$data = Invoke-RestMethod $uri -Method Get -Credential $cred -ContentType “Application/Json”

#Dump results as CSV
$data.value | Export-Csv -Path ‘C:\Users\XXXXX\Desktop\TEST\test.csv’ -NoTypeInformation

Right now I get no errors. However, the CSV that is created afterword contains no data. I am unsure if I am missing a step to get the data into the CSV properly.

After getting the program to write to PowerShell correctly, I would like some pointers on how to appoint each column from the baq to a variable within PowerShell.

Thanks for the help!

Solved the no returns issue, the account used to access rest has to have access to the companies you are trying to get returns from.

Still need ideas on how to assign each column from the baq return to variables.

1 Like

@Banderson, @josecgomez

Hate to bother you gentlemen, but I’m at a loss.

I have gotten to a point where the code does the rest call successfully and writes all the data to a CSV file that is parsed. What I need to do now is transition the data I am collecting, in blue, into the post method, in green. I have not found a way to do this successfully code and snippets below:

 #Epicor username
    $user = “***”
    #Epicor password
    $pass = “***”

    #BAQ End point URL
    $uri = “https://gccdtapp02.epicorsaas.com/****/api/v1/BaqSvc/PriorityDispatchMaster/”

    #Create encoded password
    $secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force

    #Create Credential
    $cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)

    #Rest Call
    $data = Invoke-RestMethod $uri -Method Get -Credential $cred -ContentType “Application/Json”

    $data.value | Get-Member 

    Write-Output "TEST" 

    #Write-Output $data.value
    Write-Output $data.value.Count

    #Dump results as CSV
    $data.value | Export-Csv -Path ‘C:\Users\*****\Desktop\TEST\test.csv’ -NoTypeInformation

    $xd = $data.value | select 1 JobHead_PartNum

    $payload = @()
    $arrayArray= @()
    $endpoint = "*****"

    $length = $data.value.Count

    #For($i=0; $i -lt $length;$i++)
    #{
         $data = @{
        "ResourceGroup_JCDept" = "$xd"
        "ResourceTimeUsed1_ResourceGrpID" ="AAAAA555555"
        "ResourceTimeUsed1_ResourceID" ="AAAAA555555"
        "Calculated_RegionCode" ="AAAAA555555"
        "JobOpDtl_Company01" ="AAAAA555555"
        "JobOpDtl_JobNum01" ="AAAAA555555"
        "JobHead_PartNum" ="AAAAA555555"
        "JobHead_RevisionNum" ="AAAAA555555"
        "Calculated_Op" ="AAAAA555555"
        "JobOper_OpCode" ="AAAAA555555"
        "JobHead_ProdQty" =98.6
        "JobOper_QtyCompleted" =98.6
        "Calculated_piecesRemaining" =98.6
        "JobOper_EstSetHours" =98.6
        "JobOper_ActSetupHours" =98.6
        "JobOper_EstProdHours" =98.6
        "JobOper_ActProdHours" =98.6
        "JobHead_ReqDueDate" ="2021-05-24T19:07:03.495Z"
        "JobOper_DueDate" ="2021-05-24T19:07:03.495Z"
        "JobOper_StartDate" ="2021-05-24T19:07:03.495Z"
        "JobHead_SchedCode" ="AAAAA555555"
        "Calculated_PriorOp" ="AAAAA555555"
        "ResourceTimeUsed4_ResourceGrpID" ="AAAAA555555"
        "Calculated_NextOp" ="AAAAA555555"
        "ResourceTimeUsed5_ResourceGrpID" ="AAAAA555555"
        "Customer_Name" ="AAAAA555555"
        "Vendor_Name" ="AAAAA555555"
        "Calculated_DaysInDepartment" =98.6
        }
        $arrayArray +=,$data
        Write-Output $arrayArray
    #}
    Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($ArrayArray))

My powershell skills are rusty, I’m guessing you want to loop through the value array something like this might help

1 Like

The iteration will be part of it for sure. The main thing I’m trying to do right now is get the first entry from the Get method to then be posted to my end point. The issue is I have no clue how to take the data from the get and apply it to the post requirements.

For instance in my get method I have a JobHead_PartNumber and in my post method I have a JobHead_PartNumber. I would like to post the first entry from the get method but cant get my data to tie in.

If you could explain it easier in c# I can take it from there. Also thanks for the quick response!

Did you try making the rest calls in post man? You can get the call information from swagger, use that URL to build your call in post man, and postman has a tab that will give you code for a bunch of different languages. Including Powershell.

1 Like

Out of curiosity, what are you trying to do? If you’re making a post call with the data you receive from the BAQ, couldn’t you just make the BAQ a UBAQ and do what you need to do there? Why pull it out, just to shove it back in again?

**Edit… It’s a SAAS thing isn’t it?

Oh, so the goal of this project is to create a streaming dataset with the results of that baq. Then make it all pretty within PowerBi and nest it within our SharePoint for distribution.

As a follow-up question I like that it can create the get code, but how do I then post that code to my endpoint?

1 Like

You can make get, post, patch… all kinds of rest types. I don’t understand your question.

Have you checked this out?

Solved: Re: Using a REST API as a data source - Microsoft Power BI Community

@Banderson,

So I have a specified endpoint that powerbi wants the data to go to with the post method. However, when I run the post for the endpoint I am greeted with the following error:

The original question had to do with whether or not Postman could handle endpoints. Now looking back at it, my question was poorly written, and kind of a dumb question.

@utaylor,

We had used the “Get Odata Feed” option to get our data out of Epicor using baq calls and put them into our dataset. The issue is that by creating a dataset like that you are limited to 8 refreshes per day on your datasets.

Our goal in writing this out using the rest API is to create a “living” dataset that will refresh whenever new data is presented to it. Thanks for the reply though!

1 Like

Right on, I didn’t know about that limitation!

@Banderson,

I think I’m, getting somewhere with what Jose posted earlier and troubleshooting with Postman. I solid returns coming from our baq and I get solid posts from the standard setup given to me by powerbi. My question now is whether or not I can create a Foreach value loop at the tail end of the convertto-json statement and whether or not that will give me returns for all of my entries. Have you done something like that before?

I haven’t done any of that before. I haven’t used PowerBI and I have no experience with PowerShell. Sorry.

There are so many ways to come at this.

Alternatively you could use @josecgomez rest nuget in visual studio and make a rest call to your baq using c# and then write a foreach loop that runs through the BAQ response and outputs it to a .csv file or xml or whatever. You could pack that up into an executable and then use windows scheduler to run the exe every 15 minutes or whatever to update the file.

Not sure if you are more comfortable with powershell or something in visual studio.

But I haven’t ever done much of any of this. I did use powershell to specify a baq to DMT though which was cool. I am pretty sure there is an example of that in the ice tools book. And I think you had to output the baq to a file. However, this was not using rest.

3 Likes

Sorry Dylan, I’m a bit dense. Can you explain the business outcome without any reference to BAQ, REST, DataSet, PowerBI, or Postman? I hear how you want to do something but I’m still not clear on what the user’s desired outcome is.

Thanks!

1 Like

@Mark_Wonsil,

Sure!

The desired outcome of this project is to display our priority dispatch report and have it automatically refresh every five or so minutes. This will allow our team leads to prioritize jobs as needed and will keep them up to date without having to stop and print off another priority dispatch report. We want to have this displayed in each of our departments, be accessible for our corporate offices, and down the road be mounted at each machine. I have done something similar in the past but with limitations on the amount of times the data can be scheduled to be refreshed. To overcome these limitations I have been working on the project outlined above.

Love it. Great idea!

UPDATE:
I now have my data mapped the way I want it. Assigning variables from data was a lot easier than I was making it.

NEW ISSUE:
The data that goes into the data source looks akin to this and I’m not entirely sure how to fix it just yet:

image

@Banderson,

Postman question for you. I have a column that comes up in my response that I do not want to show up. In this case, it’s called “RowIdent”. I would like to filter out this column from the response and allow all my other items through.