TFS-Powershell Scripting

2019-08-24 12:23发布

问题:

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.

回答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"