Get Output from a PS script and pass to a curl command RRS feed

  • Question

  • How do I pass the output of a powershell script to update a parameter in curl command .

    Example : 

    gci D:\temp | ? { $_.BaseName } | sort CreationTime -desc | select -f 1 

    If the output of the above command is a folder name ie xyz which corresponds to a "backup id" in below curl command .

    curl -k -H "Cache-Control:no-cache" -H "Content-Type:application/json" -u "user:pswd" -X POST http://xyz.com -d "{"""abc""":["""def"""],"""ghi""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""xyz"""}"   

    How can we use powershell to fetch the folder name  and update the backupid parameter in the above curl command and execute the curl  with the new updated backup id. In others other words if the output from powershell is 1234, it should update backupId of the .bat file running the above curl to be 1234 and get executed via task scheduler.                           

    • Moved by Bill_Stewart Wednesday, December 12, 2018 5:13 PM Abandoned
    Tuesday, August 14, 2018 8:45 PM

All replies

  • Save the output to a variable.
    Check if it is a directory
    If it is, use the variable in the curl command.

    For example:

    $gci = Get-ChildItem D:\temp | Sort-Object CreationTime -Descending | Select-Object -First 1
    if ($gci.Mode[0] -eq 'd') {
        # it's a directory!
        $Body = @{
            'abc' = @{
                'def' = $null
            'ghi' = @{
                'DATA' = $null
                'HISTORY' = $null
                'CONFIGURATION' = $null
            'backupId' = $gci.Name
        } | ConvertTo-Json
        # Rewrite Curl to Invoke-WebRequest
        Invoke-WebRequest -Uri http://xyz.com -Body $Body -ContentType ...

    Please note that curl is an alias for Invoke-WebRequest nowadays. This means you can't execute curl in your formatting using PowerShell. Please consider moving to native PowerShell.

    Tuesday, August 14, 2018 9:25 PM
    • I am trying pass a paramter for "backupid" in the curl syntax.
      First part get me the folder name for backup id. Second part holds the json under $opt and last part executes the curl command .
      $backupID = Get-ChildItem D:\temp | % {$_.BaseName} | Sort-Object CreationTime -Descending | Select-Object -First 1
      $opt = "{"""dgcRestoreOptions""":["""CUSTOMIZATIONS"""],"""repoRestoreOptions""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""$($backupID)"""}"
      Invoke-Command {&curl -k -H "Cache-Control:no-cache" -H "Content-Type:application/json" -u "user:passwd" -X POST https://example.com -d $opt}
      I get below error when I execute the script
      Unexpected token 'dgcRestoreOptions""":["""CUSTOMIZATIONS"""],"""repoRestoreOptions""":["""DATA""","""HISTORY""","""CONFIGURATION"""],"""backupId""":"""$($backupID)"""}"' in expression or statement. + CategoryInfo:ParserError: (:) [], ParseException + FullyQualifiedErrorId : UnexpectedToken
      Could you assist me with the error and how do I replace Invoke-Command to Invoke-webrequest for the last piece

    • Merged by Bill_Stewart Thursday, August 16, 2018 1:24 PM Duplicate
    Wednesday, August 15, 2018 10:54 PM
  • I was unable to reply to earlier post so I had to post another one.
    Wednesday, August 15, 2018 11:15 PM
  • There is no reason to not be able to reply.  Sometimes you may need to refresh the page and try again.  The servers are having issues today.


    • Edited by jrv Thursday, August 16, 2018 1:41 PM
    Wednesday, August 15, 2018 11:17 PM