0

The first function will work when on its own.

Function:

function Get-Search {

    $find = Read-Host "Type of Git to search"
    $url = 'https://github.com/search?q=' + $find
    Write-Host ""
    Write-Host "Here is a list of the top 10 results:"
    $contributors = Invoke-WebRequest -Uri $url -UseBasicParsing
    $contributors.Links| where class -EQ "v-align-middle" | select href 

}                                 

Get-Search

It prints correctly what I typed (Linux) into the search input.

Type of Git to search: Linux

Here is a list of the top 10 results:

href                        
----                        
/torvalds/linux             
/raspberrypi/linux          
/jaywcjlove/linux-command   
/0xAX/linux-insides         
/GameServerManagers/LinuxGSM
/judasn/Linux-Tutorial      
/endlessm/linux             
/beagleboard/linux          
/linuxkit/linuxkit          
/afaqurk/linux-dash

This is the correct output.

But when I add the following function:

function Get-Search {

    $find = Read-Host "Type of Git to search"
    $url = 'https://github.com/search?q=' + $find
    Write-Host ""
    Write-Host "Here is a list of the top 10 results:"
    $contributors = Invoke-WebRequest -Uri $url -UseBasicParsing
    $contributors.Links| where class -EQ "v-align-middle" | select href 

}                                 

Get-Search

function Get-Menu {
    
    Write-Host 
    "
    Options:

    Want to explore a 'Git-README'? Type 1
    Want to load a 'Git-Repo'? Type 2
    Want to make a new search? Type 3

    "
    $choice = Read-Host "Type number here"
    Write-Host ""
    $pick1 = if ([string]1 -eq $choice )

    {
    $add = Read-Host "Add an above repo here"
    $url2 = 'https://raw.githubusercontent.com/' + $add + '/master/README.md'
    $contributors = Invoke-WebRequest -Uri $url2 -UseBasicParsing
    $contributors.Links| where class "p" | select innerText
    }

}

Get-Menu

The first function stops working:

Type of Git to search: Linux

Here is a list of the top 10 results:


Type number here:

The searched list is now missing.

Why?

Matthew
  • 27
  • 6

2 Answers2

0
Write-Host 
"
Options:

Want to explore a 'Git-README'? Type 1
Want to load a 'Git-Repo'? Type 2
Want to make a new search? Type 3

"

Are to different commands:

  1. Write-Host which outputs nothing (except for a linefeed) to the display
  2. The rest which is a string which is put on the pipeline, similar to Write-Output ....
    The output (string) has different properties than $contributors object, see also: PowerShell write-output missing information - prints only 1st object.

In PowerShell, it is generally not necessary to use the Write-Output cmdlet in instances where the output is displayed by default.

So, the correct syntax for this is:

Write-Host "

Options:

Want to explore a 'Git-README'? Type 1
Want to load a 'Git-Repo'? Type 2
Want to make a new search? Type 3

"

(the quote should come directly after Write-Host)

iRon
  • 20,463
  • 10
  • 53
  • 79
  • Yes, there are more commands. I haven't added them because it wasn't important to my issue of the first function not working when more information was added. :) – Matthew Jan 31 '21 at 05:48
0

Similar question here: Show output before Read-Host

The line

$contributors.Links| where class -EQ "v-align-middle" | select href

places the list of results in a buffer, which is output when the script ends. This is why it works in your first run.

In the second run the script does more processing before reaching the end. Specifically, it hits the Read-Host prompt in your Get-Menu function. You should find that the list of hrefs appears after you type an input to the console prompt Type number here:.

In order to force the output in Get-Search, assign the output to a variable and print it explicitly:

$output = $contributors.Links| where class -EQ "v-align-middle" | select href | Out-String
Write-Output $output