header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Test Webpage
Last Post 06 Jan 2012 11:23 AM by Bobdee. 15 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BobdeeUser is Offline
Basic Member
Basic Member
Posts:130
Avatar

--
01 Nov 2010 01:51 AM
    Hi - can anyone point me in the right direction for a problem I'm having?

    I want to script opening websites then return a value depending on what happens.  Sort of like - site opens = 0 anything else = 1.

    Something like this - I know this won't work but it shows my thinking

    $ie = new-object -comobject "InternetExplorer.Application"
    $ie.visible = $false

    if (!( $ie.navigate("http://www.mysite.com"))) 
       { 
          $response = 0 
       }
    else 
       { 
          $response = 1 
       }

    Thanks in advance.

    Rob
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 05:11 AM
    Instead of using a COM object, why not use native .NET Framework objects. For instance, you could use the following:

    $webRequest = [net.WebRequest]::Create("http://www.mysite.com")
    $response = $webRequest.GetResponse()


    This returns a HttpWebResponse object to the $response variable, and then you can check if the site loads by looking at the StatusCode property. This maps to the HttpWebResponse.StatusCode enumeration which gives you a lot of flexibility to see what happened when you requested the site.

    You can then check like this...

    if ($response.statuscode -eq 200)
    {
    $siteLoaded = $true
    }
    else
    {
    $siteLoaded = $false
    }
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 05:53 AM
    That's fantastic, and simple! Brilliant, thanks for that.

    Rob
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 06:28 AM
    This works for all sites outside my intranet...

    Do you have any ideas on why? Proxy settings etc?
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 06:42 AM
    You can use the system.net.webproxy class.

    $proxy = new-object net.webproxy
    $proxy.address = 'http://proxysrv.domain.com:80'
    $webRequest = [net.webrequest]::Create("http://www.mysite.com")
    $webRequest.Proxy = $proxy
    $response = $webRequest.GetResponse()


    See if that helps you get to what you want. Just substitute your proxy address for 'proxysrv.domain.com'
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 07:05 AM

    Same error - I'm actually getting 401 errors.

    Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized." At :line:13 char:35 + $response = $webRequest.GetResponse <<<< ()

    My thinking with the proxy was that possibly my credentials are not being passed to the site?  I can hit external sites no problem.

    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 07:12 AM
    $proxy.credentials = new-object system.net.networkcredential("Domain\username","password")

    add a line like this after you create the $proxy object and before you make the .GetResponse() call.
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 07:23 AM
    Timing out now!

    Exception calling "GetResponse" with "0" argument(s): "The operation has timed out"

    At line:5 char:36

    + $response = $webRequest.GetResponse <<<< ()

    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 07:26 AM

    Do you need a proxy to access your intranet?  If so, does the proxy require authentication, or does the website intranet need authentication?

    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 07:30 AM
    Yeah, everything routes through a proxy and the site requires authentication. We also require exceptions for internal resources - my site being one.

    I would have thought they were picked up by .Net, or do they too have to be manually loaded?

    Thanks again.
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 07:40 AM
    Sorry to ask this again, but just trying to be clear on what we are troubleshooting.

    Does the proxy require authentication?

    Does the site require authentication?

    There are two different ways to supply credentials for the site and the proxy.  The one I gave you authenticates you to the proxy server.  You may still need to provide authentication to the site itself.

    After you create the webrequest object, you can use the UseDefaultCredential property to use the credentials that Powershell is running in (according to the MSDN information). 

    $webRequest.UseDefaultCredentials = $true

    See if that helps any.
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 07:51 AM
    yes, both require authentication...

    I've added the UseDefaultCredentials property and we have a winner!

    Thanks for your time, I greatly appreciate it.

    Rob
    JonathanUser is Offline
    Basic Member
    Basic Member
    Posts:109
    Avatar

    --
    01 Nov 2010 08:16 AM
    Glad to help and glad it worked for you.
    Jonathan Tyler
    http://powershellreflections.wordpress.com
    Follow Me On Twitter
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    01 Nov 2010 09:39 AM
    I ran into this problem lately too, here is the advanced function I wrote to solve this problem.

    Note: Usually if your testing for something you wanting to get a true/false response. I made this command to mimic Test-Path.

    Ussage:

    Test-WebPath cnn.com -Timeout 10

    or

    if(Test-WebPath('website.com/blah/')){Do Stuff}



    Function Global:Test-WebPath
    {


    <#
    .Synopsis

    .Description

    .Parameter $Path

    .Example
    PS> Test-WebPath -Path cnn.com

    .Example
    PS> Test-WebPath -Url yahoo.com -Timeout 6
    .Example
    PS> if(Test-WebPath('website.com/blah/')){Do Stuff}

    .Link
    about_functions
    about_functions_advanced
    about_functions_advanced_methods
    about_functions_advanced_parameters

    .Notes
    NAME: Test-WebPath
    AUTHOR: PowershellJedi
    LASTEDIT: 10/28/2010 10:19:14 PM
    #Requires -Version 2.0
    #>



    [CmdletBinding()]
    param(
    [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
    [Alias('Url')]
    [string]$Path,
    [Parameter(Position=1)]
    [int]$Timeout = 6
    )


    Process
    {
    $ErrorActionPreference="SilentlyContinue"

    if(!($Path.StartsWith('http://'))){[string]$Path = 'http://' + $Path}

    #-------
    if($Timeout){
    if($Timeout.Length -le 3){
    [string]$Timeout = $Timeout
    [string]$Timeout = $Timeout + '000'
    [int]$Timeout = $Timeout
    }# if
    }# if
    #-------


    [System.Net.HttpWebRequest] $request = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($Path)
    $request.Timeout = $Timeout
    $Response = $request.GetResponse()

    if($Response){$true}else{$false}

    $request.Abort()

    Remove-Variable -ErrorAction SilentlyContinue -Name Path
    Remove-Variable -ErrorAction SilentlyContinue -Name Response
    Remove-Variable -ErrorAction SilentlyContinue -Name request
    Remove-Variable -ErrorAction SilentlyContinue -Name Timeout

    }# Process



    }# Test-WebPath



    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    01 Nov 2010 11:30 AM
    Jedi, it's a bit outta my depth, but I can see what is fundamentally going on. It's a nice solution to the problem, thank you. Rob
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    06 Jan 2012 11:23 AM
    Back knowing more...

    Great post Jedi :-)
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer