2012-07-30 31 views
6

Tôi đã tạo tập lệnh Powershell sau đây mà tôi hy vọng sẽ sử dụng để sao chép tệp vào một mạng chia sẻ.Powershell không thể liên kết tham số 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 }" 

} 

tôi chạy kịch bản này từ Powershell ISE với dòng sau:

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" 

sau đó tôi nhận được lỗi sau:

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 

thể ai vui lòng cho tôi biết nơi tôi đã đi sai ?

Trả lời

15

Dòng:

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

là vấn đề.

Viết nó như thế này:

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

Các -f đã được thực hiện như là tham số hơn các nhà điều hành định dạng chuỗi -ForegroundColor.

+0

Đó là nhờ lớn manojlds –

1

Gói tin nhắn trong các lần parens. Bạn cũng có thể sử dụng mở rộng biến và nhúng các biến mà không sử dụng các nhà điều hành -format (như bạn đã làm trong các cuộc gọi ghi-host khác):

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

Cảm ơn Shay. Tôi không bao giờ có nhiều may mắn với việc sử dụng một chuỗi + để nối, nhưng có lẽ tôi đã nhận được vấn đề đó khi sử dụng chúng ở phía bên tay phải của dấu bằng –

Các vấn đề liên quan