2012-07-30 82 views
6

我已經創建了下面的Powershell腳本,我希望用它來將文件複製到網絡共享。Powershell無法綁定參數ForegroundColor

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 

誰能告訴我哪裏出了錯?

回答

15

線:

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

就是問題所在。

寫這樣的:

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

-f正在採取如超過字符串格式運算符的-ForegroundColor參數。

+0

這是偉大的感謝manojlds – 2012-07-30 14:52:14

1

將消息包裝在parens中。您還可以使用變量擴展和嵌入變量不使用-format操作(就像你在其他寫主機呼叫一樣):

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

由於吉文。我從來沒有使用+來連接字符串的運氣很多,但也許我在等號的右側使用它們時遇到了這個問題 – 2012-07-30 14:54:36

相關問題