Anamoly when passing a uri to invoke-webrequest

Welcome Forums General PowerShell Q&A Anamoly when passing a uri to invoke-webrequest

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 7 months ago.

  • Author
  • #77932

    Points: 3
    Rank: Member


    I am experiencing a bit of an intersting issue when I am passing an uri to the cmdlet.

    I type in this syntax like this:

    Connect-Thrukwebsite -uri https://server01/bcmonitoring -credential $credential

    However i get this error.

    Invoke-WebRequest : Not Found
    The requested URL /bcmonitoring/cgi-bin/login.cgi was not found on this server.
    At C:\Users\weiyentan\Documents\GitHub\ps\Thruk\OMDThruk\OMDThruk.psm1:40 char:3
    +         Invoke-WebRequest -Uri $urlpath -WebSession $thrukwebsession  ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

    However when i expand the url like so

    invoke-webrequest https://icinga/bcmonitoring/bcmonitoring/cgi-bin/login.cgi -SessionVariable test

    it recognises it. Is there a way to get the function to recognise the uri?

    function is below.

    function Connect-Thrukwebsite
    		[Parameter(Position = 0)]
    		[Parameter(Mandatory = $true,
    				   Position = 1)]
    		[Parameter(Position = 2)]
    	$Thrukweburl = $uri
    	$login = Invoke-WebRequest "$uri/thruk/cgi-bin/login.cgi" -SessionVariable global:thrukwebsession
    	$login.Forms[0].Fields.login = $credential.UserName
    	$login.Forms[0].Fields.password = $credential.GetNetworkCredential().Password
    	$UAstring = "Firefox"
    	Invoke-WebRequest -Uri "$uri/thruk/cgi-bin/login.cgi" -WebSession $thrukwebsession -Body $login.Forms[0].fields -Method Post -OutFile $env:TEMP\dump.htm
    	$LoggedIn = Select-String -Path $env:temp\dump.htm -Pattern ($credential.UserName) -Quiet
    	$urlpath = "$uri/cgi-bin/login.cgi"
    	#Remove-Item .\dump.htm -Force -Confirm:$false -ErrorAction SilentlyContinue
    	if ($LoggedIn -eq $true)
    		Write-Verbose "You are now logged in!"
    		$mainPage = Invoke-WebRequest -Uri $urlpath -WebSession $thrukwebsession -Body $login.Forms[0].fields -Method Post
    		Write-Error "Login failed!"

    Hope is appreciated.

  • #77938

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    The command is just returning the error given to it by the server. It's likely the server doesn't recognize the first URI, or that the underlying operating system functions don't.

  • #77947

    Points: 3
    Rank: Member

    Thanks @Don.

    I went back and rejigged it from:
    Invoke-WebRequest -Uri $urlpath -WebSession $thrukwebsession -Body $login.Forms[0].fields -Method Post | Out-Null to

    Invoke-WebRequest -Uri $uri -WebSession $thrukwebsession -Body $login.Forms[0].fields -Method Post | Out-Null

  • #77985

    Points: 1
    Rank: Member

    It actually look like you might have just forgot to put thrunk in your urlpath based on your other uses

    $urlpath = "$uri/cgi-bin/login.cgi"

    maybe it should have been

    $urlpath = "$uri/thruk/cgi-bin/login.cgi"


The topic ‘Anamoly when passing a uri to invoke-webrequest’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort