- 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;
From TCC
Get the current date using PowerShell;
From TCC
Get the location of the PowerShell Profile;
From TCC
Get the content of the tccrt function that I have defined in PowerShell 5.1
From TCC
Launch tccrt and display the ISODATE
From TCC
Close the PowerShell 5.1 server
ps_http_server.ps1
Joe
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