Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Use CURL to send/receive commands from a PowerShell 5.1 process

Aug
2,799
144
Thiis is just a proof-of-concept to see if it could be done.

Continue to use the PShell command to interact with PowerShell 5.1
------------------------------
I have PowerShell 5.1 running in a Windows Terminal tab.

From the PowerShell 5.1 running in a Windows Terminal tab, I run the following;
Code:
PS U:\> .\ps_http_server.ps1
PS HTTP server listening on http://127.0.0.1:8080/

From TCC
Get the current date using PowerShell;
Code:
R:\>curl -s -X POST -d "get-date | Out-String" http://127.0.0.1:8080/

April 2, 2026 9:19:42 PM

From TCC
Get the location of the PowerShell Profile;
Code:
R:\>curl -s -X POST -d "$Profile" http://127.0.0.1:8080/
C:\Users\jlcav\OneDrive\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

From TCC
Get the content of the tccrt function that I have defined in PowerShell 5.1
Code:
R:\>curl -s -X POST -d "get-content function:tccrt" http://127.0.0.1:8080/

  & "C:\Program Files\JPSoft\TCC_RT_35\tcc.exe" /I /Q /C $args

From TCC
Launch tccrt and display the ISODATE
Code:
R:\>curl -s -X POST -d "tccrt echo %_isodate" http://127.0.0.1:8080/
2026-04-02

From TCC
Close the PowerShell 5.1 server
Code:
U:\>curl -s -X POST -d "exit" http://127.0.0.1:8080/
bye

ps_http_server.ps1
Code:
# ps_http_server.ps1
# Launch the TCP Server in an already running PowerShell 5.1
# .\ps_http_server.ps1
#
# curl -s -X POST -d "exit" http://127.0.0.1:8080/
$prefix = "http://127.0.0.1:8080/"
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add($prefix)
$listener.Start()
Write-Host "PS HTTP server listening on $prefix"

try {
  while ($listener.IsListening) {
    $ctx = $listener.GetContext()   # blocking
    try {
      $req = $ctx.Request
      $resp = $ctx.Response

      $reader = New-Object System.IO.StreamReader($req.InputStream, $req.ContentEncoding)
      $body = $reader.ReadToEnd()
      $reader.Close()

      # Accept raw command in body or JSON {"cmd":"..."}
      $cmd = $null
      try { $obj = ConvertFrom-Json $body -ErrorAction Stop; $cmd = $obj.cmd } catch { $cmd = $body.Trim() }

      if ($cmd -eq 'exit') {
        $out = "bye"
        $buffer = [System.Text.Encoding]::UTF8.GetBytes($out)
        $resp.ContentType = "text/plain; charset=utf-8"
        $resp.ContentLength64 = $buffer.Length
        $resp.OutputStream.Write($buffer, 0, $buffer.Length)
        $resp.OutputStream.Close()
        break
      }

      # Execute and capture output and errors
      $out = & { Invoke-Expression $cmd } 2>&1 | Out-String
      if ($out -eq $null) { $out = "" }

      $buffer = [System.Text.Encoding]::UTF8.GetBytes($out)
      $resp.ContentType = "text/plain; charset=utf-8"
      $resp.ContentLength64 = $buffer.Length
      $resp.OutputStream.Write($buffer, 0, $buffer.Length)
      $resp.OutputStream.Close()
    } catch {
      try {
        $err = $_.ToString()
        $buf = [System.Text.Encoding]::UTF8.GetBytes("ERROR: $err")
        $ctx.Response.StatusCode = 500
        $ctx.Response.ContentType = "text/plain; charset=utf-8"
        $ctx.Response.ContentLength64 = $buf.Length
        $ctx.Response.OutputStream.Write($buf,0,$buf.Length)
        $ctx.Response.OutputStream.Close()
      } catch {}
    }
  }
} finally {
  $listener.Stop()
  $listener.Close()
}

Joe
 
This also works well with PowerShell 7.5.5

Code:
PowerShell 7.5.5

   A new PowerShell stable release is available: v7.6.0
   Upgrade now, or check out the release page at:
     https://aka.ms/PowerShell-Release?tag=v7.6.0

Loading personal and system profiles took 1386ms.
PS U:\> .\ps_http_server.ps1
PS HTTP server listening on http://127.0.0.1:8080/

From TCC
Code:
U:\>curl -s -X POST -d "$PSVersionTable" http://127.0.0.1:8080/

Name                           Value
----                           -----
PSVersion                      7.5.5
PSEdition                      Core
GitCommitId                    7.5.5
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

From TCC
Code:
U:\>curl -s -X POST -d "exit" http://127.0.0.1:8080/
bye

Joe
 
Back
Top