1

我正在尝试使用访问网站并登录的 PowerShell 脚本来配置我的构建服务器以预热我的网站。整个网站都在登录后面。

我的网站使用 Azure AD B2C 进行身份验证,并已使用 Microsoft 的入门指南进行配置:https ://github.com/AzureADQuickStarts/B2C-WebApp-OpenIdConnect-DotNet

当我通过浏览器登录时,一切都按预期工作。

我当前的登录代码如下所示:

$session = Get-AuthenticatedSession $DomainName $config.authenticationDetails $UserName $Password

Function Get-AuthenticatedSession {
  param(
    [Parameter(Mandatory=$true,Position=0)]
    [string]$domainName,
    [Parameter(Mandatory=$true,Position=1)]
    [object]$authenticationDetails,
    [Parameter(Mandatory=$true,Position=2)]
    [string]$username,
    [Parameter(Mandatory=$true,Position=3)]
    [string]$password
  )

  # Login - to create web session with authorisation cookies
  $loginPage = "$domainName$($authenticationDetails.url)"

  Write-Host "Getting Login Page $loginPage"

  try{
    $login = Invoke-WebRequest $loginPage -SessionVariable webSession -TimeoutSec 600 -UseBasicParsing
  }catch{
    Write-Host "First attempt failed with $($_.Exception.Response.StatusCode.value__) , retrying"
    $login = Invoke-WebRequest $loginPage -SessionVariable webSession -TimeoutSec 600 -UseBasicParsing
  }

  Write-Host "Got Login Page, filling out form"

  $fields = @{}
  $fields["$($authenticationDetails.userNameField)"] = $username
  $fields["$($authenticationDetails.passwordField)"] = $password

  Write-Host "logging in"

  $request = Invoke-WebRequest -Uri $loginPage -WebSession $webSession -Method POST -Body $form  -TimeoutSec 600| Out-Null

  $webSession

  Write-Host "login done"
}

这是我在登录后请求页面的代码,之后运行:

foreach ($page in $config.urls) {
    RequestPage "$DomainName$($page.url)" $session
}

Function RequestPage {
    param(
        [Parameter(Mandatory=$true,Position=0)]
        [string]$url,
        [Parameter(Mandatory=$true,Position=1)]
        [object]$webSession
    )
    Get-Date
    Write-Host "requesting $url ..."
    try { $request = Invoke-WebRequest $url -WebSession $webSession -TimeoutSec 60000 -UseBasicParsing} catch {
      $status = $_.Exception.Response.StatusCode.Value__
      if ($status -ne 200){
        Write-Host "ERROR Something went wrong while requesting $url" -foregroundcolor red
      }
    }

    Write-Host $request

    Get-Date
    Write-Host "Done"
    Write-Host ""
}

问题是脚本似乎没有正确登录。每当我在登录后请求页面时,我总是会返回登录页面的 html。任何帮助深表感谢!

编辑- 页面上似乎没有 FORM 元素 - 哦!实际的登录是一个附加了 javascript 点击事件的按钮元素。登录页面由 Microsoft 托管,因此我几乎无法控制 DOM 元素:

<div class="buttons">
            <button id="next" tabindex="1">Sign in</button>
          </div>

所以我想我的问题是是否可以使用 PowerShell 触发按钮单击?当我还希望脚本在 IE 可能不可用的构建服务器上运行时

4

1 回答 1

0

我没有理由与 Azure AD B2C 混淆,因此,没有关于该站点是什么或您可以在其上做什么的经验。不过,至于...

所以我想我的问题是是否可以使用 PowerShell 触发按钮单击?当我还希望脚本在 IE 可能不可用的构建服务器上运行时

...简单的答案是,是的,正如文章中所讨论和演示的那样...教 PowerShell to Click。红色文章以获得完整解释

在 PowerShell 编辑器中——在这种情况下,我使用的是 ISE——当然在 VSCode 中也是如此。

# View the page live to se what it does - just do stuff
Start-Process 'http://www.minasi.com/addit.htm'

# Now, inspect the site page/form. 
# View the page code to see what is there - If in the ISE

$minasi = Invoke-WebRequest -Uri 'http://www.minasi.com/addit.htm'
$minasi.Content | Out-ISETab
$minasi.AllElements | Out-ISETab
$minasi.Forms | Out-ISETab
$minasi.Forms.Fields | Out-ISETab
$minasi.InputFields | Out-ISETab

# View the page code to see what is there - If in VSCode

$minasi = Invoke-WebRequest -Uri 'http://www.minasi.com/addit.htm'
$minasi.Content | New-EditorFile
$minasi.AllElements | New-EditorFile
$minasi.Forms | New-EditorFile
$minasi.Forms.Fields | New-EditorFile
$minasi.InputFields | New-EditorFile


# Interact with the page to achieve the same results as GUI would by going direct to the 'results page' not the 'form page'.

$MethodPostURI = 'http://www.minasi.com/addit-worker.asp'
$Body ='addend1=3&addend2=9&B1=SUBMIT%21'
$page = Invoke-WebRequest $MethodPostURI -body $Body -method POST
$page.content


# You can also find elements by looping to find something specific, like the below...
$minasi.AllElements.FindById('SomeIdNameString')

# Or 

$SubmitElement = $minasi.AllElements | 
Where{
        $PSItem.tagName -eq 'INPUT' -and 
        $PSItem.value -eq 'SUBMIT!'
     }
$SubmitElement # The use dot lookup to see if there is a click option.

最后,这里有一些你可能想看看的东西。

查询

该模块旨在创建一个变量,我们可以使用类似 jQuery 的语法在网站上进行类似 jQuery 的操作。

于 2019-05-26T09:13:09.170 回答