1

I have a 2 PS function that show certain processes and handles. I also have write-output command to show which is process and which are handles. How can prevent this write-output from popping up before the output of the function is ready to be shown ?

##Functions
# -------------------------------------------------------------------------------------------------------
function show-process($PName){
    Write-Output "The running $PName processes are:"
    Get-Process -Name $PName | select ProcessName, @{N='Session ID'; E={$_.SI}}, @{N='Process ID'; E={$_.ID}}, Handles
}

function show-handles($HPath){
    Write-Output "Handles that have files open in $HPath : "
    handle $HPath 
}

# -------------------------------------------------------------------------------------------------------
##Start
# ------------------------------------------------------------------------------------------------------
show-process -PName $CLREXE

show-handles -HPath $CLRDIR

Currently i got this one: The write-output is shown at the right place. But it doesn't wait for the output from the function. It just displays the write-output as soon as press enter. But i want it to wait for the output and then display the write-output together with the function's output. enter image description here

@cid your thinking give this output. enter image description here

But i want to have like this enter image description here

Keeran
  • 302
  • 1
  • 15
  • just place your `write-output` at the end of the functions – Cid Sep 20 '21 at 14:17
  • That won't help as it displays the write-output underneath my output. I want to have it above my output. – Keeran Sep 20 '21 at 14:20
  • Then, it's at the good place if you want it before... I'm not sure to understand your question, can you show us the current output and the expected one? – Cid Sep 20 '21 at 14:22
  • yessir, look at the post – Keeran Sep 20 '21 at 14:28
  • Does this answer your question? [Usage of Write-Output is very unreliable compared to Write-Host](https://stackoverflow.com/questions/59220186/usage-of-write-output-is-very-unreliable-compared-to-write-host) – iRon Sep 20 '21 at 14:57

1 Answers1

1

Firstly, functions return values (or objects to be more precise). They shouldn't write the output as well. Secondly, you need to capture the output of the cmdlets to variables instead to control when to display the result.

##Functions
# ---------------------------------------------------------------------------------
function get-myprocess($PName){
    Get-Process -Name $PName | 
        select ProcessName, 
            @{name = 'SessionID'; exp = {$_.SI}}, 
            @{name = 'ProcessID'; exp = {$_.ID}}, 
            Handles
}

function get-filehandle($HPath){
    handle $HPath 
}

# --------------------------------------------------------------------------------
##Start
# --------------------------------------------------------------------------------
$process = get-myprocess -PName $CLREXE

"The running $CLREXE processes are:"
$process

$handle = get-filehandle -HPath $CLRDIR

"Handles that have files open in $CLRDIR :"
$handle

or, a better way of keeping the functions from leaking output during execution...

##Functions
# ---------------------------------------------------------------------------------
function get-myprocess($PName){
    $result = Get-Process -Name $PName | 
        select ProcessName, 
            @{name = 'SessionID'; exp = {$_.SI}}, 
            @{name = 'ProcessID'; exp = {$_.ID}}, 
            Handles
    return $result
}

function get-filehandle($HPath){
    $result = handle $HPath
    return $result
}

# --------------------------------------------------------------------------------
##Start
# --------------------------------------------------------------------------------
"The running $CLREXE processes are:"
get-myprocess -PName $CLREXE

"Handles that have files open in $CLRDIR :"
get-filehandle -HPath $CLRDIR
Dennis
  • 871
  • 9
  • 29
  • thx for your time. The code looks better. However, get-myprocess waits until get-filehandle has done searching for handles. I thought, there should be someways to run get-myprocess, display the result and then search for handle with get-filehandle and then display the result of get-filehandle. I am not sure, if it is good idea, but i just wonder if it would work like that. Anyways thank you and i gunna accept this as answer, as i like the explanation. – Keeran Sep 21 '21 at 14:09
  • No worries. Just change the order of execution... (Answer edited) – Dennis Sep 21 '21 at 14:15
  • Added a better way to contain the output of the functions. – Dennis Sep 21 '21 at 14:28
  • Corrected the examples to use the correct variables. Variables used within the scope of a function can't be referenced from the outside. (However, the other way around applies.) – Dennis Sep 21 '21 at 14:32