Powershell 是否可以使用Invoke Webrequest登录到使用Azure AD B2C身份验证的站点?

Powershell 是否可以使用Invoke Webrequest登录到使用Azure AD B2C身份验证的站点?,powershell,login,azure-ad-b2c,invoke-webrequest,Powershell,Login,Azure Ad B2c,Invoke Webrequest,我正试图通过使用访问网站并登录的PowerShell脚本来配置构建服务器以预热我的网站。整个网站都在登录之后 我的网站正在使用Azure AD B2C进行身份验证,并已使用Microsoft的入门指南进行配置: 当我通过浏览器登录时,一切正常 我当前的登录代码如下所示: $session = Get-AuthenticatedSession $DomainName $config.authenticationDetails $UserName $Password Function Get-Au

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

我的网站正在使用Azure AD B2C进行身份验证,并已使用Microsoft的入门指南进行配置:

当我通过浏览器登录时,一切正常

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

$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。非常感谢您的帮助

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

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

登录

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

我没有理由去搞Azure AD B2C,因此,没有经验知道该站点是什么或者你可以在上面做什么。然而,至于

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

。。。答案很简单,是的,正如文章中讨论和演示的那样。请仔细阅读文章以作充分解释

在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的操作的语法


我没有理由去搞Azure广告B2C,所以,我没有经验知道那个网站是什么,或者你可以在上面做什么。然而,至于

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

。。。答案很简单,是的,正如文章中讨论和演示的那样。请仔细阅读文章以作充分解释

在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的操作的语法