powershell,websession名称问题

时间:2017-07-17 11:48:03

标签: powershell

$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                                       
    }
}

我收到此错误,并且说它与网络搜索有关,我不确定我做错了什么。

我使用相同的websession($ login)登录和访问页面。

" Invoke-WebRequest:无法绑定参数' WebSession'。无法转换类型" Microsoft.PowerShell.Commands.HtmlWebResponseObject"的值输入  " Microsoft.PowerShell.Commands.WebRequestSession&#34 ;.  在U:\ PowerShell \ BWI \ BWI.ps1:40 char:84 这是带有错误消息的websession。

$uploadPage = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/bwident/pawnshop/" + $upload[0]) -WebSession $login

1 个答案:

答案 0 :(得分:0)

不确定您是如何使用这些功能的。检查Invoke-WebRequest docs,看起来没有办法将$login SessionVariable范围设置为Global。

因此,在loginToRAPID函数中返回此变量并将其传递给uploadXML函数。否则uploadXML无法访问同一会话。这比在可能的情况下将变量范围设置为Global更好。

下面,我已将参数添加为$session。并在地方将$login更改为$loginResponse

$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:loginResponse = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -WebSession $login -Method post -Body $form.Fields

    return $login              
}

function uploadXML($session = loginToRAPID) {    
    if ($loginResponse.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 = $loginResponse.AllElements | where {$_.tagName -contains "span"}
        $account = $info.innerhtml | Select-Object -First 3      
        Write-Host "`tLogged in as" $account "`n" -ForegroundColor Green 
        $upload = $loginResponse.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 $session
    }
}