0

I have this script which runs fine with no issue on the server locally but when I made a task in Team Foundation Server(update2017) and run it from there it throws an error, the error is after the script for reference.

 param(
 [string]$ServiceNames
)
if([string]::IsNullOrWhiteSpace($ServiceNames))
{
   throw "Missing argument [-ServiceNames $ServiceNames]"
}
    $Services=$ServiceNames.Split(",")
    foreach($Service in $Services)
{
   if(Get-Service $Service | Where {$_.status –eq 'Stopped'})
{
   Get-Service $Service | Where {$_.status –eq 'Stopped'} | Start-Service
   Write-Host "$Service has been started."
}
else
{
   Write-Host "$Service is already running."
}
}

and this error came.

if(Get-Service $Service | Where {$_.status â?"eq 'Stopped'})

Unexpected token 'â?"eq 'Stopped'})

Thanks in advance.

11
  • This is an encoding issue. Is your script saved in UTF8-BOM? Commented Jul 11, 2018 at 14:48
  • I am not sure but in case if it's not how to do that? Commented Jul 11, 2018 at 14:49
  • 1
    Sorry, the ISE is deprecated and limited. I don't see a way to change the encoding from it. I'd advise you to move to VSCode with the PowerShell extension as that's MSFT's direction for creating powershell scripts. From there, you can update a file's encoding in the bottom right. You'll want to convert the file to UTF8 with BOM. Also, you're using long-dashes instead of short in -eq, which is the cause of your encoding issue (charcode 8211 vs charcode 45). Did you copy/paste this code from an email or something that auto-converts dashes? Commented Jul 11, 2018 at 14:57
  • 1
    As an extra piece of information, Windows PowerShell's engine parses everything as "ANSI" unless it's encoded as "UTF8 with BOM". Commented Jul 11, 2018 at 15:05
  • 1
    Another extra piece of information: Microsoft Office packages (as Word and Outlook) tend to automatically change dashes (from charcode 45 to 8211) around phrases. I recommend to avoid these applications for copying PowerShell scripts (or at least use them with care). Commented Jul 11, 2018 at 15:28

1 Answer 1

1

Yes, copy/Paste from Word or Outlook always inserts characters you don't want in the editor. For that i have put below function in my Powershell profile file.

This is not meant as a direct answer to this question because TheIncorrigible1 already gave that. It may however help others.

function Editor-ReplaceSmartQuotes {
    ## this function replaces "smart-qoutes" and long dashes you get 
    ## when pasting from Word into normal straight characters (" ' -)
    $text = Editor-GetSelectedText
    $psISE.CurrentFile.Editor.InsertText(($text -creplace '[\u201C\u201D\u201E\u201F\u2033\u2036]', '"' `
                                                -creplace "[\u2018\u2019\u201A\u201B\u2032\u2035]", "'" `
                                                -creplace "[\u2013\u2014\u2015]", "-"))
}

and added it to my ISE menu with:

Editor-AddMenu "Replace Smart_Quotes in Selection" {Editor-ReplaceSmartQuotes} "Alt+Q"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.