Forum Discussion
Message: There is an unterminated string literal at position 46 in ... (Special Charater the cause)
Hi,
I'm hoping someone can guide me on an issue I'm having with the error message "Message: There is an unterminated string literal at position 46 in ..." I know the cause but am unsure of the solution.
When I use get-team -DisplayName "Peer Tutoring - Metal Fabrication & Welding" the script exits with the above message when it gets to the & (special character). I'm actually passing a variable that contains "Peer Tutoring - Metal Fabrication & Welding" and other similar MS Teams names in a ForEach. So when my script hits one of these Teams names it throws the error and successfully moves on to other MSTeams names without the &.
Is there away to not have to rename all the Teams having this issue and still pass the Teams name with the & included?
$Courses = Import-Csv "C:\Scripts\PeerTutoring\Programs and Corresponding Teams for Peer Tutoring.csv"
ForEach ($Course in $Courses) {
$groupid = Get-Team -Displayname $Course.TeamName | select GroupId
}
Thanks in advance,
Randy
Hi, Randy.
Using web-specific special characters in resources never ends well, as you're seeing. Fortunately, there is a reasonably straight-forward solution as there's many ways to encode such strings.
Here's one that uses the [uri] class to escape the displayName properly.
Using your example:
# This: Get-Team -DisplayName "Peer Tutoring - Metal Fabrication & Welding"; # Becomes this: Get-Team -DisplayName ([uri]::EscapeDataString("Peer Tutoring - Metal Fabrication & Welding"))
Cheers,
Lain
2 Replies
- LainRobertsonSilver Contributor
Hi, Randy.
Using web-specific special characters in resources never ends well, as you're seeing. Fortunately, there is a reasonably straight-forward solution as there's many ways to encode such strings.
Here's one that uses the [uri] class to escape the displayName properly.
Using your example:
# This: Get-Team -DisplayName "Peer Tutoring - Metal Fabrication & Welding"; # Becomes this: Get-Team -DisplayName ([uri]::EscapeDataString("Peer Tutoring - Metal Fabrication & Welding"))
Cheers,
Lain
- RandyRiaukaCopper Contributor