我想爲我的wix項目設置一個新的guid。我想通過使用流水線功能來實現這一點。如何在PowerShell中將一個函數的輸出傳遞給另一個函數?
我寫了下面的功能
Function GetGUIDFrom-Wix {
[CmdletBinding()]
[OutputType([string])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateScript({Test-Path $_ })]
[string] $Path
)
Begin
{
$xmldata = New-Object XML
$xmldata.Load($Path)
}
Process
{
$Guid = $xmldata.Wix.Product.Id
}
End { Write-Output $Guid }
}
Function SetGUIDTo-Wix {
[CmdletBinding()]
[OutputType([string])]
Param (
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=0)]
[ValidateScript({Test-Path $_ })]
[string] $Path,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[string] $Guid
)
Begin
{
$xmldata = New-Object XML
$xmldata.Load($Path)
}
Process
{
If ($Guid -eq $xmldata.Wix.Product.Id) {
$NewGuid = [string] $NewGuid = [string] [guid]::newguid().tostring().toUpper()
$xmldata.Wix.Product.Id = $NewGuid
}
Else {
Write-Error "Guid is not matching. Can't set new guid"
}
}
End {
$xmldata.Save($Path)
return $NewGuid
}
}
我認爲功能可按GetGUIDFrom - 維克斯將返回GUID來管道和SetGUIDTo - 維克斯將收到流水線GUID,並設置文件。
以下是我的函數調用
$Path = E:\MyWixproj.wxs
GetGuidFrom-Wix -Path $Path | SetGuidTo-Wix -Path $Path -guid $_
但結果說
SetGUIDTo - 維克斯:無法綁定參數參數「的Guid」,因爲它是 一個空字符串。
如果我只是執行GetGUIDFrom-Wix -Path $ Path | Out-File c:\ test.txt,它將GUID值返回給文件。我們不能將輸出作爲管道發送到另一個自定義函數嗎?
'Write-Output'是正確的,它會發送'$ guid'到管道中。 – 2014-12-08 10:33:27
它看起來像你依靠參數綁定。您可以使用['Trace-Command'] [檢查PowerShell如何綁定您的應用程序(請參閱http://technet.microsoft.com/library/db7c9374-998e-44c3-ad94-e0445176cf7b(v = wps.630).aspx)管道參數](http://blogs.technet.com/b/heyscriptingguy/archive/2014/12/04/trace-your-commands-by-using-trace-command.aspx) – alx9r 2014-12-08 21:47:10