使用运行空间的 Powershell 脚本

Powershell Script Using Runspaces

我是 PowerShell 的新手,只是为了练习,我得到了说明,"Read in fortune 100 URLs to an array...then using runspaces, connect to each one and write the page you get with Invoke-WebRequest to file." 然后我得到了这个 link:https://www.zyxware.com/articles/4344/list-of-fortune-500-companies-and-their-websites

我一直在阅读有关运行空间的所有内容。现在我正在努力将我的 links 放入一个数组中,然后我可以从中提取前 100 个 URL。任何 help/advice 将不胜感激。

我现在遇到的问题是,当我调用 link 变量时,它不会给我 links。似乎我应该能够调用 $link 变量,这样我就可以获得所有 link 并将其放入一个数组中。使所有 link 出现的唯一方法是使用“$page.Links.href”。有人可以向我解释为什么调用该变量不起作用吗?

$page = Invoke-WebRequest https://www.zyxware.com/articles/4344/list-of-fortune-500-companies-and-their-websites

foreach( in $page.Links){
    if(.href -like '*www*com'){
        Write-Host .href

        $link = .Links.href

    }
}

           $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
$RunspacePool.Open()

一步一个脚印。

在做像运行空间这样高级的事情之前,您应该学习一些快速的 PowerShell 在线课程和材料,这样您就有了一个基线。特别是因为您说您从未真正使用过 PoSH。

https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276

https://youtube.com/results?search_query=beginner+powershell

网上有几本免费电子书...

https://powershell.org/ebooks

...以及大量可下载的脚本。

https://powershellgallery.com

和一长串资源...

Windows PowerShell Survival Guide https://social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

首先了解如何使用 PoSH WebCmdlet。

Get-Command -Name Invoke-WebRequest| Format-Table -AutoSize

CommandType Name              Version Source                      
----------- ----              ------- ------                      
Cmdlet      Invoke-WebRequest 3.1.0.0 Microsoft.PowerShell.Utility


# Get paramter, example, full and Online help for a cmdlet or function

(Get-Command -Name Invoke-WebRequest|).Parameters
Get-help -Name Invoke-WebRequest| -Examples
Get-help -Name Invoke-WebRequest| -Full
Get-help -Name Invoke-WebRequest| -Online


Get-Command -Name Invoke-RestMethod | Format-Table -AutoSize

CommandType Name              Version Source                      
----------- ----              ------- ------                      
Cmdlet      Invoke-RestMethod 3.1.0.0 Microsoft.PowerShell.Utility


# Get paramter, example, full and Online help for a cmdlet or function

(Get-Command -Name Invoke-RestMethod ).Parameters
Get-help -Name Invoke-RestMethod  -Examples
Get-help -Name Invoke-RestMethod  -Full
Get-help -Name Invoke-RestMethod  -Online


Then search using the string 'Invoke-WebRequest to parse website for URLs'

Do that website URL parsing first, get that where you need it then move on to runspaces.
If you can't make the first part happen the runspaces is moot.

因此,请尝试使用您放在一起的代码获取 URL,然后如果您对此有疑问,post 您已尝试过的内容,论坛将尝试推介.同样的事情将适用于运行空间部分的第二步。