Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm using this to test for return of service: $loop = "loop" do {$response = $null; Start-Sleep -Seconds 5 try {$response = Invoke-WebRequest "https://login.microsoftonline.com/common/oauth2/" -ErrorAction SilentlyContinue | select status*} catch {Write-Host "Down $(get-date)"} finally {} if ($response) {Write-Host "OK" ($response).StatusCode (Get-Date)} } while ($loop -eq "loop")


Doesn't work for me, I get:

At line:1 char:16 + $loop = "loop" do {$response = $null; Start-Sleep -Seconds 5 try {$re ...

+ ~~

Unexpected token 'do' in expression or statement.

+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken


Cleaned-up version

while ($true) { $response = $null; try { $response = Invoke-WebRequest "https://login.microsoftonline.com/common/oauth2/" -ErrorAction SilentlyContinue | select status*; if ($response) { "OK $($response.StatusCode) $(Get-Date)" | Write-Host; } } catch { "Down $(Get-Date)" | Write-Host; }; Start-Sleep -Seconds 5; }


Ahh, thank you. I simply dabble in PS on the side and write util-like scripts as a brain stretch. This is the first time I've ever had someone look at and 'clean' anything... much appreciated.


Thank you, that works.


Sorry about that - I have no idea how to format comments. I placed a copy here: https://www.codepile.net/pile/gz77EXzd


This version works for me, thank you :-)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: