Anamoly when passing a uri to invoke-webrequest

Hi,

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
{
	[CmdletBinding()]
	param
	(
		[Parameter(Position = 0)]
		[string]$uri,
		[Parameter(Mandatory = $true,
				   Position = 1)]
		[pscredential]$credential,
		[Parameter(Position = 2)]
		[switch]$apache
	)

	$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
	}
	else
	{
		Write-Error "Login failed!"
	}
}

Hope is appreciated.

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.

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

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”
vs.
“$uri/thruk/cgi-bin/login.cgi”

maybe it should have been

$urlpath = “$uri/thruk/cgi-bin/login.cgi”

?