As usual, the ideas for the posts start as a question from a colleague or a customer.

In this case, the customer had to distribute differently the folders in his FileServers’ volumes.

He had added an additional drive, copied all the relevant folders, and now needed to change the shares (non cluster resources) settings.

If there were only two or three, there is no problem doing it “manually”, but when you have more than 150 of them… it’s PowerShell time!

All the shares information and settings are stored in the registry, under the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesLanmanServerShares key (as described briefly in KB125996).

Each value represents a share, and the mutli-string value data contains the settings for it.

So by looping on each value, searching for the relevant folder in the value-data text (e.g. “Path=C:myOldSharePath”) and replacing it with the new value, we could easily change them all in a script.

This led to the PowerShell function below:

function Change-SharePath {
    [cmdletbinding(SupportsShouldProcess=$true)]
    param(
        $OldPath,
        $NewPath
    )
 
    $RegPath = ‘HKLM:SYSTEMCurrentControlSetServicesLanmanServerShares’
    dir -Path $RegPath | Select-Object -ExpandProperty Property | ForEach-Object {
        $ShareName = $_
        $ShareData = Get-ItemProperty -Path $RegPath -Name $ShareName |
                Select-Object -ExpandProperty $ShareName
        if ($ShareData | Where-Object { $_ -eq  “Path=$OldPath}) {
            $ShareData = $ShareData -replace [regex]::Escape(“Path=$OldPath), “Path=$NewPath
 
            if ($PSCmdlet.ShouldProcess($ShareName, ‘Change-SharePath’)) {
                Set-ItemProperty -Path $RegPath -Name $ShareName -Value $ShareData
            }
        }
    }
}
 

Where we would use it:

– With the -WhatIf parameter for testing purposes:

Change-SharePath -OldPath D:myShare -NewPath E:SubfoldermyNewSharePath -WhatIf

– With the -Verbose parameter for details during the changes:

Change-SharePath -OldPath D:myShare -NewPath E:SubfoldermyNewSharePathVerbose
 

And don’t forget to restart the Server service (aka lanmanserver) after all the changes, for the new settings to take effect:

Restart-Service -Name lanmanserver
 

HTH,

Martin.

Advertisements