0

i wanna do a script which first checks the internet connection and if the computer has internet downloads a file. If the computer has no internet, the shell should write "You are not connected with the internet". The script does not work with

connection test-netconnection www.hawk.de -CommonTCPPort HTTP{}

My script is:

if() {

$client = new-object System.Net.WebClient
$client.Credentials = Get-Credential
$client.DownloadFile(“https://www.mydomain.de/sites/default/files/styles/xs_12col_16_9_retina/public/DSC_6947.JPG”,“C:\Users\Ole\Downloads\Github\bild.JPG”)
}else {
  Write-Host "Could not connect to the Internet."
}
fi

Thanks for your help.

2
  • Please be a bit more detailed than "does not work" - what happens? What behavior are you observing? Are any errors thrown? If so, post them here :) Remember, we can't see your screen Commented Mar 9, 2020 at 12:37
  • 1
    fi is not powershell. Your if ends at the last curly brace. Commented Mar 9, 2020 at 13:29

3 Answers 3

2

Windows tracks Internet connection status as part of Network Location Awareness; you can check this status using something like this:

If ((Get-NetConnectionProfile).IPv4Connectivity -contains "Internet" -or (Get-NetConnectionProfile).IPv6Connectivity -contains "Internet") {
    # Do something here
}

https://learn.microsoft.com/en-us/windows/win32/winsock/network-location-awareness-service-provider-nla--2

Sign up to request clarification or add additional context in comments.

Comments

1

Try this for your if block..

IF (((Test-NetConnection www.site.com -Port 80 -InformationLevel "Detailed").TcpTestSucceeded) -eq $true)

Change the web address to suit obviously...

6 Comments

-eq $true is superfluous. TcpTestSucceeded is already a boolean.
Maybe - but it makes the code easier for others to read and understand.... IT also allows others to easily change the code/logic if they want something to happen if it fails. So having it there makes the code easier to read, easier to refactor, and easier for those less powershell savvy to understand. Never post shorthand code... Every time you do god kills a puppy.
Then I propose you add another -eq $true to test the truth value of that statement, and another -eq $true to test the truth value of that statement, and another ...
There's only one statement bro... Do you want something to happen if it's true or if it's false? You can easily change the true to a false in either case based on desired results. What I posted solves the OP's question with an easy to read, simple and flexible code block. You're nitpicking because maybe my code doesn't exactly look like what you would have written. That's fine, but it adds nothing to the conversation.
If voted up the answer because it is correct. But to me if (x.TcpTestSucceeded) reads as normal English, whereas if (x.TcpTestSucceeded) -eq $true doesn't. I don't ask my children "Have you done your homework -eq $true?".
|
0

You can use try..catch block for this:

try {
  $client = new-object System.Net.WebClient $client.Credentials = Get-Credential $client.DownloadFile(“https://www.mydomain.de/sites/default/files/styles/xs_12col_16_9_retina/public/DSC_6947.JPG”,“C:\Users\Ole\Downloads\Github\bild.JPG”) 
}
catch {
  write-host "Not connected to the internet"
}

if..fi syntax refers to bash that's not powershell. Try..Catch is used to handle exceptions and errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.