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
  • 773
  • 1
  • 7
  • 17
  • I thought I had a solution within reach when I found the registry keys for the network profiles in the registry. But my hopes were crushed as I found out I'm not allowed (probably by group policy) to query keys within HKEY_LOCAL_MACHINE\SOFTWARE – Klinghust Sep 22 '17 at 13:31
  • @Facebook Thanks for your reply! I have not completed this yet. I guess the default gateway pretty much stays static. How come??? :) I've found that `wevtutil` lists the GUID if not formated as text (ie. formated as XML). Working with strings in batch is such a pain so I'm working on a .vbs script to extract the GUID from the output of wevtutil. This vbs script I plan to create from the batch-script and delete once done. I'll post my solution as soon as I get time to finish it. – Klinghust Sep 30 '17 at 21:49
  • One more question, is the main and most important purpose of your task here to ensure ***"This task has to run only when connected to a specific network"***? Is that the main objective here regardless of the `UUID` you capture via some CLI and batch script or vbs script? I have a simple idea that may be able to help but I'd like to hear back from you first if possible. – Vomit IT - Chunky Mess Style Sep 30 '17 at 22:56
  • @Facebook Yes it is the main objective here as the script is supposed to mirror a user specified folder to a network share. If the script were to run on another network it could possibly mirror the folder to some strangers computer. – Klinghust Oct 01 '17 at 11:26
  • Okay, I have an idea I'll play with an confirm works as I'm thinking and then write up at answer for you at least. I assume this network share is only available on this network as some sort of UNC path such as `\\server\share` too correct? I'll test some ideas with batch to see if there can be a couple checks before running the script. – Vomit IT - Chunky Mess Style Oct 01 '17 at 13:28
  • @Facebook Correct. – Klinghust Oct 01 '17 at 17:46

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

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
  • Thanks for the detailed instructions! Neat approach. Only downside I can see is that whenever connected to another network the cmd window will briefly flash up on the screen while the checks are running. Also... I don't know why but the IP-address don't show up in `arp -a` even after pinging. – Klinghust Oct 05 '17 at 13:09
  • @Klinghust If the ping responds then the IP and MAC should show up but maybe ensure you run `cmd.exe` as administrator maybe. You can run this hidden in a couple different ways which I know how to do as well and neither are complex at all. Look over my answer here: https://superuser.com/questions/957267/how-to-disable-automatic-reboots-in-windows-10/1208716#1208716 and look at `#2` (create the vbs script), `#9`, `#10`, and `#11` (selecting Actions options) for my initial idea to run hidden since in Task Scheduler I think you have to have the option of `Run only when the user is logged on` – Vomit IT - Chunky Mess Style Oct 05 '17 at 13:34
  • If the ping does not respond, then the IP nor the MAC should show up from the behavior when I tested it so maybe it's run a run as admin issue if it's not simply working as expected and the DW you ping not responding? – Vomit IT - Chunky Mess Style Oct 05 '17 at 13:35
  • The ping command responds `Reply from 10.250.30.27: bytes=32 time=24ms TTL=121 Ping statistics for 10.250.30.27: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)` But still no trace of it in `arp -a`. Same results when running cmd as admin. – Klinghust Oct 05 '17 at 13:47
  • @Klinghust How's your network topography with this? Can you explain how the machine you run this from talks with this gateway for whatever hops it may need to make? Is this a physical machine or a virtual machine? I'll try to figure out another way but actually if the shared folder is only available when connected to this network versus other ones, then maybe you can simply just set and use the `IF NOT EXIST "%FolderShr%" EXIT` logic and not even worry about the arp cache. Perhaps the merely responding to the ping command with success and the share check would be better? – Vomit IT - Chunky Mess Style Oct 05 '17 at 14:04
  • I'm not sure about the network topology as I'm not in IT. But from what I can find using `tracert` it seems this server is located somewhere other than in house (8hops). My concern is that if someone connects to another network and there is a server by the same name the files would be copied to it. That's why I want the GUID as it is unique for the network. – Klinghust Oct 06 '17 at 10:38
  • @Klinghust If you create a hidden dummy share with a unique name and confirm it's available, then I would think that's suffice. Furthermore, whatever script you have the job execute, point that to a script on the network share as well so in case someone does happen to have all these in their environment they'd probably not have the script in the UNC path you use for it. So if these are local machine scripts that only run on the network, make those only accessible from that same network as well. If the script isn't available, the job will run but the script will not execute. – Vomit IT - Chunky Mess Style Oct 06 '17 at 12:42
  • @Klinghust As far as the UUID, GUID or whatever, I think Windows OS stores a name that's unique for it for the networks it connects to and this is only per machine. So the UUID is not really the network broadcasting this and rather the WIndows OS storing this and creating the UUID per machine per network so the same network UUID on PC1 and PC2 would not be the same ID number. I'll have to read up on it more but from what I gather with quick reading that seems to be the case. – Vomit IT - Chunky Mess Style Oct 06 '17 at 12:45
  • I cant use IP as a condition as I have no guarantee for it to stay the same as servers are exchanged once every now and then. I can use the name of the server as IT will always update the link in `net use` if the name changes. Your assumptions about GUID/UUID are as far as I have experienced correct. And that's why I need to get it automatically from the PC that runs the script that adds the scheduled task. – Klinghust Oct 06 '17 at 17:24
  • 1
    Are you running the batch script from task scheduler? If so, then rather than pointing the program/script to a local `C:\scripts\process.cmd` batch script, call it from `\\server\share\scripts\process.cmd`. The IP address of the DG shouldn't change that often I wouldn't think but server and PCs I understand are a different story. Certainly on another network with even the same IP address as the DG that is has and even if there's a server with the same share names setup, there likely won't be a batch script with the same name running the logic. I think a simple solution like this would be fine. – Vomit IT - Chunky Mess Style Oct 06 '17 at 19:36
  • I guess placing the script on the server would be a decent solution. I might actually go for that in the end as well as having the GUID inserted in the scheduled task :) Thanks for all your effort!! – Klinghust Oct 06 '17 at 22:04
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 "%tmp%\TKH\Mirror_Folders\Get GUID.vbs" "%tmp%\TKH\Mirror_Folders\Log entries.txt"`) 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
  • 773
  • 1
  • 7
  • 17