none
Local To UNC Path RRS feed

  • Question

  • Hi Scripting guys

    as part of a script Im putting together I am looking to use powershell to return a UNC path to a file or folder

    when given a remote server name and local path.

    example

    ServerName, %SystemDrive%\Log  would return \\servername\c$\log

    Can you point me in the right direction

    Thanks

    Thursday, November 3, 2011 6:35 PM

Answers

All replies

  • Hi,

    You can use WMI class Win32_Share to map share to local path and Win32_Enviroment class to resolve environment variables. With this information you can build UNC path. I hope you are familliar with WMI.

    Best regards!


    http://www.purgar.net
    • Edited by Jure Purgar Thursday, November 3, 2011 6:48 PM
    Thursday, November 3, 2011 6:47 PM
  • I used something like this in one of my scripts:

    $uncpath=$localpath.Replace(":", "$") # replaces the ":" with a "$" in a local path (e.g. C:\temp => C$\temp)

     
    $uncpath=$uncpath.Insert(0, "\\"+$MachineName+"\") # adds \\$maschinename\ (e.g. C$\temp => \\$maschinename\C$\temp)

    Is this what you're looking for?



    • Edited by Thomas Biebl Friday, November 4, 2011 8:23 AM
    • Proposed as answer by scorpiotek Wednesday, October 3, 2012 9:13 PM
    Friday, November 4, 2011 8:22 AM
  • This ought to do it:

    $Server = "ServerName"
    $SystemDrive = (Get-WMIObject -Class Win32_OperatingSystem -Computername $Server).SystemDrive
    $UNCPath = "$Server`\$($SystemDrive.Replace(':','$'))`\Log"

    Karl 


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Friday, November 4, 2011 10:39 PM