# Help correcting code to be successful

**URL:** <https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612>\
**Category:** PowerShell Help\
**Created:** [October 10, 2022, 7:31pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612 "2022-10-10T19:31:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marines214](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marines214](https://forums.powershell.org/u/marines214)\
**Post date:** [October 10, 2022, 7:31pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/1 "2022-10-10T19:31:41Z")

</div>

# Continously Test-NetConnection until PingSucceeded = True so I can be notified via email and then force the NIC to be disabled.

fucntion disableNic () {  
Send-MailMessage -To GOOGLE EMAIL -Subject ‘Lab 1 computer connected to internet’ -Body “LAB 1 CONNECTED TO WIFI” -Credential (Get-Credential) -SMTPServer [smtp.google.com](http://smtp.google.com) -port 587 -from EMAIL  
Get-NetAdapter | ?{ $\_.status -eq “UP” } | Disable-NetAdapter -Confirm:$false  
}

while (1 -eq 1) {  
if (Test-Netconnect -Quiet) -4q $True {  
disableNIC()  
}  
}

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 10, 2022, 7:45pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/2 "2022-10-10T19:45:28Z")

</div>

Adam,  
Welcome to the forum. 👋🏽

What’s your question?

And BTW: When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://aws1.discourse-cdn.com/business5/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![marines214](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marines214](https://forums.powershell.org/u/marines214)\
**Post date:** [October 10, 2022, 9:03pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/3 "2022-10-10T21:03:27Z")

</div>

Hi and thank you.

Yes, I am looking to create a script where it does the following: 1) Continuously check for an internet connection (Even if the computer is turned off or restarted). 2) Sends a notification via email 3) Disables Ethernet and Wi-Fi connection. 4) restarts the script again.

\<

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 10, 2022, 9:21pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/4 "2022-10-10T21:21:44Z")

</div>

> [@marines214](#):
>
> Yes, I am looking to create a script where it does the following:

OK. And what is your question? You should ask a specific question about a particular piece of you code you wrote yourself and you have an issue with.

Please keep in mind - this forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

---

<div class="post-metadata">

**Author:** ![marines214](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marines214](https://forums.powershell.org/u/marines214)\
**Post date:** [October 10, 2022, 9:53pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/5 "2022-10-10T21:53:17Z")

</div>

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/b3139e23b8f492b1f03f3c273388f4f433b493be.png)

Thanks for taking the time and for being patient with me. What am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 10, 2022, 10:16pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/6 "2022-10-10T22:16:11Z")

</div>

Please do not post images of code or error messages. Instead post the plain text and format it as code.

There are several issues with your code. I’d recommend to do a big step back and start with learning the very basics of PowerShell first. This will save you from a lot of wasted time and frustrations.

You should ALWAYS read the complete help including the examples for the cmdlets you’re about to use to learn how to use them

> [@marines214](#):
>
> What am I doing wrong here?

Amlost everything. 😏 `Test-NetConnection` does not have a parameter `-Quiet`. The syntax for your `if` statement is wrong. You should consider adding a pause to decrease the load you put on your computer for this task.

> **[about If - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_if?view=powershell-7.2)**
>
> Describes a language command you can use to run statement lists based on the results of one or more conditional tests.

> **[Test-NetConnection (NetTCPIP)](https://learn.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=windowsserver2022-ps)**
>
> Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.

---

<div class="post-metadata">

**Author:** ![marines214](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marines214](https://forums.powershell.org/u/marines214)\
**Post date:** [October 11, 2022, 3:08pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/7 "2022-10-11T15:08:07Z")

</div>

Do you offer tutoring? lol. and/or recommend any resources.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 11, 2022, 3:14pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/8 "2022-10-11T15:14:52Z")

</div>

> [@marines214](#):
>
> recommend any resources.

I already linked 2 resources! 🤷🏽‍♂️ Did you read them? … completely? … including the examples?

Start with learning the very basics first

> **[Getting Started with PowerShell - PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/01-getting-started?view=powershell-7.2)**
>
> Where to find and how to launch PowerShell for new users.

> **[Getting Started with PowerShell 3.0](https://learn.microsoft.com/en-us/shows/getstartedpowershell3/)**
>
> This Jump Start is designed to teach the busy IT Professionals about this powerful management tool. Learn how PowerShell works and how to make PowerShell work for you from the experts Jeffrey Snover, the inventor of PowerShell, together with Jason...

---

<div class="post-metadata">

**Author:** ![marines214](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@marines214](https://forums.powershell.org/u/marines214)\
**Post date:** [October 11, 2022, 3:34pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/9 "2022-10-11T15:34:37Z")

</div>

I completely missed that. I apologize. I’m sorry you’re so frustrated.

Thanks for all your help. I truly appreciate it.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/help-correcting-code-to-be-successful/20612/10 "2024-05-16T20:16:33Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


