Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
scott_stefanich
Active Participant

A friend at Microsoft made a simple yet profound observation while discussing software integration: Sometimes, we're too close to our products.

To broaden my horizons, I took a break from SAP products as a professional and took a look at Microsoft products as an enthusiast. Having enjoyed SAP's  openSAP online courses, Microsoft Virtual Academy seemed like a good place to start.

One MVA course which appeared applicable across a variety of topics was Getting Started with PowerShell 3.0. An instructor referred to an idea loosely summarized as 'script everything'. Trusting accomplished professionals, I took the advice at face value and began making simple scripts.

Script

As hinted by the blog title, below is a minimalist script to consume and use a SAP Gateway Service in Windows Powershell. A request's response (in this case, a collection of business partners) can be handled as an object in PowerShell.


$uri = “https://sapes1.sapdevcenter.com/sap/opu/odata/sap/ZGWSAMPLE_SRV/BusinessPartnerCollection"
$body = @{'$format'='json'}
$contentType = "application/json”
$credential = Get-Credential -Message "SAP Gateway Demo System, ES1"
$method = "GET"
$collection = Invoke-RestMethod -Uri $uri –Body $body -ContentType $contentType -Credential $credential –Method $method
$collection.d.results | ConvertTo-Csv | out-file c:\temp\Partners.csv







Help

Unless you are into memorization, a good first step in PowerShell ventures tends to be using the help system. Recalling a discussion about RESTful web services, a quick search identified the Invoke-RestMethod cmdlet.

The help description referred to REST web services, inspiring hope for use with SAP Gateway services, and included useful resources, e.g. examples. To open help in a nifty window, get a cmdlet's help using the -ShowWindow parameter.


Get-Help Invoke-RestMethod -ShowWindow







REST Method

Having identified the cmdlet and perused its help, I proceeded with parameters and arguments.

URI

According to help, the -Uri parameter accepts a string. I used the URI for a business partner collection in a SAP Gateway Demo System service. The collection contains what it sounds like, business partners. Below is the collection as viewed in a web browser,

Credential

Even with a dim awareness of security, a plain text password sounds like a bad idea. The PowerShell Get-Credential cmdlet prompts for a user and a password which is managed as a secure string.

As a main purpose of PowerShell is to automate tasks, encrypting and automating credentials is described in Secure Password with PowerShell: Encrypting Credentials.

Do something?

Remarkably, I retrieved the business partner collection without a goal or purpose beyond learning. I retroactively added a business scenario: Return data to a format whence it came, a text file.

The collection results are a system object,

I piped the results to a locally-saved CSV file, a historically popular format. The CSV can then be imported into, you guessed it, Microsoft Excel.

Conclusion

Consuming a SAP Gateway service from PowerShell was relatively painless.

PowerShell handles data as objects. Objects can be manipulated and used as input to subsequent commands in the pipeline.

Looking forward, various use cases present themselves. Data could be queried or loaded in bulk. Data objects could be used for other services, SAP, Microsoft, or beyond.

--- Scott

1 Comment