Samselvaprabu
Samselvaprabu

Reputation: 18137

How to pipeline the output of one function to another function in powershell?

I am trying to set a new guid for my wix project . I would like to acheive this by using pipelined functions.

I have written the following functions

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 
    }

}

I thought that the funciton GetGUIDFrom-Wix will be return the guid to pipeline and SetGUIDTo-Wix will receive pipelined guid and set it in file.

Following is my function call

$Path = E:\MyWixproj.wxs
GetGuidFrom-Wix -Path $Path | SetGuidTo-Wix -Path $Path -guid $_

But the result says

SetGUIDTo-Wix : Cannot bind argument to parameter 'Guid' because it is an empty string.

If i simply execute GetGUIDFrom-Wix -Path $Path | Out-File c:\test.txt ,it returns the GUID value to the file. Can't we send the output as pipeline to another custom function?

Upvotes: 0

Views: 4186

Answers (2)

James Ruskin
James Ruskin

Reputation: 990

The issue here is that you're passing an array to SetGUIDTo-Wix, instead of an object.

I see that you're using ValueFromPipelineByPropertyName, which is looking through the pipelined object for any property with that name - as the array has no nicely formatted properties (just a set of values), it's not able to assign anything from it.

There are two possible methods to quickly correct that -
1. Make sure that GetGUIDFrom-WIX is returning an object, with GUID property, or
2. Use ValueFromPipeline, instead of ValueFromPipelineByPropertyName.

Upvotes: 1

Micky Balladelli
Micky Balladelli

Reputation: 9991

A couple of issues:

Note ValueFromPipeline instead of ValueFromPipelinebyPropertyName

Function GetGUIDFrom-Wix {

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

Also for function 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

    )

And

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

Upvotes: 3

Related Questions