Search code examples
powershellmodulescopepowershell-3.0

Can't return object from powershell module function


OK, I looked, and read, then looked some more... For the life of my I cannot figure out why my object will not return from my function! This is contained in a module within a separate file being called from another script. Everything is assigned properly BUT once this function loses scope so do the variables... How do I return the wiStore object OR set it so it does not lose scope after words.

Edit: Please note that nothing is returned, I can see the variable set in the debugger but magically it goes null after the function has finished executing.

function Get-WiStore
{
    param
    (
        $CollectionUrl
    )

    Add-TfsReferences
    $tfsUri = New-Object System.Uri $CollectionUrl
    $tfProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $tfsUri
    #WHY DO YOU DISAPEAR!!!!?!?!?!?!?!?!?! :'(
    $wiStore = New-Object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore $tfProjectCollection
}



function Add-TfsReferences
{
    #my *.dll references       
}

Solution

  • I have solved the issue myself, the problem was my references were failing but power shell failed to notify me. If you require references for a module then you should create a Module Manifest. Also, I removed the variable $wiStore and simply returned a new object.

    function Get-WiStore
    {
        param
        (
            $CollectionUrl
        )
    
        $tfsUri = New-Object System.Uri $CollectionUrl
        $tfProjectCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection $tfsUri
        New-Object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore $tfProjectCollection
    }
    

    Please see an answer to my other question for more details: https://stackoverflow.com/a/38246932/3812871