Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell-测试TXT/CSV文件中的多个URL,并将HTTP代码记录到CSV文件中以进行报告_Powershell_Url - Fatal编程技术网

PowerShell-测试TXT/CSV文件中的多个URL,并将HTTP代码记录到CSV文件中以进行报告

PowerShell-测试TXT/CSV文件中的多个URL,并将HTTP代码记录到CSV文件中以进行报告,powershell,url,Powershell,Url,希望有人能帮上忙 我基于此构建了一个脚本 目前,上面的构建是为了一次处理一个URL,这对我来说很好,因为链接的数量和脚本的使用率总体上很低,所以我保持原样 但是,随着上述脚本的使用率和URL的增加,我希望进一步修改代码,使其能够同时运行多个URL 我的想法是将URL保存到TXT或CSV文件中,逐行读取,并逐行运行脚本。然后,它将记录响应并将HTTP代码(例如200、404等)输出到CSV(或原始CSV文件)中,并在其中输入数据 如果可能的话,我想记录“$HTTP_Response”的输出并将其添

希望有人能帮上忙

我基于此构建了一个脚本

目前,上面的构建是为了一次处理一个URL,这对我来说很好,因为链接的数量和脚本的使用率总体上很低,所以我保持原样

但是,随着上述脚本的使用率和URL的增加,我希望进一步修改代码,使其能够同时运行多个URL

我的想法是将URL保存到TXT或CSV文件中,逐行读取,并逐行运行脚本。然后,它将记录响应并将HTTP代码(例如200、404等)输出到CSV(或原始CSV文件)中,并在其中输入数据

如果可能的话,我想记录“$HTTP_Response”的输出并将其添加进去,但这将是第二个目标

任何帮助都将不胜感激

谢谢。

Rajiv.

您要做的第一件事是将脚本转换为函数,并将URL作为参数

function Get-HTTPResponseCode
{
  param(
    [Parameter(Mandatory = $true, Position = 0)]
    [uri]$Url,

    [switch]$Quiet
  )

  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  try {
    # First we create the request.
    $HTTPS_Request = [System.Net.WebRequest]::Create("$Url")

    # We then get a response from the site.
    $HTTPS_Response = $HTTPS_Request.GetResponse()

    # We then get the HTTP code as an integer.
    $HTTPS_Status = [int]$HTTPS_Response.StatusCode
    $HTTPS_StatusDesc = [string]$HTTPS_Response.StatusDescription
  }
  finally {
    # Finally, we clean up the http request by closing it.
    $HTTPS_Response.Close()
    $HTTPS_Response.Dispose()
  }

  #Write-Host "HTTP CODE: $HTTPS_Status"

  if(-not $Quiet){
    if ($HTTPS_Status -eq 301) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Landing page moved permanently and redirects to another URL."
      Write-Host "Please update Landing page to new URL"
    }
    elseif ($HTTPS_Status -eq 302) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "If this occurs once, then no issues"
      Write-Host "If this occurs more than once, please update Landing page to new URL"
    }
    elseif ($HTTPS_Status -eq 200) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Landed on page"
    }
    elseif ($HTTPS_Status -gt 400) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Error - issue with Landing page. Please investigate."
    }
  }

  # return the response code
  return $HTTPS_Status
}
现在我们有了一个易于重用的函数,我们可以做一些有趣的事情,比如在计算属性中使用它,例如:

$url=@(
'https://www.stackoverflow.com'
'https://www.stackexchange.com'
)
$URLs |选择对象@{Name='URL';表达式={$},@{Name='Status';表达式={Get-HTTPResponseCode-URL$}}}|导出Csv。\path\to\result.Csv-NoTypeInformation
如果要单独存储输入URL,只需将它们放入一个文件中,每行一个,然后使用
Get Content
从磁盘读取它们:

$URL=获取内容。\path\to\file\with\URLs.txt

Hi Mathias,我有没有办法将URL添加到TXT文件中,让您修改后的脚本逐行读取并输出到CSV文件?@RajivAhmed绝对,添加了使用
Get Content
Hi@Mathias从文件读取URL的示例,我已经测试了上述内容,尽管脚本运行,但输出文件仅显示从任一方法输入的URL,并且状态代码列为空。我做错什么了吗?我的错,示例中有一个输入错误,现在修复了。谢谢。我也错过了。我认为这是一个更复杂的问题。。。是否可以获取此行的输出:“$HTTPS\u Response=$HTTPS\u Request.GetResponse()”并将其添加到CSV中?我试图修改代码,但它没有给出我想要的-在CSV中声明为“System.Net.HttpWebResponse”。
function Get-HTTPResponseCode
{
  param(
    [Parameter(Mandatory = $true, Position = 0)]
    [uri]$Url,

    [switch]$Quiet
  )

  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  try {
    # First we create the request.
    $HTTPS_Request = [System.Net.WebRequest]::Create("$Url")

    # We then get a response from the site.
    $HTTPS_Response = $HTTPS_Request.GetResponse()

    # We then get the HTTP code as an integer.
    $HTTPS_Status = [int]$HTTPS_Response.StatusCode
    $HTTPS_StatusDesc = [string]$HTTPS_Response.StatusDescription
  }
  finally {
    # Finally, we clean up the http request by closing it.
    $HTTPS_Response.Close()
    $HTTPS_Response.Dispose()
  }

  #Write-Host "HTTP CODE: $HTTPS_Status"

  if(-not $Quiet){
    if ($HTTPS_Status -eq 301) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Landing page moved permanently and redirects to another URL."
      Write-Host "Please update Landing page to new URL"
    }
    elseif ($HTTPS_Status -eq 302) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "If this occurs once, then no issues"
      Write-Host "If this occurs more than once, please update Landing page to new URL"
    }
    elseif ($HTTPS_Status -eq 200) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Landed on page"
    }
    elseif ($HTTPS_Status -gt 400) {
      Write-Host "HTTP CODE: $HTTPS_Status"
      Write-Host "HTTP CODE DESCRIPTION: $HTTPS_StatusDesc"
      Write-Host "Error - issue with Landing page. Please investigate."
    }
  }

  # return the response code
  return $HTTPS_Status
}