Script required to automatically connect to specific Wi-Fi network

Page 7 of 8 FirstFirst ... 5678 LastLast

  1. Posts : 1,807
    Windows 10 Pro 21H1 19043.1348
    Thread Starter
       #61

    Matthew Wai said:
    In "Netsh.vbs", replace the original line with the following line:
    Code:
    CreateObject("WScript.Shell").Run"%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi"""
    You can see that the only difference is the addition of the highlighted part. Manually run the task again. If an error arises, the problem lies in that part. Note that a flashing window is normal.

    You may run the following command via PowerShell to view the task results:

    Get-ScheduledTaskInfo "Task Name"

    Replace Task Name with the actual name.

    Most interesting after that update...

    When I manually ran the task in TS, it successfully connected 5 times for 5 tests.
    When I close the lid (without AC) and then open, no connection.
    When I close the lid and add AC just before opening, it successfully connects.

    Summary;

    The simple change to the naming of C:\ to %WinDir% seems to allow the code to function 100% when the task is manually launched. Fascinating and a great catch Matthew. Bravo and thanks!

    It does appear as though the Task Scheduler will not successfully run the script when the PC is awakened from sleep without the 'Start the task only if the PC is on AC power' checked and with the AC power connected.

    Could this be related to what Denis @Try3 was suggesting when he said the Task Manager reliability on DC power was less than stellar?
    Last edited by W10 Tweaker; 08 Mar 2021 at 17:33.
      My Computer


  2. Posts : 16,576
    Windows 10 Home x64 Version 22H2 Build 19045.3930
       #62

    W10 Tweaker said:
    It does appear as though the Task Scheduler will not successfully run the script when the PC is awakened from sleep without the 'Start the task only if the PC is on AC power' checked and with the AC power connected.
    Could this be related to what Denis @Try3 was suggesting when he said the Task Manager reliability on DC power was less than stellar?
    I did not say that. I said
    Try3 said:
    TS cannot wake from hibernation unless AC is available.
    Nothing about reliability or what particular settings have been made. Instead, a straightforward cannot.

    Denis
      My Computer


  3. Posts : 1,807
    Windows 10 Pro 21H1 19043.1348
    Thread Starter
       #63

    Try3 said:
    I did not say that. I said

    Nothing about reliability or what particular settings have been made. Instead, a straightforward cannot.

    Denis


    Sorry, my mistake. I must have remembered what I wanted to hear.

    I need to perform further testing to try and determine why it connects perfectly, 100% of the time when I run the task manually, yet only intermittently when I try to run the task automatically, when returning from a sleep state.

    So far, it has successfully connected from a sleep state when I apply AC power, pause for a minute and then open the lid. I also have set the condition to 'Start the task only if the PC is on AC power'. I made this change after numerous unsuccessful connections returning from sleep with AC power off.
      My Computer


  4. Posts : 7,607
    Windows 10 Home 20H2
       #64

    Code:
    CreateObject("WScript.Shell").Run"%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi""",0
    Add ,0 at the end of the command to hide the flashing window.

    W10 Tweaker said:
    numerous unsuccessful connections returning from sleep with AC power off.
    Does "unsuccessful connections" mean that "Netsh.vbs" ran automatically but failed to start the connection?

    W10 Tweaker said:
    The simple change to the naming of C:\ to %WinDir% seems to allow the code to function 100% when the task is manually launched.
    Manually run the following task and see whether a window will pop up.

    "C:\Scripts\WLAN\Netsh_testing.vbs"

    CreateObject("WScript.Shell").Run"netsh"

    No %WinDir% is used in the command.

      My Computer


  5. Posts : 1,807
    Windows 10 Pro 21H1 19043.1348
    Thread Starter
       #65

    Matthew Wai said:
    Code:
    CreateObject("WScript.Shell").Run"%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi""",0
    Add ,0 at the end of the command to hide the flashing window.

    ,0 added. No more cmd window flash. Thanks.



    Matthew Wai said:
    Does "unsuccessful connections" mean that "Netsh.vbs" ran automatically but failed to start the connection?

    Thanks for being more precise than my former description, I'll keep working on that.

    As far as I can tell from reading logs in the TS and PS, it does appear the task is running but not successfully connecting.

    Please read below before engaging your neural processor in high gear.



    Matthew Wai said:
    Manually run the following task and see whether a window will pop up.

    "C:\Scripts\WLAN\Netsh_testing.vbs"

    CreateObject("WScript.Shell").Run"netsh"

    No %WinDir% is used in the command.


    I skipped this last test in favour of fiddling with the task triggers. After reading the logs, I started to get the impression that running this task with 5 different triggers and a condition may have been contributing to the hit and miss performance.




    So... at this juncture, I believe I have arrived at my goal.


    The task appears to now run 100% of the time and without the 'AC power on' condition. I've de-cluttered and disabled 4 of the 5 triggers, leaving only 'On workstation unlock of any user' set.

    I'll continue testing to confirm reliability and compose a summary of the settings, just on the off chance that someone else is as particular as I am.


    Thank-you Matthew and Denis for your religious-like conviction to assist me on this project. Your efforts are greatly appreciated. And thanks to others like Shawn who have added ideas along the way.



    I'll try to keep updating this reliability detail.


    8 successful auto-connect to Wi-Fi out of 11 attempts.
    Last edited by W10 Tweaker; 12 Mar 2021 at 08:32.
      My Computer


  6. Posts : 7,607
    Windows 10 Home 20H2
       #66

    W10 Tweaker said:
    8 successful auto-connects to Wi-Fi out of 11 attempts.
    The following code will make 4 attempts to start the connection. See whether it works better. The pop-up message (i.e. the last line) is optional although the error code could be useful.
    Code:
    Set X=CreateObject("WScript.Shell"):Do:Return=X.Run _
    ("%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi""",0,True)
    If Return = 0 then WScript.Quit 
    Attempts = Attempts + 1 : Loop until Attempts = 4
    MsgBox "Error code: " & Return
      My Computer


  7. Posts : 1,807
    Windows 10 Pro 21H1 19043.1348
    Thread Starter
       #67

    Matthew Wai said:
    The following code will make 4 attempts to start the connection. See whether it works better. The pop-up message (i.e. the last line) is optional although the error code could be useful.
    Code:
    Set X=CreateObject("WScript.Shell"):Do:Return=X.Run _
    ("%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi""",0,True)
    If Return = 0 then WScript.Quit 
    Attempts = Attempts + 1 : Loop until Attempts = 4
    MsgBox "Error code: " & Return

    Thanks Matthew, I'll give that a whirl asap and report.
      My Computer


  8. Posts : 1,807
    Windows 10 Pro 21H1 19043.1348
    Thread Starter
       #68

    Matthew Wai said:
    The following code will make 4 attempts to start the connection. See whether it works better. The pop-up message (i.e. the last line) is optional although the error code could be useful.
    Code:
    Set X=CreateObject("WScript.Shell"):Do:Return=X.Run _
    ("%WinDir%\System32\netsh.exe wlan connect ""[ssid]"" ""{profile name]"" ""Wi-Fi""",0,True)
    If Return = 0 then WScript.Quit 
    Attempts = Attempts + 1 : Loop until Attempts = 4
    MsgBox "Error code: " & Return


    OK, new vbs code set and I did include the last line for potential future diagnostics.

    I ran the task manually and it scored 10 successful network connects for 10 attempts. Bravo and thanks for your continued support.

    I'll start and maintain a new reliability summary in this post.




    1 successful auto-connect to Wi-Fi out of 1 attempt.
      My Computer


  9. Posts : 1,746
    Windows 10 Pro x64 22H2
       #69

    Hello guys, this is an interesting thread!

    I didn't know automating WI-FI could be such a geeky problem but I did some quick research to see why and what other alternatives could there be.

    I don't want to hijack this thread or anything similar but there are things that should probably be discussed.

    1. netsh.exe is deprecated:
    https://social.technet.microsoft.com...m=w81previtpro

    2. WBScript is deprecated as well:
    Microsoft puts another nail in VBScript coffin Naked Security

    Depreciation primarily means:
    1. in the future this solution may no longer work
    2. security issues, primarily storage of WI-FI password

    powershell seems like it replaces both of these things but according to my quick research there is no well established solution, but there is a PS module made by MSFT guy:

    GitHub - jcwalker/WiFiProfileManagement: Module used for management of wireless profiles

    The rest seems like an easy solution to create a script, I found a simple tutorial here:
    Manage WiFi connection in Windows 10 with PowerShell | 4sysops

    Another great feature is that task management and script auto-run at logon in Windows 10 already prefers powershell trough GPO:

    Script required to automatically connect to specific Wi-Fi network-untitled.png

    I don't know how does wbscript store passwords but in powershell there is a class for this purpose:
    https://docs.microsoft.com/en-us/dot...g?view=net-5.0

    You could also use PSCredential object only to store password and SSID although I'm not sure how smart could that be for storage, but surely OK if prompting at logon.

    My whole point here is that I would seek for a solution that satisfies 3 conditions:
    1. is modern approach thus time durable
    2. is secure for todays challenges
    3. is easy to manage and update as needed

    I may be wrong or missing some points, if so I'm glad to hear your suggestions.
      My Computer


  10. Posts : 7,607
    Windows 10 Home 20H2
       #70

    zebal said:
    Michael does not mean to use VBScript on a website/browser.

    zebal said:
    I don't know how does wbscript store passwords
    The password is in the VBScript file.

    zebal said:
    1. in the future this solution may no longer work
    The VBScript file is not intended to solve a problem but to start a connection.
    Very simple, basic commands are used in the script. No support and update are needed.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 07:32.
Find Us




Windows 10 Forums