Forum Discussion

Marceepoo's avatar
Marceepoo
Copper Contributor
Nov 16, 2023

Code func, fnLn so that write-host "fnLn $(varA) = $varA" produces: 'Line No. 5: $(varA) = ABC'

I coded a PowerShell (PS) function as follows on lines 1 -4.  On line 5 of the PS script, I inserted the write-host statement(?)/command(?) shown below. [i.e., I don't know which term I should use here, statement or command, or perhaps some other word]:

    function fnLn {
        $MyInvocation.ScriptLineNumber
        #Write-Output $MyInvocation
    }
    write-host "fnLn $(varA) = $varA"

I want to learn how to modify the function, fnLn, so that if I run the PS script in the Windows PowerShell ISE, the following output will appear in the console:

    Line No. 5: $(varA) = ABC

If it's not too much trouble, I'd like any tips to find URLs where I could learn about how to solve this problem.   Thank you for any tips you can give me. 

                    Marc

  • Marceepoo 

     

    While you can't produce the kind of output you're after inline, if you're happy calling out to a utility function to display the information, you can get a little closer.

     

    In the example below, I've using the helper function, Write-VariableDetail, to output the information you originally asked for. As you can see though, you do have to provide parameters to the helper function as there's no way of implicitly knowing what the variable's name and value are otherwise.

     

    Write-VariableDetail will work okay for simple variable types like string, int, Boolean, etc. but I haven't put any effort into "nicely" displaying complex types or arrays. PowerShell will default to a ToString() method if the object has one, and if not, it'll simply display the type name - which will look strange.

     

    Example

     

    function Write-VariableDetail()
    {
        [cmdletbinding()]
        param(
            [parameter(Mandatory=$true)] [string] $Name
            , [parameter(Mandatory=$true)] [string] $Value
        );
    
        Write-Information -MessageData "Line $($MyInvocation.ScriptLineNumber): `$$Name = $Value" -InformationAction:Continue;
    }
    
    function Invoke-MyAmazingFunction()
    {
        $Variable1 = "Whazzup!";
        $Variable2 = "Nutin.";
    
        Write-VariableDetail -Name "Variable1" -Value $Variable1;
        Write-VariableDetail -Name "Variable2" -Value $Variable2;
    }

     

     

    Output

     

     

    If you don't want to use the helper function approach then that takes you back to my first reply.

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Marceepoo 

     

    Hi, Mark.

     

    I'm unaware of a way to produce the current line number (deliberately excluding breakpoints as that interferes with execution) within the executing function, so with respect to including it in your Write-Host statement: you can't (as far as I'm aware).

     

    With respect to the other content, I have a question first: Are you passing a parameter to fnLn? I ask as I can't see one defined, but in order for your Write-Host from within the function to make sense, I'm assuming you are intending to pass one.

     

    If you are passing in a parameter (which you can still do even though no parameters are defined, it's just not obvious with nothing defined that that is the intended context) then you can produce something similar to the remaining expected output like this:

     

    Example

    function fnLn {
        if ($args)
        {
            for ($index = 0; $index -lt $args.Count; $index++)
            {
                Write-Host -Object "`$args[$index] = $($args[$index])";
            }
        }
    }

     

    Output

     

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Marceepoo 

     

    While you can't produce the kind of output you're after inline, if you're happy calling out to a utility function to display the information, you can get a little closer.

     

    In the example below, I've using the helper function, Write-VariableDetail, to output the information you originally asked for. As you can see though, you do have to provide parameters to the helper function as there's no way of implicitly knowing what the variable's name and value are otherwise.

     

    Write-VariableDetail will work okay for simple variable types like string, int, Boolean, etc. but I haven't put any effort into "nicely" displaying complex types or arrays. PowerShell will default to a ToString() method if the object has one, and if not, it'll simply display the type name - which will look strange.

     

    Example

     

    function Write-VariableDetail()
    {
        [cmdletbinding()]
        param(
            [parameter(Mandatory=$true)] [string] $Name
            , [parameter(Mandatory=$true)] [string] $Value
        );
    
        Write-Information -MessageData "Line $($MyInvocation.ScriptLineNumber): `$$Name = $Value" -InformationAction:Continue;
    }
    
    function Invoke-MyAmazingFunction()
    {
        $Variable1 = "Whazzup!";
        $Variable2 = "Nutin.";
    
        Write-VariableDetail -Name "Variable1" -Value $Variable1;
        Write-VariableDetail -Name "Variable2" -Value $Variable2;
    }

     

     

    Output

     

     

    If you don't want to use the helper function approach then that takes you back to my first reply.

     

    Cheers,

    Lain

Resources