A friend had asked for some assistance with a script that was adding objects into Active Directory. It was failing to create Active Directory groups – “Invalid DN Syntax” was the descriptive message we received.
However the DN syntax appeared to be valid:
And when we copied and pasted the text from $groupname it worked fine…
So what was the difference?
To find if the strings were different we did a compare:
OK, trimmed they match. But when we surrounded $groupname with exclamation points there weren’t any spaces or tabs…
To find out why we check the string at byte level. To do this we used:
We can see the broken one has a carriage return at the end ( In PowerShell “`r” )
The problem was a file had been retrieved split by line feed ( In PowerShell “`n”) But Windows text files usually end in Carriage Return + Line Feed (“`r`n”) It is the line feed that moves text onto the next line, so that is why we didn’t see any spaces when printing string in PowerShell. PowerShell just doesn’t display the unprintable characters.
In the days of VBScript I found similar issues with tabs in the text file. In the case of VBScript the Trim function doesn’t remove these.