none
Web Request for 2 URL using ForEach RRS feed

  • Question

  • Hello,

    I am try to check web request status for 2 urls. For some reason it is giving me output only for second url. Below is the code:

    Workflow Check-MPStatus
    {
        PARAM(
        [string[]]$SRVList
        )
    
        foreach -parallel($computer in $SRVList)
        {
    
    
            InlineScript
            {
    
                    $FQDN = [System.Net.DNS]::GetHostEntry($USING:computer).HostName 
                    $MPListURL="http://$FQDN/SMS_MP/.sms_aut?mplist"
                    $MPCertURL="http://$FQDN/SMS_MP/.sms_aut?mpcert"
                    $URL = $MPListURL,$MPCertURL
    
    
                    $props = [ordered]@{ 
                      Time = Get-Date; 
                      URL = $null 
                      StatusCode = $null 
                      StatusDescription = $null 
                      ResponseLength = $null }
    
    
                foreach($Link in $URL)
                {
    
                   Try
                    {
                          $request = Invoke-WebRequest -Uri $Link
    
                          $props.URL = $Link
                          $props.StatusCode = [int]$request.StatusCode
                          $props.StatusDescription = $request.StatusDescription
                          $props.ResponseLength = $request.RawContentLength
    
                    }
    
    
                     Catch 
                     { 
    
                           $request = $_.Exception.Response 
    
                     } 
                 }  
    
                 New-Object -TypeName PSObject -Property $props     
    
    
             } 
         }
     }
    
    
    
    Check-MPStatus -SRVList Server1
    
    
    

    Please advice. Thanks

    Rajiv

    Friday, May 23, 2014 3:43 PM

Answers

  • Your new-object cmdlet is outside the foreach loop. That means that it'll only operate once using the last URL's values.
    Friday, May 23, 2014 4:10 PM

All replies

  • Maybe one of them is failing?

    Try your catch statement like this:

    Catch {
    $props
    .URL = $Link $props.StatusCode =$_.Exception.Response
    }

    I typically use $error[0].exception.message instead of $_.Exception.Response but to each their own - perhaps in this case your error catch works better.


    I hope this post has helped!

    Friday, May 23, 2014 4:06 PM
  • You might prefer to use $props.StatusDescription for the error message, I don't know what you're trying to do so I just threw in the StatusCode property because that made sense to me.

    I hope this post has helped!

    Friday, May 23, 2014 4:07 PM
  • Your new-object cmdlet is outside the foreach loop. That means that it'll only operate once using the last URL's values.
    Friday, May 23, 2014 4:10 PM
  • Good catch!

    I hope this post has helped!

    Friday, May 23, 2014 4:13 PM
  • Hi Alex,

    Thanks! Moving the New-Object inside foreach solved the issue. My bad :)

    Rajiv

    Monday, May 26, 2014 9:46 AM