I want part of my script to only run if the computer has any established network connection. It can be wifi, ethernet, anything that's not virtual. I don't need full internet, just LAN.
This is all for Windows 10 64bit, and this script is part of a larger script I'm going to use for deploying some machines.
Note: I'm definitely a powershell noob, I'm not very familiar with it. Honestly I'm still a noob with programming and scripting in general, but powershell especially.
I think I need some function to check each network adapter individually, but I'm not sure how to do that in powershell.
if (Get-NetAdapter -Name "*" -Physical == "Up") {
Write-Host "Pass"
}
else {
Write-Host "Fail"
}
I hoped my code would maybe check each option in the list of Get-NetAdapter
, but it gave an error on the ==
argument, it can't find a positional parameter that accepts it. I also tried =
which gave the same error.
Examplecode to check an array of network adapter
$networkavailable = $false;
foreach ($adapter in Get-NetAdapter){
if ($adapter.status -eq "Up"){$networkavailable = $true; break;}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With