# Simple WebService to run various powershell commands # You have to install and bind certificate manually, if HTTPS is used as Admin with # netsh http add sslcert ipport=0.0.0.0:44388 certhash= "appid={01234567-1234-1234-1234-1234567890AB}" # remove with # netsh http delete sslcert ipport=0.0.0.0:44388 param ( [string]$listenparam = "http://+:44388/", #"https://+:443/", #"http://+:80/" [string]$configfile = ".\config.csv" ) set-PSDebug -strict $error.clear() Write-host "ADSyncWebService:Start" Write-host " Parameter Listenparam: $($listenparam)" Write-host " Parameter Configfile : $($configfile)" Write-host " Checking Configfile $($configfile)" if (Test-Path -path $configfile -pathtype leaf) { Write-host " Importing Configfile $($configfile)" $configcsv = import-csv $configfile Write-host " Configfile loaded. total lines $($configcsv.count)" $line = 0 foreach ($config in $configcsv) { $line++ Write-host " Checking config line $($line) " -nonewline if (!$config.action) {Write-Host "no mandantory Action found" -ForegroundColor red; exit} if (!$config.secret) {Write-Host "no mandantory Secret found" -ForegroundColor red; exit} if (!$config.script) {Write-Host "no mandantory script found" -ForegroundColor red; exit} Write-host "Looks valid" -ForegroundColor green } } else { Write-Error "Configfile $($configfile) not found" } Write-host " Init HTTPListener on $($listenparam)" try{ $HttpListener = New-Object System.Net.HttpListener $HttpListener.Prefixes.Add($listenparam) $HttpListener.Start() } catch { Write-Error "Unable to create HTTPListener. Run as Admin or add binding with NetSH" Exit 1 } While ($HttpListener.IsListening -and (!([console]::KeyAvailable))) { Write-Host "---- Waiting for Request on $($listenparam) Press any key and wait until next request arrives" -BackgroundColor blue $HttpContext = $HttpListener.GetContext() if(([console]::KeyAvailable)){ Write-host "Keypress detected - terminating script" -ForegroundColor green break } $HttpRequest = $HttpContext.Request $RequestUrl = $HttpRequest.Url.OriginalString Write-Host " Got: $($RequestUrl)" -ForegroundColor green if($HttpRequest.HasEntityBody) { $Reader = New-Object System.IO.StreamReader($HttpRequest.InputStream) $body = $Reader.ReadToEnd() Write-Host " Body: $($body)" } else { Write-Host " Body: " [string]$body=$null } #$HttpRequest $request = [pscustomobject][ordered]@{ url = $httpRequest.url rawurl = $httpRequest.rawurl headers = $httpRequest.headers HttpMethod = $httpRequest.HttpMethod UserAgent = $httpRequest.UserAgent RemoteEndPoint = $httpRequest.RemoteEndPoint QueryString = $HttpRequest.QueryString body = $body } $returnbody = @() [string]$action = $HttpRequest.QueryString["action"] [string]$secret = $HttpRequest.QueryString["secret"] $HttpResponse = $HttpContext.Response Write-Host " Action:$($action)" Write-Host " Secret:$($secret)" if (!$action){ Write-Warning "Action not specified" $returnbody = "Action not specified" $HttpResponse.StatusCode = 400 # bad request } elseif (!$secret){ Write-Warning "Secret not specified" $returnbody = "Secret not specified" $HttpResponse.StatusCode = 400 # bad request } else { Write-Host "---Start Processing Configuration" foreach ($config in $configcsv) { Write-Host "CheckingConfigLine $(($config | convertto-csv -NoTypeInformation)[1])" -NoNewline if ($config.action -ne $action) { Write-Host "Action-NoMatch" -ForegroundColor Yellow } elseif ($config.secret -ne $secret) { Write-Host "Secret-NoMatch" -ForegroundColor Yellow } else { Write-Host "Match" Write-host "Starting Script $($config.script)" -ForegroundColor Magenta $returnbody+= & ".\$($config.script)" -request $request } } } if (!$returnbody) { $returnbody+="{Result = NoRulesMatch}" } $HttpResponse.StatusCode = 200 $HttpResponse.Headers.Add("Content-Type","application/json") $ResponseBuffer = [System.Text.Encoding]::UTF8.GetBytes(($returnbody | ConvertTo-Json)) $HttpResponse.ContentLength64 = $ResponseBuffer.Length $HttpResponse.OutputStream.Write($ResponseBuffer,0,$ResponseBuffer.Length) $HttpResponse.Close() Write-Host "Processing Done" } Write-host " Stopping HTTPListener" $HttpListener.Stop() Write-host "ADSyncWebService:End"