websession error

Welcome Forums General PowerShell Q&A websession error

This topic contains 1 reply, has 2 voices, and was last updated by

1 year, 6 months ago.

  • Author
  • #75022

    Points: 64
    Rank: Member
    $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
    At U:\PowerShell\BWI\BWI.ps1:40 char:84
    + ... adPage = Invoke-WebRequest -uri ("www.google.com") -WebSession $login
    + ~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingEx
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands

  • #75467

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

    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.

The topic ‘websession error’ is closed to new replies.

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