代码之家  ›  专栏  ›  技术社区  ›  Wiktor

如何获取循环外的值-powershell

  •  0
  • Wiktor  · 技术社区  · 8 年前

    Function MyFunction{
    clear-host
    $hash = @{"ING"= "http://google.pl/" ; "youtube" = "https://youtube.com/" ;}
    $weburl = $hash.Values 
    foreach ($weburls in $weburl){ 
    $HTTP_Request = [System.Net.WebRequest]::Create($weburls)
    $HTTP_Response = $HTTP_Request.GetResponse() 
    # We then get a response from the site.
    $HTTP_Status = [int]$HTTP_Response.StatusCode # We then get the HTTP code as an integer.
    If ($HTTP_Status -eq 404 -or $HTTP_Status -eq 503 -or $HTTP_Status -eq 403 ) {
        Write-Host "$weburls The Site may be down, please check. - status is $HTTP_Status!"
        $URLRESP = New-Object -TypeName psobject
        $URLRESP | Add-Member -MemberType NoteProperty -Name  Status -Value "The Site may be down, please check!"
        [array]$URLRES += $URLRESP
    }
    Else {
        Write-Host "OK"
        $URLRESP = New-Object -TypeName psobject
        $URLRESP | Add-Member -MemberType NoteProperty -Name  Status -Value "OK"
        [array]$URLRES += $URLRESP
    }
    $HTTP_Response.Close() # Finally, we clean up the http request by closing it.    }
    

    循环到此结束。现在我正在将它转换为html,其中包含值Name,Value,Link,在这里我想添加一个新值-the“ 状态

        $hash.GetEnumerator() | sort -Property Name | Select-Object Name, Value, Status | 
        ConvertTo-HTML -Head $headLink  -Property *,@{Label="Link";Expression={"
    }
    
    $html_url = MyFunction 
    Add-Type -AssemblyName System.Web
    [System.Web.HttpUtility]::HtmlDecode($html_url) | Out-File  "\\......\scresult\test.htm" -Append
    

    我不知道如何把一个价值“地位”放在它之外。很抱歉,我不擅长脚本编写,我不知道怎么做。我有一种感觉,如果我把输出线放在循环中,它将乘以每个值。

    2 回复  |  直到 8 年前
        1
  •  1
  •   ArcSet    8 年前

    Arraylist 添加一个 PSCustomObject 你想要传递的属性。这样你就可以在最后返回一个数组 Hashtables 同样的 Keys .

    function GetSiteStatus(){
        [CmdletBinding()]
        param(
            [Parameter(ValueFromPipeline)]
            [hashtable]$WebSiteInfo
        )
        process{
            $Response = New-Object System.Collections.ArrayList
            $WebSiteInfo.GetEnumerator() | %{
                $Item = $_
                try{ 
                    $status = Invoke-WebRequest -Uri $_.Value | %{
                        if(@('404','503','403') -match $_.StatusCode){
                            "$($Item.Key) The Site may be down, please check. - status is $($_.StatusCode)"
                        }else{
                            "OK"
                        }
                    }
                    $Response.Add([PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value)}) | out-null
                }catch{
                    $Status = "$($Item.Key), $_."
                    $Response.Add([PSCustomObject]@{"Name"= $Item.Key; "Value"=$Item.Value; "Status"=$Status; "Link"=$($Item.value)}) | out-null
                }
            }
            return $Response
        }
    }
    

    convertto-html 和 out-file.

    $HTML = @{
        "Test403" = "https://httpstat.us/403"; 
        "Test404" = "https://httpstat.us/404";
        "Test503" = "https://httpstat.us/503";
        "Google" = "http://www.Google.com";
        "GoogleFake" = "https://GoogleFAKE1312.com"
    } | GetSiteStatus | ConvertTo-Html -Property Name,Value,Status,@{Label="Link";Expression={"<a href='$($_.Value)'>$($_.Name)</a>"}}
    
    Add-Type -AssemblyName System.Web
    [System.Web.HttpUtility]::HtmlDecode($HTML) | Out-File C:\TEST\test.htm
    
        2
  •  1
  •   Clayton Lewis    8 年前

    一些变化,这里是一个工作脚本,我能够测试。我想它会帮你得到你想要的东西

    [System.Collections.ArrayList]$Results = @()
    
    Function MyFunction {
        Clear-Host
        $hash = @{
            "ING"     = "http://google.com/"
            "youtube" = "https://youtube.com/"
        }    
        $weburl = $hash.Values 
        foreach ($url in $weburl){ 
            $HTTP_Request = [System.Net.WebRequest]::Create($url)
            $HTTP_Response = $HTTP_Request.GetResponse()
            $HTTP_Status = [int]$HTTP_Response.StatusCode
    
            If (($HTTP_Status -eq 404) -or ($HTTP_Status -eq 503) -or ($HTTP_Status -eq 403)) {
                Write-Host "$url The Site may be down, please check. - status is $HTTP_Status!"            
            }
            Else {
                Write-Host "$url is OK"            
            }
            $HTTP_Response.Close()
    
            $Result = [PSCustomObject]@{
                URL = $url
                Status = $HTTP_Status
            }
            $Results.Add($Result) | Out-Null
        }
    }
    
    MyFunction
    
    $Results
    

    编辑

    再次查看后,如果您导致错误响应,它将抛出异常。您需要添加一些错误处理,例如 try catch 并可能解析错误以获得准确的错误代码。当我做快速测试时,我假设它返回了一个错误代码。

    在line:1 char:1 +$HTTP\u Request.GetResponse() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~