我正在尝试使用访问网站并登录的 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 可能不可用的构建服务器上运行时