PowerShell无法绑定ForegroundColor参数。

10

我创建了以下Powershell脚本,希望用它将文件复制到网络共享。

function Copy-Deploy
{
 param(
  [Parameter(Position=0,Mandatory=$true,HelpMessage="Path to scripts")]
  [Alias("pth")]
  [string]$ScriptPath,

  [Parameter(Position=1,Mandatory=$true,HelpMessage="Deployment script filename")]
  [Alias("dep")]
  [string]$PowershellDeploymentScript,

  [Parameter(Position=2,Mandatory=$true,HelpMessage="MSI filename")]
  [Alias("m")]
  [string]$MSI,

  [Parameter(Position=3,Mandatory=$true,HelpMessage="Filename of the MSBuild script (.btdfproj)")]
  [Alias("msb")]
  [string]$MSBuildScript,

  [Parameter(Position=4,HelpMessage="UNC path to target server folder")]
  [Alias("server")]
  [string]$TargetServerPath

  )

 $ErrorActionPreference="Stop"

 #Step 1 : copy the MSI, .btdfproj script and control powershell script to the remote server
 Write-Host " Going to enter the mis script block"
 $misScript =
 {
    Write-Host " Path+Filename = {0}({1})" -f $ScriptPath, $PowershellDeploymentScript
    Write-Host " Going to copy files" 
    Write-Host " ScriptPath = $ScriptPath"
    Write-Host " PowershellDeploymentScript = $PowershellDeploymentScript"
    Write-Host " MSI = $MSI"
    Write-Host " MSBuildScript = $MSBuildScript"
    Write-Host " TargetServerPath = $TargetServerPath"


    Copy-Item -Path "$ScriptPath" + "$PowershellDeploymentScript" -Destination "$TargetServerPath"
    Copy-Item -Path "$ScriptPath" + "$MSI" -Destination "$TargetServerPath"
    Copy-Item -Path "$ScriptPath" + "$MSBuildScript" -Destination "$TargetServerPath"
 }
 Invoke-Command -scriptblock $misScript

 #Step2 : Execute the powershell script ExecuteBizTalkAppMSI.ps1 remotely on the target server
 #using syntax... invoke-command -computer $MachineName -command { $TargetServerPath + ExecuteBizTalkAppMSI.ps1 }" 

}
我从Powershell ISE运行这个脚本,使用以下代码:
Copy-Deploy "C:\Builds\3\x.Int.MIS\SupportBTDF\Sources\x.Int.MIS\Dev\V1.0\Src\Solutions\MIS\x.Int.MIS.Deployment\" "ExecuteBizTalkAppMSI.ps1" "bin\debug\x.Int.MIS-3.0.0.msi" "x.Int.MIS.Deployment.btdfproj" "\\d-vasbiz01\BizTalkDeployment"

然后我遇到了以下错误:

Cannot bind parameter 'ForegroundColor'. Cannot convert value "C:\Builds\3\x.Int.MIS\SupportBTDF\Sources\x.Int.MIS\Dev\V1.0\Src\Solutions\MIS\x.Int.MIS.Deployment\,ExecuteBizTalkAppMSI.ps1" to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White".At C:\Builds\3\x.Int.MIS\SupportBTDF\Sources\x.Int.MIS\Dev\V1.0\Src\Solutions\MIS\x.Int.MIS.Deployment\CopyDeployScriptThenExecute.ps1:79 char:16

请问有人可以告诉我我错在哪里吗?

2个回答

26

这行代码:

Write-Host " Path+Filename = {0}({1})" -f $ScriptPath, $PowershellDeploymentScript

问题就在这里。

按照以下方式编写:

Write-Host (" Path+Filename = {0}({1})" -f $ScriptPath, $PowershellDeploymentScript)

之前-f被误认为是-ForegroundColor参数而非字符串格式运算符。


1

将消息用括号括起来。您还可以使用变量扩展并嵌入变量,而无需使用-format运算符(就像您在其他write-host调用中所做的那样):

Write-Host " Path+Filename = $ScriptPath($PowershellDeploymentScript)"

感谢 Shay。我从来没有在使用加号连接字符串方面有太多的运气,但也许当我在等号右侧使用它们时会出现问题。 - Rob Bowman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接