2014-12-08 28 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。

相關問題