Search code examples
batch-filecmdwmic

WMIC query to derive absolute share paths on LAN in Windows 10


I'm trying to derive an absolute or full local path from a network path of a folder on a PC drive on LAN using WMIC or similar network tool run from a batch. A few threads devoted to similar tasks on this site when tested don't offer working solution in Win 10.

For example, when running a suggested in one answer query in Win 10 Cmd, I got:

C:\WINDOWS\system32>wmic share where "name='\\Laptop\Data'" get path
Node - OFFICE
ERROR:
Description = Invalid query

C:\WINDOWS\system32>wmic share where "name='Data'" get path
No Instance(s) Available.

I need this result: K:\Data , where K:\ is a hard drive of the remote PC on LAN, and Data is shared folder on that drive.

Can someone suggest a working query & batch for that task? WMIC documentation is way too extensive to derive a working query by trial-and-error without significant experience in using the tool.


Solution

  • Suggested in the thread techniques worked with certain additional actions.

    When using WMIC, I had to add local admin account to WMI Control Security property. As well, by running GPEDIT.msc enabled "Allow inbound remote administration exceptions" to Firewall rules, despite Firewall was disabled. The proper query is below, processing it's output required a batch file similar in approach to PsExec batch:

    wmic /user:[username] /password:[password] /node:"PC-ID" share get
    
    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    for /f "tokens=1,2 skip=1 delims=:" %%u in ('wmic /user:[username] /password:[password] /node:"Laptop" share get') do @(
        set "var1=%%u" & set "var2=%%v"
        set "var1.1=!var1:~89,-1!" & set "var2=!var2:~0,33!" & set "var1.2=!var1:~97!" & set "var1.3=!var1.1:~0,4!"
        if not "!var1.3!"=="IPC$" if not "!var1.1!"=="" echo \\Laptop\!var1.1! = !var1.2!:!var2!) 
    exit /b
    
    ::Output
    \\Laptop\ADMIN$   = C:\WINDOWS
    \\Laptop\C$       = C:\
    \\Laptop\D$       = D:\
    \\Laptop\Data     = K:\
    \\Laptop\K$       = K:\
    \\Laptop\Docs     = K:\Other\Docs
    \\Laptop\print$   = C:\windows\system32\spool\drivers
    

    When using PsExec instead of WMIC, I had to install it first, then add an extra key LocalAccountTokenFilterPolicy in Registry, then modify the command posted earlier:

    @echo off
    for /f "tokens=1,2" %%u in ('psexec64 -u [username] -p [password] \\Laptop cmd /c net share 2^>nul') do @(
        for /f "tokens=1,2 delims=:" %%x in ("%%u %%v") do @(
            if not "%%y"=="" echo \\Laptop\%%u = %%v ) )
    exit /b
    
    ::Output
    \\Laptop\C$ = C:\
    \\Laptop\D$ = D:\
    \\Laptop\print$ = C:\windows\system32\spool\drivers
    \\Laptop\K$ = K:\
    \\Laptop\ADMIN$ = C:\WINDOWS
    \\Laptop\Data = K:\
    \\Laptop\Docs = K:\Other\Docs