2014-12-08 52 views
0

我想为我的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值返回给文件。我们不能将输出作为管道发送到另一个自定义函数吗?

+1

'Write-Output'是正确的,它会发送'$ guid'到管道中。 – 2014-12-08 10:33:27

+0

它看起来像你依靠参数绑定。您可以使用['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

回答

2

几个问题:

注,而不是ValueFromPipelineValueFromPipelinebyPropertyName

Function GetGUIDFrom-Wix { 

    [CmdletBinding()] 
    [OutputType([string])] 
    Param 
    (
     # Param1 help description 
     [Parameter(Mandatory=$true, 
       ValueFromPipeline=$True, 
       Position=0)] 
     [ValidateScript({Test-Path $_ })] 
     [string] $Path 
) 

也为功能SetGUIDTo-Wix

Function SetGUIDTo-Wix { 

    [CmdletBinding()] 
    [OutputType([string])] 
    Param (
    [Parameter(Mandatory=$true, 
       ValueFromPipeline=$false, 
       Position=0 
       )] 
    [ValidateScript({Test-Path $_ })] 
    [string] $Path, 
    [Parameter(Mandatory=$true, 
       ValueFromPipeline=$True, 
       Position=1 
       )] 
    [string] $Guid 

    ) 

而且

GetGuidFrom-Wix -Path $Path | SetGuidTo-Wix -Path $Path 
1

这里的问题是你要传递一个数组到SetGUIDTo-Wix,而不是一个对象。

我看到您使用的是ValueFromPipelineByPropertyName,它正在通过流水线对象查看具有该名称的任何属性 - 由于该数组没有很好的格式化属性(只是一组值),所以无法从它。

有两种可能的方法来快速纠正 -
1.确保GetGUIDFrom-WIX是返回一个对象,具有GUID属性,或
2.使用ValueFromPipeline,而不是ValueFromPipelineByPropertyName。

相关问题