1

I'm trying to expose an endpoint using RestPS routes in powershell. I was able to expose it and run a custom PS script when an endpoint (http://localhost:8080/scan) is hit.

How do we pass body through routes & based on that value I need to execute the custom script.

RestPSroutes.json - Example

{
    "RequestType": "GET",
    "RequestURL": "/scan",
    "RequestCommand": "C:/RR/api-compliance.ps1"   }

snippet from the above script (api-compliance.ps1) ;

###############
# Setup Creds #
###############

$userID = "****"
$Pass   = "*****"
#$Creds  = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userID,$Pass
$vCenter = "vCenter01"
$cluster = "Cluster01"
$vmhost = "Host01"
$bl = "Baseline01"

######################
# Connect to vCenter #
######################

Connect-VIServer $VCTRs -user $userID -password $Pass -WarningAction SilentlyContinue -Force




   $baseline = Get-Baseline | ? {$_.name -eq $bl}
 
   $baseline |  Attach-Baseline -Entity $vmhost -Confirm:$false 
Scan-Inventory -Entity $vmhost 

Right now we are hardcoding the below values

  $vCenter = "vCenter01"
    $cluster = "Cluster01"
    $vmhost = "Host01"

But we want these to be passed as a request body. Can someone help?

RCR R
  • 11
  • 1

1 Answers1

0

Simply declare parameters $RequestArgs and $Body in the target script/function, and RestPS will automatically pass the appropriate values along as strings:

param(
  [string]$RequestArgs,
  [string]$Body
)

$RequestArgs.Split('&') |ForEach-Object {
  $paramName,$paramValue = $_.Split('=')
  Write-Host "Received query parameter ${paramName} with value '$paramValue'"
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206