0

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?

2

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.