我想为我的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