How to Effect Parent Scope with Cmdlet Executed in Module Function

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 months ago.

  • Author
    Posts
  • #75034

    Dakota Clark
    Participant

    I am trying to update the parameter completion options for some cmdlets by running a function in my module.
    An example of the code that is run:

    $completers = Microsoft.PowerShell.Management\Get-ChildItem -Path C:\example -Filter '*.Completer.ps1'
    foreach($item in $completers)
    {
        $message = 'Re-importing Completer: {0}' -f $item.FullName
        Microsoft.PowerShell.Utility\Write-Verbose -Message $message
        & $item.FullName
    }
    

    An example of the completer code:

    $param_name = 'UserPrincipalName'
    $cmdlets = Microsoft.PowerShell.Core\Get-Command -Module 'MSOnline' -ParameterName $param_name
    
    $argument_completer = @{
        CommandName = $cmdlets.Name
        ParameterName = $param_name
        ScriptBlock = {
            param($command_name, $parameter_name, $word_to_complete, $command_ast, $fake_bound_parameter)
    
            $item_list = MSOnline\Get-MsolUser | Microsoft.PowerShell.Core\Where-Object { $PSItem.DisplayName -match $word_to_complete } | Microsoft.PowerShell.Core\ForEach-Object {
                $completion_text = $PSItem.UserPrincipalName
                $tool_tip = 'The name of users in the selected tenant.'
                $list_item_text = $PSItem.DisplayName
                $completion_result_type = [System.Management.Automation.CompletionResultType]::ParameterValue
    
                [System.Management.Automation.CompletionResult]::new($completion_text,$list_item_text,$completion_result_type,$tool_tip)
            }
    
            return $item_list
        }
    }
    
    Microsoft.PowerShell.Core\Register-ArgumentCompleter @argument_completer
    

    If I run the code myself the change occurs however when I run the function that holds the code no change occurs.

    How can I get the function to update the parameter completion in the parent scope?

  • #75466

    Don Jones
    Keymaster

    Use Set-Variable and specify a scope?

You must be logged in to reply to this topic.