I’m working on creating a simple script that converts folder names and paths to AD security group names and I’m bumping into a slight problem.
Basically I want to strip special characters from the folder paths and that works fine, however the one thing I’m not getting is converting backslashes ‘’ to dots ‘.’
The function below does everything I want, but the last part.
function Convert-SpecialCharacter {
param (
[parameter(mandatory=$true)]
[string]$Directory
)
$substitutions = @{
"'" = "";
' ' = '';
'-' = '';
'/' = '';
',' = '';
'&' = '';
'é' = 'e';
'\.' = '';
'\s' = '';
}
foreach ($s in $substitutions.Keys) {
$Directory = $Directory -replace $s, $substitutions[$s]
$Directory = $Directory -replace '[\\/]','.'
}
return $Directory
} # end function Convert-SpecialCharacter
I initially had the line $Directory = $Directory -replace '[\\/]','.'
in the $substitutions hash table as simply '\\' = '.';
but it didn’t work in there either and I thought it might be that it and the '\.' = '';
clashed even though I’d ordered the hash table so it should remove existing ‘.’ characters first and then do the backslash to dot conversion afterwards.
But even separating it out it doesn’t work as I wish. Basically any non-alphanumeric character I put in as a replacement gets ignored and just becomes
This path ‘UTB_Rektorer\Fsk omr. 3’ becomes ‘UTB_RektorerFskomr3’ instead of ‘UTB_Rektorer.Fskomr3’.
I’ve tried escaping the dot character in a number of ways: '\.', '[\.]' or '[.]'
. I get a lot of “interesting” strings from that, but never the result I want.
I’ve also tried putting in other special characters such as ‘-’ or ‘,’ but I get the same result as with the dot-character.
Using alphanumeric characters on the other hand works just fine, using ‘aaa’ returns the string ‘UTB_RektoreraaaFskomr3’
I’ve tried googling it and I’m finding lots of results for replacing/removing a special character, but none for having it as the replacement itself.
So help me Obi-Wan Kenobi, you’re my only hope!?