2

When executing the below script, the output will be displayed very briefly as the script is closing. I am looking for a way to have the script pause after the output.

function Get-Hostname() {
    $IP_Address = Read-Host -Prompt 'What is the target IP Address'
    [System.Net.Dns]::GetHostByAddress("$IP_Address")
}

function Exit-Application() {
    Read-Host "Press Enter to Exit Application"
    exit
}

Get-Hostname
Exit-Application

enter image description here

Thanks in advance

0

3 Answers 3

2

Make sure the Get-Hostname function outputs the object to the console before you call Exit-Application, Try something like this:

function Get-Hostname() {
    $IP_Address = Read-Host -Prompt 'What is the target IP Address'
    [System.Net.Dns]::GetHostByAddress("$IP_Address") | Format-Table -AutoSize
}

function Exit-Application() {
    Read-Host "Press Enter to Exit Application"
    exit
}

Get-Hostname
Exit-Application
Sign up to request clarification or add additional context in comments.

1 Comment

That did it, thanks!
2

I don't know why this happens, but you can fix it by changing

[System.Net.Dns]::GetHostByAddress("$IP_Address")

To:

Write-Output $([System.Net.Dns]::GetHostByAddress($IP_Address))

and piping the output of the function.

Get-Hostname | ft

Using Write-Output preserves the object being returned from the function which you can work with or in this instance process the function output using Format-Table (ft).

You can check the object type being returned with (Get-Hostname).getType() | ft -autosize

IsPublic IsSerial Name        BaseType     
-------- -------- ----        --------     
True     False    IPHostEntry System.Object  

The full code:

function Get-Hostname {
    $IP_Address = Read-Host -Prompt 'What is the target IP Address'
    Write-Output $([System.Net.Dns]::GetHostByAddress("$IP_Address"))
}

function Exit-Application {
    Read-Host "Press Enter to Exit Application"
    exit
}

Get-Hostname | ft
Exit-Application

2 Comments

This worked too, thanks!
Write-Output makes no difference here - what matter is the explicit use of ft (Format-Table), which forces synchronous output.
0

You can try something like a read host prompt

  Read-Host -Prompt 'Press any key to close'
    exit

2 Comments

Same output happens, Exit is prompted before output is displayed.
@NickB: Read-Host is what the OP is using, and it surfaces the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.