Comparison operators with UNC Path

Welcome Forums General PowerShell Q&A Comparison operators with UNC Path

This topic contains 2 replies, has 2 voices, and was last updated by

2 years ago.

  • Author
  • #63681

    Points: 55
    Rank: Member

    My task is to redirect share permission paths from within the Registry (Windows 2012 server) and I am having issues with isolating the path within a 'if' statement argument.

    Declare old and new path as a variable
    $RegPath Get a list of shares within the Registry

    $shareNames = Get a list of just ShareName folders from within the registry

    For Each Sharename in Sharename
    if (path statement contains the old path)
    Change to new path

    Commented out Registry assignment and just placed 'Write-Output' so I could immediately tell if my statement was ever 'true'.

    #Declare old path and new path
    $OldPath = "C:\utility\test"
    $NewPath = "C:\utility\test2"
    #Get List of Shares
    $RegPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares'
    #Get list of Directory Shares and pipe to 'for each object'
    $shareNames = dir -Path $RegPath | Select-Object -ExpandProperty Property
    foreach ($shareName in $ShareNames)
        $ShareData = Get-ItemProperty -Path $RegPath -Name $ShareName | Select-Object -ExpandProperty $ShareName
        if (($ShareData | Where-Object { $_ -contains  (“Path=$OldPath")}))
         # $ShareData = $ShareData -replace [regex]::Escape(“Path=$OldPath“), “Path=$NewPath“  
          #Set-ItemProperty -Path $RegPath -Name $ShareName -Value $ShareData
          Write-Output $sharename
        }#end of if statement
    }#end of For Each

    My Current Shares

    C:\Users\bclanton\Google Drive\Code\Projects\TP_RegEdit> Get-SmbShare
    Name     ScopeName Path                              Description    
    ----     --------- ----                              -----------    
    ADMIN$   *         C:\Windows                        Remote Admin   
    C$       *         C:\                               Default share  
    IPC$     *                                           Remote IPC     
    Portal   *         C:\Portal                                        
    print$   *         C:\Windows\system32\spool\drivers Printer Drivers
    Share1$  *         C:\utility\test2\share                           
    Share10$ *         C:\utility\test\share10                          
    Share2$  *         C:\utility\test\share2                           
    Share3$  *         C:\utility\test\share3                           
    Share4$  *         C:\utility\test\share4                           
    Share5$  *         C:\utility\test\share5                           
    Share6$  *         C:\utility\test\share6                           
    Share7$  *         C:\utility\test\share7                           
    Share8$  *         C:\utility\test\share8                           
    Share9$  *         C:\utility\test\share9    

    My problem seems to be that the 'true' statement is never launching. My troubleshooting steps so far:

      Stepped through code and verified the contents of $_ is what it should be
      Changed { $_ -contains (“Path=$OldPath")})) to { $_ -contains (“Path=c:\utility\test")})) which seems to work, however when I use a partial path { $_ -contains (“c:\utility\test")})), the statement is never 'true'. It seems that 'contains' is not what I want.

      I tried 'like'and 'match' and it also fails. Match fails with "Insufficient hexadecimal digits" error.

    Problem seems to be in my comparison operator?

  • #63688

    Points: 24
    Team Member
    Rank: Member

    Welcome to the Forums,

    The -contains operator only works if the left side of the comparison is an array and it doesn't do partial matches.

    You'll need to either use -eq, -like or -match.

    $path = 'c:\utility\test2'
    $path -eq 'c:\utility\test2'
    $path -like 'c:\utility\test*'
    $path -match '^c:\\utility\\test'

    However, you might want to ask yourself a couple of questions:
    – Why are you diving into the Registry on Windows Server 2012 with the SmbShare cmdlets being available to abstract the complexity of getting the share configuration, removing shares and creating new ones?
    – Is directly changing the Registry values of the shares supported by Microsoft? I'm pretty certain it is not supported and you should use Remove-SmbShare and New-SmbShare to re-create the share with a different path but the same access configuration which is not difficult to achieve.

  • #63721

    Points: 55
    Rank: Member

    I have a File Directory structure that contains more than 200+ shares on a file server for hosted client. The majority of the shares are for Home directories, Shared files.

    I received a directive that a holding company is being dissolved so root folders of this 'very large' tree will be changed. I need to test fully but at least from my Windows 10 laptop, I can modify this registry setting and adjust the path and all share permissions are changed, with a lanman service restart OR a reboot.

    Still researching but this seems to be an easier and 'ensured' method of getting permissions moved without removing and recreating a share.


The topic ‘Comparison operators with UNC Path’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort