Advertisement
metalx1000

Run CMD Command web server interface

Aug 13th, 2015
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. This creates a web server on port 800 that runs given commands
  3. It servers up output from this script.
  4. example: http://localhost:8000/?cmd=dir c:\
  5.  
  6. #>
  7.  
  8. $url = 'http://localhost:8000/'
  9. $listener = New-Object System.Net.HttpListener
  10. $listener.Prefixes.Add($url)
  11. $listener.Start()
  12.  
  13. Write-Host "Listening at $url..."
  14.  
  15. while ($listener.IsListening)
  16. {
  17.     $context = $listener.GetContext()
  18.     $requestUrl = $context.Request.Url
  19.     $response = $context.Response
  20.    
  21.     Write-Host ''
  22.     Write-Host "> $requestUrl"
  23.  
  24.     $localPath = $requestUrl.LocalPath
  25.    
  26.    
  27.     $cmd = $requestURL -split "cmd=";
  28.     $cmd = $cmd[1].split();
  29.    
  30.     Write-Host "Executing: cmd /c $cmd"
  31.    
  32.     $OutputVariable = & cmd /c $cmd | Out-String
  33.    
  34.     Write-Host "$OutputVariable"
  35.    
  36.    
  37.     $buffer = [System.Text.Encoding]::UTF8.GetBytes($OutputVariable)
  38.     $response.ContentLength64 = $buffer.Length
  39.     $response.OutputStream.Write($buffer, 0, $buffer.Length)
  40.    
  41.    
  42.     $response.Close()
  43.  
  44.     $responseStatus = $response.StatusCode
  45.    
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement