Script that does not run correctly in scheduled tasks

adgm1 Posted messages 306 Status Member -  
adgm1 Posted messages 306 Status Member -

Hello,

I have a script that blacklists wifi SSIDs when a particular SSID is discovered. Everything works when I run my batch which in turn should launch a .vbs script under a specific condition.

The problem arises when I schedule my script to run at logon or unlock. I do enable maximum permissions and use the SYSTEM account.

The .vbs fails to turn on the wifi while it does just fine when launched manually. Any idea why the sendkeys commands are not executed?

The batch:

@echo off chcp 65001 >nul setlocal enabledelayedexpansion :checkwifi REM check if wifi is on netsh wlan show networks if %errorlevel% equ 0 goto listwifi else do ( call "C:\toggle_wifi_on.vbs" goto CHECKSSID ) :listwifi REM list nearby wifi networks netsh wlan show networks > "c:\listwifi.txt" goto CHECKSSID :CHECKSSID netsh wlan show networks > "c:\listwifi.txt" set var="freeboxAD" findstr /i %var% "c:\listwifi.txt" >nul if %errorlevel% equ 1 ( REM unblock the blacklist and re-allow all SSIDs netsh wlan delete filter permission=denyall networktype=infrastructure ) else ( REM blacklist foreign SSIDs and allow work SSID netsh wlan add filter permission=denyall networktype=infrastructure netsh wlan add filter permission=allow ssid="FreeboxAD" networktype=infrastructure ) exit 

The .vbs

set WshShell = Wscript.createObject("WScript.Shell") WshShell.run "ms-availablenetworks:" WScript.Sleep (1000) WshShell.SendKeys ("+{TAB}") WshShell.SendKeys (" ") WScript.Sleep(700) WshShell.SendKeys ("{ESC}")


6 answers

fabul Posted messages 42078 Registration date   Status Moderator Last intervention   6 054
 

Hello,

In a scheduled task, for a script, you need to specify the %Path%

(The folder where the file is located (Ex: C:\Users\x))

(Start In (Optional)) in addition to the full file path.

If you haven't done it, it might fail like that...

0
adgm1 Posted messages 306 Status Member 10
 

Okay, can you tell me more about this subject? I can see the "start in" section under the action tab, but what %path% should I use? The same as where my 2 files are located? That is, C:\?

0
fabul Posted messages 42078 Registration date   Status Moderator Last intervention   6 054
 

The same path without \Example.bat

The folder where the script file is located.

I never place it directly on C:\

C:\Users\x works for me when the batch is in the x folder.

Directly on C: it might be C: without \, I don't know if that's possible.

0
brucine Posted messages 24677 Registration date   Status Member Last intervention   4 146
 

Hello,

Aside from the vbs issue that may or may not be read at startup and the permissions, to run a batch in a scheduled task (when the cmd file is not in the PATH), I specify the fully qualified path:

but these are not batches launched at startup (and for which you will have to check "run even if the user is not logged in" and also use, I suppose, the appropriate password); the only scheduled task I have at startup is launching my security software, which has nothing specific except that the user account is "INTERACTIVE", an unknown quantity.

0
adgm1 Posted messages 306 Status Member 10 > brucine Posted messages 24677 Registration date   Status Member Last intervention  
 

Hello Brucine. I think I found what is blocking my VBS. It seems that in the scheduled task under the general tab, if you choose the system account, it automatically checks "run whether the user is logged on or not". So you have to select the user account and then check option 1 (run only when user is logged on), and then the VBS runs correctly. Strange. It's a shame because with this scheduled task configuration, the prompt window is visible while everything was invisible with the "SYSTEM" account.

0
brucine Posted messages 24677 Registration date   Status Member Last intervention   4 146 > adgm1 Posted messages 306 Status Member
 

Try cheating by scheduling not the batch file, but a shortcut, properties, run in minimized window.

