Home > Software engineering >  How to pass the body using powershell routes.jason
How to pass the body using powershell routes.jason

Time:12-03

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?

CodePudding user response:

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'"
}

CodePudding user response:

Mathias R. Jessen - we want vCenter, cluster , vmhost to be passed through payload and to be received in target script , here is the payload below.

{
"vCenter": "vCenter01",
"cluster" : "Cluster01",
"vmhost":"Host01"
}
  • Related