param( [Parameter(Mandatory = $true, HelpMessage="CIS userid to authenticate with")] [string] $cisuserid, [Parameter(Mandatory = $true, HelpMessage="CIS password to authenticate with")] [string] $cispassword, [Parameter(Mandatory = $true, HelpMessage="Resource name to access (eg. customer, state, serviceorder, etc)")] [string] $resource ) # Retrieves some sample information from a Advanced REST Web API URL. # Some example resource names are "state", "account", "customer", "customeraccount", "serviceorder" #--- Start of invalid SSL certificate hack # This stuff here is just to make it work with a non-valid SSL certificate, like self-signed ones. # Would not be needed on a "real" production site with a valid, professional SSL certificate add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy #--- End of invalid SSL certificate hack # Advanced REST Web API URL to call $restApiRoot = 'https://restdev.myresturl.com/data/' $restApiResourceName = $resource $restApiUrl = $restApiRoot + $restApiResourceName # create HTTP Basic Auth header using CIS userid and password $authHeader = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($cisuserid + ':' + $cispassword)) # create HTTP headers dictionary, and include additional headers to indicate type of request and type of response desired $headers = @{'Authorization' = $authHeader; 'Content-Type' = 'application/json'; 'Accept' = 'application/json'} # we will measure the call time too, so get the time now before the call $starttime = get-date write-host "Calling REST URL: $restApiUrl" $result = $null # call REST API with GET verb try { $result = Invoke-RestMethod -Method Get -Uri $restApiUrl -Headers $headers } catch { write-host "Invoke-RestMethod failed with message: '" $_ "'" } # get time right after call so we can determine how long call took $endtime = get-date if ($result -ne $null) { # parse the response contents as needed - it is already converted to a PowerShell object! # List resources always contain the list contents inside a property with the same name as the list resource you requested. # Additional list level properties TotalItems, PageSize, TotalPages, CurrentPage are also returned. $timetaken = $endtime - $starttime write-host "Request took $($timetaken.TotalMilliseconds) ms" #Get number of items $count = $result._embedded.$restApiResourceName.Count write-host "Request returned $count '$restApiResourceName' items" write-host "TotalItems = " $result.TotalItems write-host "PageSize = " $result.PageSize write-host "TotalPages = " $result.TotalPages write-host "CurrentPage = " $result.CurrentPage # get first item in list of result items and display the state code and description $item1 = $result._embedded.$restApiResourceName[0] write-host "Details of first $restApiResourceName" $item1 }