Skip to content

Instantly share code, notes, and snippets.

@remcok
Last active January 21, 2020 17:04
Show Gist options
  • Save remcok/a185b9027ca85be1e18414e29b73f98a to your computer and use it in GitHub Desktop.
Save remcok/a185b9027ca85be1e18414e29b73f98a to your computer and use it in GitHub Desktop.
Scheduled task with powershell calling http resulting exit code
Add a scheduled task to Windows to execute the script "wake-up-http.ps1"
powershell -NonInteractive -NoLogo -NoProfile -Command ". C:\wake-up-http.ps1; Exit $LASTEXITCODE"
$body = "userName=<<USERNAME>>&password=<<PASSWORD>>"
$uri = "http://localhost:8080/api/auth"
$request = [System.Net.WebRequest]::Create($uri)
$request.Accept = "application/json"
$request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
$request.ContentLength = $body.length
$request.Method = "POST"
try
{
$requestStream = $request.GetRequestStream()
$streamWriter = New-Object System.IO.StreamWriter($requestStream)
$streamWriter.Write($body)
}
finally
{
if ($null -ne $streamWriter) { $streamWriter.Dispose() }
if ($null -ne $requestStream) { $requestStream.Dispose() }
}
$status = 0;
try
{
$response = $request.GetResponse()
}
catch [System.Net.WebException] {
If ($_.Exception.Response.StatusCode.value__) {
$status = [int]($_.Exception.Response.StatusCode.value__ ).ToString().Trim();
}
If ($_.Exception.Message) {
$crapMessage = ($_.Exception.Message).ToString().Trim();
Write-Output $crapMessage;
}
}
finally
{
if ($null -ne $response) { $response.Close() }
}
Exit $status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment