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

pmac5
  • 65
  • 1
  • 7

3 Answers3

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
Jacob
  • 1,182
  • 12
  • 18
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
Theo
  • 57,719
  • 8
  • 24
  • 41
0

You can try something like a read host prompt

  Read-Host -Prompt 'Press any key to close'
    exit
NickB
  • 7
  • 3