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.
PseudoCode
Declare old and new path as a variable
$oldPath
$newPath
$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?