首页 > 解决方案 > TFS-Powershell 脚本

问题描述

我有这个脚本,它在本地服务器上运行良好,没有问题,但是当我在 Team Foundation Server(update2017) 中创建任务并从那里运行它时,它会引发错误,错误在脚本之后以供参考。

 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."
}
}

这个错误来了。

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

意外的令牌 'â?"eq 'Stopped'})

提前致谢。

标签: powershellencoding

解决方案


是的,从 Word 或 Outlook 复制/粘贴始终会在编辑器中插入您不需要的字符。为此,我在我的 Powershell 配置文件中添加了以下函数。

这并不意味着直接回答这个问题,因为TheIncorrigible1已经给出了答案。然而,它可能会帮助其他人。

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]", "-"))
}

并将其添加到我的 ISE 菜单中:

Editor-AddMenu "Replace Smart_Quotes in Selection" {Editor-ReplaceSmartQuotes} "Alt+Q"

推荐阅读