The replace method worked in this code for two years, why did it stop? Am I missing an update? Did a security update kill it? I just want to know if it's me, or if by some fluke it worked to begin with.
The following is a slice of a script that uploads home directory files/folders to a secure site. It records/changes the file path in order to upload them to the appropriate location on the site, while maintaining the original file structure of the user's homedir.
#Mapped drive to overcome long file names
New-PSDrive -Name "X" -PSProvider FileSystem -Root "<server path to homedir root>"
$ADuser = Get-ADUser -Identity <username> -Server <domain> -Properties homeDirectory
#I know this looks weird. We have users who've changed their name, and their homedir folder wasn't
#renamed. Our server path up to the users original sAMAccountName is 41 characters. I've done this
#with the username typed in directly, and $aduser.samaccountname; test how you will, it doesn't
#matter:
$SourceFolderPath = "X:\$($ADuser.homedirectory.Substring(41))"
#I've typed this in directly, thinking that something was wrong with the property, or that it
#wasn't stored as a normal string, but it doesn't matter:
$rootPath = $ADuser.HomeDirectory
#This is just to setup the slice of code below that's part of a larger function
$Docs = $true
Get-ChildItem $SourceFolderPath -Recurse | % {
if ($_.PSIsContainer -eq $True) {
#I put my breakpoint here:
if($Docs) {
$folderUrl = $_.FullName.Replace($rootPath,"/Documents").Replace("\","/")
}
elseif($Website) {
$folderUrl = $_.FullName.Replace($SourceFolderPath,"/Website").Replace("\","/")
}
else {
$folderUrl = $_.FullName.Replace($SourceFolderPath,"").Replace("\","/")
}
if($folderUrl) {
#This is obviously a function that's not here, you shouldn't get here anyway, but I've
#commented it out.
#Ensure-Folder -Web $web -ParentFolder $list.RootFolder -FolderUrl $folderUrl
}
}
else{
#Alternatively, you could put a breakpoint here if you don't have/want any folders in your
#test. You're clearly missing the $list variable, but it doesn't matter for testing, and
#the replace method doesn't work here either:
if($Docs) {
$folderRelativeUrl = $list.RootFolder.ServerRelativeUrl + $_.DirectoryName.Replace($rootPath,"/Documents").Replace("\","/")
}
elseif($Website) {
$folderRelativeUrl = $list.RootFolder.ServerRelativeUrl + $_.DirectoryName.Replace($SourceFolderPath,"/Website").Replace("\","/")
}
else {
$folderRelativeUrl = $list.RootFolder.ServerRelativeUrl + $_.DirectoryName.Replace($SourceFolderPath,"").Replace("\","/")
}
}
}
I should also note, that it's only the first call of replace that doesn't work. The second call that replaces "\" with "/" still works. You can remove the second call, and the first still doesn't work. I've tested the replace method on folder/file.fullname outside of this loop and it works fine. It's only in this loop that it has stopped working. I've looked at object classes (string, as expected), verified that the variables contain what they're supposed to, tested on the server and a local desktop, and it just simply doesn't work anymore. The server is using PS version 5.1.14409.1027, and my desktop is on 5.1.19041.1682.
I do have a fix in place, I just want to know why this quit working, and maybe it will get some attention to be fixed in the future if it's not just something in my environment, or if I'm using the method incorrectly.
The fix for those interested is to use the -replace operator instead:
$folderUrl = $_.FullName -replace [regex]::Escape("$rootPath"),"/Documents" -replace "\\","/"