【问题标题】:PowerShell function works fine when on its own, but stops working when followed by another function. Why?PowerShell 函数在单独使用时可以正常工作,但在其他函数之后会停止工作。为什么?
【发布时间】:2021-02-01 18:49:11
【问题描述】:

第一个函数会单独运行。

功能:

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

它可以正确打印我在搜索输入中输入的内容 (Linux)。

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

这是正确的输出。

但是当我添加以下函数时:

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

第一个函数停止工作:

Type of Git to search: Linux

Here is a list of the top 10 results:


Type number here:

现在搜索到的列表不见了。

为什么?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    这里有类似的问题:Show output before Read-Host

    线

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

    将结果列表放入缓冲区,脚本结束时输出。这就是它在您的第一次运行中起作用的原因。

    在第二次运行中,脚本在到达终点之前会进行更多处理。具体来说,它会在您的 Get-Menu 函数中命中 Read-Host 提示。您应该会发现在您在控制台提示符Type number here: 中输入输入后会出现 href 列表。

    为了在Get-Search 中强制输出,请将输出分配给一个变量并显式打印:

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

    【讨论】:

      【解决方案2】:
      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
      
      "
      

      对不同的命令:

      1. Write-Host 不向显示器输出任何内容(换行除外)
      2. 其余的是放在管道上的字符串,类似于Write-Output ...
        输出 (string) 具有与 $contributors 对象不同的属性,另请参阅:PowerShell write-output missing information - prints only 1st object

      在 PowerShell 中,在默认显示输出的情况下,通常不需要使用 Write-Output cmdlet。

      所以,正确的语法是:

      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
      
      "
      

      (引用应该直接跟在Write-Host之后)

      【讨论】:

      • 是的,还有更多命令。我没有添加它们,因为当添加更多信息时,第一个功能不起作用的问题并不重要。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多