简体   繁体   English

在PowerShell输出表中添加自定义值

[英]Add custom value in PowerShell output table

I have a list of application web URLs, wherein I am trying to monitor HTTP statuscode of the web URL by Invoke-WebRequest command method. 我有一个应用程序Web URL列表,其中我试图通过Invoke-WebRequest命令方法监视Web URL的HTTP状态代码。

I got the list objects by below command: 我通过以下命令获取列表对象:

Invoke-WebRequest https://example.com/123 | Get-Member

Here, I am using statuscode and statusdescription fields to include it in output: 在这里,我使用statuscode和statusdescription字段将其包括在输出中:

Invoke-WebRequest https://example.com/123 |
    Select-Object StatusCode, StatusDescription |
    ft -a

Now I wish to include a custom name of URL in output before statuscode and statusdescription so that output will look like below 现在,我希望在状态代码和状态描述之前在输出中包含URL的自定义名称,以便输出如下所示

URL          StatusCode StatusDescription
---          ---------- -----------------
abc web page        200 OK

If you dont need to store data in variables, and just print is fine for you 如果您不需要将数据存储在变量中,只需打印就可以了

$URLsources = "http://fooo/fooURL1:800","http://fooo/fooURL2:800","http://fooo/fooURL2:800"

#table definition
    $tabName = "Output table"

    #Create Table object
    $table = New-Object system.Data.DataTable “$tabName”

    #columns definition
    $col1URL = New-Object system.Data.DataColumn URL,([string])
    $col2Status = New-Object system.Data.DataColumn Status,([string])
    $col3Desc = New-Object system.Data.DataColumn Desc,([string])

    #add columns
    $table.Columns.Add($col1URL)
    $table.Columns.Add($col2Status)
    $table.Columns.Add($col3Desc)


foreach($url in $URLsources){
  $result = Invoke-WebRequest $url 

  #preparation of the row
      $row = $table.NewRow()
      $row.URL= $url
      $row.Status= $result.StatusCode
      $row.Desc= $result.StatusDescription

  #add row to the table  
  $table.Rows.Add($row)

}

#print out the table
$table | format-table -AutoSize 

You can add everything to your output: 您可以将所有内容添加到输出中:

Invoke-WebRequest www.google.com | Select-Object -Property @{n="URL";e={'Any Name'}},StatusCode, StatusDescription

If you want the real url which you checked in your output you could do it like that: 如果您想要在输出中检入的真实网址,可以这样做:

Invoke-WebRequest www.google.com | Select-Object -Property @{n="URL";e={$_.BaseResponse.ResponseUri.Host}},StatusCode, StatusDescription

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM