I am trying to modify the PowerShell script I have found on ElegantCode.Com. I want to change it to specify a large text file of HTTP links, rather than 开发者_JAVA技巧naming the links as a parameter individually.
Once the script parses the file, I want it to pipe or echo out only the links that are valid back to a new file.
I am falling at the first hurdle and can't even figure out how I pass the input file in as a parameter.
Direct link for the script is here
    BEGIN {
    }
    PROCESS {
$url = $_;
$urlIsValid = $false
try
{
    $request = [System.Net.WebRequest]::Create($url)
    $request.Method = 'HEAD'
    $response = $request.GetResponse()
    $httpStatus = $response.StatusCode
    $urlIsValid = ($httpStatus -eq 'OK')
    $tryError = $null
    $response.Close()
}
catch [System.Exception] {
    $httpStatus = $null
    $tryError = $_.Exception
    $urlIsValid = $false;
}
$x = new-object Object | `
        add-member -membertype NoteProperty -name IsValid -Value $urlIsvalid -PassThru | `
        add-member -membertype NoteProperty -name Url -Value $_ -PassThru | `
        add-member -membertype NoteProperty -name HttpStatus -Value $httpStatus -PassThru | `
        add-member -membertype NoteProperty -name Error -Value $tryError -PassThru
$x 
       }
      } 
      END { 
      }
It appears the script it expecting the url to be piped in.  The variable $_ represents the current pipeline object. So if the text file contained on URL per line you could do something like this:
Get-Content Urls.txt | Where {$_ -notmatch '^\s*$'} | Check-Url
I put the where in the pipe to eliminate blank lines.
To pipe the valid urls to a file as requested (adding to Keith's answer):
$validUrls = ".\ValidUrls.txt"
if (Test-Path $validUrls) { Remove-Item -Force $validUrls }
$result = New-Item -Type File -Path $validUrls
Get-Content Urls.txt | Where {$_ -notmatch '^\s*$'} | Foreach-Object {
   $url = ($_ | Check-Url)
   if ($url.IsValid)
   {
      $url.Url | Out-File $result -Append
   }
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论