Last active
January 21, 2020 17:04
-
-
Save remcok/a185b9027ca85be1e18414e29b73f98a to your computer and use it in GitHub Desktop.
Scheduled task with powershell calling http resulting exit code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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