1

In short: What I need is a way to find UUID of network connection "xyz" from a batch-file.

I'm trying to setup a script that will place a scheduled task on the current user. I intend to send this script to multiple persons and therefor need it to be fully automatic. This task has to run only when connected to a specific network.

What I have done:

  • Created the task I want in Task Scheduler and exported it to use it as a template for the task I want to import with the script.
  • Set the script to replace certain values in the template with user specific details.

The problem I have is with finding the UUID that task scheduler uses when setting Start only if the following network connection is availiable. From what I've found this is not the same UUID as the network interface UUID. The exported XML-file for this option looks like this:

<NetworkSettings>
  <Name>AndoidAP</Name>
  <Id>{some-random-UUID-here}</Id>
</NetworkSettings>

In the example I've used AndroidAP which would be a wireless device. The real case would be a wired connection. Don't know if this makes any difference?

Scheduler GUI

Any help or suggestions would be greatly appreciated!

Klinghust
  • 833

2 Answers2

1

Your Requirements

So essentially you have a task that you want to only run if certain conditions are TRUE:

  1. The process must be run only if connected to a specific network
  2. The process must be run only if a specific file share is available

Potentially

You could use some IF logic and simply check the status of the conditions and take action accordingly whether to "exit processing" or "keep processing" the rest of the defined logic.

Essentially, this will. . .

  1. ping the default gateway via its IP address
  2. check the arp cache for the MAC address of the default gateway piped to FindStr
    • If the MAC address is not found then EXIT
    • If the MAC address is found then keep processing
  3. check if the UNC share exists
    • If the UNC share does not exist then EXIT
    • If the UNC share does exist then keep processing i.e. the rest of the logic

Prerequisites

  1. Get the IPv4 address of the default gateway from the ipconfig command

    enter image description here

  2. Get the physical Mac address from the IP address of the default gateway

    • Run a ping command against the IP address of the default gateway
    • Run an arp -a command and note the Physical Address that has a matching value IP address of the default gateway

    enter image description here


Batch Script

Obviously the GatewayIP=, GWMacAddr=, and FolderShr= variable values need to be set with accurate values for this to work as expected—I tested and confirmed it did from my side.

@ECHO ON 

SET GatewayIP=192.168.1.254
SET GWMacAddr=e1-c3-5b-ed-4d-61
SET FolderShr=\\machinename\sharename

ping -n 02 %GatewayIP%
arp -a | findstr /c:"%GWMacAddr%"

IF NOT %ERRORLEVEL%==0 EXIT
IF NOT EXIST "%FolderShr%" EXIT

<Rest of batch logic here since both checks above passed>
EXIT


Further Resources

0

Get network connection GUID

This will get the GUID of a given network name. As the GUID is unique to every computer this GUID is not the GUID for the network rather the unique identifier the computer uses to identify the network. I'm currently unaware of the criteria Windows uses to identify the network but at the moment this satisfies my needs.In my case the prompt wouldn't be needed as the network name is a constant (only changes when/if the company changes name).

What this does is:

  1. Creates some temporary folders.
  2. Ask user for network name. Current network name can be found here:See network name here
  3. Get log entries containing the network name.
  4. Creates a VBS-script.
  5. Runs the VBS-script that will extract the GUID from the log entries and show a popup with the GUID.
  6. Clean up temporary files and folders.

Update: Lol. Forgot to paste the script. Here we go:

Script:

@ECHO OFF
CLS

REM Make temp folder to place temporary files in IF NOT EXIST "%tmp%\TKH\Mirror_Folders" MD "%tmp%\TKH\Mirror_Folders"

REM Ask user for name of network to find GUID for ECHO wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%tmp%\TKH\Mirror_Folders\NetworkName.vbs" FOR /f "tokens=* delims=" %%a IN ('cscript //nologo "%tmp%\TKH\Mirror_Folders\NetworkName.vbs" "Enter the name of the network" "Select network"') DO SET NetworkToSearchFor=%%a

REM Add 'Name'> to %NetworkToSearchFor% to filter out false results SET SearchString="'Name'^^^>%NetworkToSearchFor%"

REM Get log entry for networkname FOR /f "delims=" %%i IN ('wevtutil qe Microsoft-Windows-NetworkProfile/Operational /q:"Event[System[(EventID=10000)]]" /c:100 /rd:true /f:xml ^| FINDSTR /R "%SearchString%"') DO ( ECHO %%i>>"%tmp%\TKH\Mirror_Folders\Log entries.txt" ) REM Make VBS-script to get GUID ( ECHO. 'Creates local variables ECHO. Private Arg, objInputFile, tmpStr ECHO. 'Populate array with arguments ECHO. 'Arg^(0^): filepath^+filename ECHO. Set Arg = WScript.Arguments ECHO. ECHO. Set objInputFile = CreateObject^("Scripting.FileSystemObject"^).OpenTextFile^(Arg^(0^)^) ECHO. tmpStr = objInputFile.ReadLine ECHO. ECHO. FirstCharacterToGet = InStr^(tmpStr, "<Data Name='Guid'>"^)^+18 ECHO. NumberOfCharactersToGet = 38 ECHO. GUID = mid^(tmpStr, FirstCharacterToGet, NumberOfCharactersToGet^) ECHO. WScript.Echo GUID )>"%tmp%\TKH\Mirror_Folders\Get GUID.vbs"

REM Call VBS-script to get GUID FOR /F "usebackq tokens=*" %%r in (CSCRIPT &quot;%tmp%\TKH\Mirror_Folders\Get GUID.vbs&quot; &quot;%tmp%\TKH\Mirror_Folders\Log entries.txt&quot;) DO SET GUID=%%r ECHO %GUID% mshta "javascript:alert('Your networks GUID is:\n%GUID%');close()"

REM Clean up RD /Q /S "%tmp%\TKH

Save this as "get GUID.cmd"

Klinghust
  • 833