0
adgm1 Posted messages 306 Status Member 10 > brucine Posted messages 24677 Registration date   Status Member Last intervention  
 

Actually, the big problem if I choose the user account is that the netsh command is not accepted from a standard session because of insufficient rights. I absolutely need to use the SYSTEM account, but then the VBS stops midway. It's strange because the VBS starts fine and executes the first command (opening the wifi menu), but the others remain on standby and just crash (sendkeys).

0
adgm1 Posted messages 306 Status Member 10
 

It doesn't seem to change much. In fact, the .vbs that is launched seems to need to maintain an activity to validate the keyboard key press actions (Shift+Tab in this case), otherwise the actions don't chain together and remain stuck at the first step, which is to open the Wi-Fi settings from the taskbar.

So basically, my .vbs runs fine at login (if the Wi-Fi is off) but it doesn't execute all the requested commands.

0
barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
 

Hello,

Rather than using SendKeys, have you ever tried directly in batch with a command like this:

netsh interface set interface name="Wireless Network Connection" admin=enabled

If that doesn't work, we should look into Powershell.


0
brucine Posted messages 24677 Registration date   Status Member Last intervention   4 146
 

I actually have something like that which isn't useful to me anyway, I don't remember why I made it, that switches from Wifi to Ethernet and Versailles high school (but which requires checking the corresponding index for the network adapter in question):

@echo off CHCP 65001>NUL setlocal enableextensions setlocal EnableDelayedExpansion FOR /F "skip=5 tokens=2,4 delims= " %%a IN ('netsh interface show interface') DO ( SET STATUT=%%a & SET NATURE=%%b SET STATUT=!STATUT:~0,-1! IF "!STATUT!"=="Connected" ( IF "!NATURE!"=="Ethernet" ( netsh interface set interface name="!NATURE!" admin=DISABLED ) ELSE wmic path win32_networkadapter where index=2 call disable ) IF "!STATUT!"=="Disconnected" ( IF "!NATURE!"=="Ethernet" ( netsh interface set interface name="!NATURE!" admin=ENABLED ) ELSE wmic path win32_networkadapter where index=2 call enable & netsh interface ipv4 set interface "Wi-Fi" metric=1 & netsh wlan connect name ="SFR_A918" ) ) 
0
adgm1 Posted messages 306 Status Member 10 > brucine Posted messages 24677 Registration date   Status Member Last intervention  
 

Thank you for this proposal. In fact, the netsh or wmic commands do not allow activating the wifi signal (the switch ON-OFF button) but only activating the wifi network card. Standard users cannot disconnect the network card which requires admin rights, so the only thing that can block the visibility of wifi networks (and which users have control over) is the wifi "button" on the taskbar or on the login page.

0
adgm1 Posted messages 306 Status Member 10
 

Unfortunately, I had already tried. This command only allows you to activate the Wi-Fi card if it is disabled. But I would rather "turn on" the Wi-Fi switch. This whole story is a bit annoying, there is the Wi-Fi card on one side and the On-off button in the quick actions (taskbar) on the other. So I don't see any other solution than the VBS script which works quite well except when scheduled. I can't understand why.

Is there a solution in PowerShell to toggle the Wi-Fi button?

0
adgm1 Posted messages 306 Status Member 10
 

The idea of the script is to create a semblance of geolocation of the user's laptop (inside or outside a designated place). If at the start of the session the "lycee" Wi-Fi is detected among the list of nearby hotspots, then the user is at the school. Therefore, we blacklist all hotspots except "lycee" to prevent students from using a mobile hotspot. If they are at home, then the school Wi-Fi is no longer visible, so we do the opposite effect: we remove the blacklist of hotspots and allow connection to any nearby Wi-Fi network.

However, in the case where the user has clicked the ON-Off button for the Wi-Fi, the script cannot function properly, and it needs to be turned ON beforehand so that the list of hotspots can be created and analyzed.

0