none
Finding the UNC path of a symlink'ed directory RRS feed

  • Question

  • Hi Scripting Guys,

    I have multiple powershell scripts which access data files on a remote networked machine working through a symlink on the local machine. This way the remote fiile location isn't coded in the scripts, and if we decide to change the location of the data then only the symlink needs to be recreated. So, for example, "c:\my_scripting\my_symlink\" in fact takes you to "\\remote_machine\my_data\"

    In a script I am working on now, I have a need to be able to retrieve the UNC path of the remote data directory, in other words - where is my_symlink pointing to?

    Can you help me with this please?

    Cheers,

    Colin

    Thursday, May 22, 2014 12:01 AM

Answers

  • Here's my solution so far (assuming only one reparse point in the local starting directory):

    # get the remote path associated with my_symlink
    [string] $uncPath = &cmd /C dir /AL *
    $uncPath = $uncPath.Split("[`[`]]")[1]

    # get the logical disk object
    $networkDrive = $uncPath.SubString(0,2)
    $logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$networkDrive'"

    # replace the network drive letter with provider name
    $uncPath = $uncPath.Replace($networkDrive, $logicalDisk.ProviderName)

    Thursday, May 22, 2014 9:50 PM

All replies

  • Not really a scriping issue.  Just use the CMD command dir.

    dir /AL /S C:\

    /AL says display attribute "reparse points"



    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 12:11 AM
  • Thanks for that. I think I can put it together with a get-wmiobject call to derive the actual unc path.
    Thursday, May 22, 2014 12:37 AM
  • Here's my solution so far (assuming only one reparse point in the local starting directory):

    # get the remote path associated with my_symlink
    [string] $uncPath = &cmd /C dir /AL *
    $uncPath = $uncPath.Split("[`[`]]")[1]

    # get the logical disk object
    $networkDrive = $uncPath.SubString(0,2)
    $logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$networkDrive'"

    # replace the network drive letter with provider name
    $uncPath = $uncPath.Replace($networkDrive, $logicalDisk.ProviderName)

    Thursday, May 22, 2014 9:50 PM
  • Excellent.  I was wondering if you would be able to extract it easily.  Looks like it was pretty easy.

    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 10:03 PM