Skip to content

Instantly share code, notes, and snippets.

@nazwadi
Created November 24, 2016 04:40
Show Gist options
  • Save nazwadi/cba9c5abc522ee004215774e26edd741 to your computer and use it in GitHub Desktop.
Save nazwadi/cba9c5abc522ee004215774e26edd741 to your computer and use it in GitHub Desktop.
Another basic powershell webserver
function Invoke-Webserver {
Param(
[Parameter(Mandatory=$True)]
[string]$port,
[Parameter(Mandatory=$True)]
[string]$ip
)
$httpd = New-Object Net.HttpListener
$httpd.Prefixes.Add("http://"+$ip+":"+$port+"/")
$httpd.Start()
While ($httpd.IsListening) {
$HC = $httpd.GetContext()
$HRes = $HC.Response
$HRes.Headers.Add("Content-Type","text/html")
$Buf = [Text.Encoding]::UTF8.GetBytes((GC (Join-Path $Pwd ($HC.Request).RawUrl)))
$HRes.ContentLength64 = $Buf.Length
$HRes.OutputStream.Write($Buf,0,$Buf.Length)
$HRes.Close()
}
$httpd.Stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment