websession error

$menuChoice = $null
$webSite = "https://dealer.md-bmc.rpdss.com/default.aspx"
$year = (Get-Date).Year
$month = (Get-Date).Month
if ($month -lt "10") {
    $month = "0" + $month
}
$metalsXML = Get-ChildItem "\\Company-040538\c$\BWI\XML"
$vincheckXML = Get-ChildItem "\\Company-040538\c$\BWI\XMLNew\Vincheck"

function loginToRAPID() {    
    $access = Invoke-WebRequest -uri $webSite -SessionVariable login
    $form = $access.forms[0]    
    $Global:organization = Read-Host "`tEnter the organization"
    $username = Read-Host "`tEnter the username"
    $password = Read-Host "`tEnter the password"
    $form.fields["organization"] = $organization
    $form.fields["Username"] = $username
    $form.fields["Password"] = $password    
    $Global:login = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -WebSession $login -Method post -Body $form.Fields              
}

function uploadXML() {    
    if ($login.AllElements | where {$_.innerhtml -like "Please provide your username and password for authentication."}) {
        Write-Host "`tLog in failed, please try again! `n" -ForegroundColor Red
    }
    else {        
        $info = $login.AllElements | where {$_.tagName -contains "span"}
        $account = $info.innerhtml | Select-Object -First 3      
        Write-Host "`tLogged in as" $account "`n" -ForegroundColor Green 
        $upload = $login.AllElements.InnerHtml | Where-Object {$_ -match 'url="(administration/uploadprofileselector.aspx\?.*?)"'} | 
            ForEach-Object {$matches[1]}   
        $uploadPage = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/bwident/pawnshop/" + $upload[0]) -WebSession $login                                       
    }
}

I’m getting this error, and it’s saying it’s related to the websession, I’m not sure what I’m doing wrong.

I’m using the same websession ($login) to login and access a page.

"Invoke-WebRequest : Cannot bind parameter ‘WebSession’. Cannot convert the value of type “Microsoft.PowerShell.Commands.HtmlWebResponseObject” to type
“Microsoft.PowerShell.Commands.WebRequestSession”.
At U:\PowerShell\BWI\BWI.ps1:40 char:84

  • … adPage = Invoke-WebRequest -uri (“www.google.com”) -WebSession $login
  •                                                                ~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Invoke-WebRequest], ParameterBindingEx
      ception
    • FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands
      .InvokeWebRequestCommand

The result of Invoke-WebRequest is a response object, not a session object. See Example 2 in the help. This:

    $Global:login = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -WebSession $login -Method post -Body $form.Fields              

Is what’s messing you up. (A) using the global scope is a Bad Idea, and (B) this is probably messing with the $login that you stored the session in previously.