首页 > 解决方案 > 如何通过 Powershell / Selenium-Chrome 驱动程序对防火墙网页进行身份验证?

问题描述

我在 Powershell 中开发了几个脚本来自动配置我工作的机器,简化并避免手动配置大量工作站的繁重工作。基本上他们使用禁用 UAC、自动登录和任务计划程序。

图片:https ://i.stack.imgur.com/pkuDl.png

最大的问题是使用要求通过浏览器进行身份验证的 Fortinet 防火墙,因为许多应用程序使用通过 SMB 安装在服务器上的文件,因此有必要进行此身份验证,因为没有它就不可能向服务器进行身份验证的算法,导致它在课程中失败。

图片:https ://i.stack.imgur.com/bqZAR.png

页面网址:https://authenticator.mpms.mp.br/caplogin/?login&post=http://10.111.147.1:1000/fgtauth&magic=0202e294cb1c7073&usermac=10:e7:c6:c5:c3:61&apmac=00:00: 00:00:00:00&apip=10.111.147.1&userip=10.111.147.22&ssid=PGJ-BANCADA&apname=FGT2KE3917900027&bssid=00:00:00:00:00:00&device_type=windows-pc

但是,通过 Selenium 进行测试,它甚至可以进行身份​​验证,但是在计算机重新启动并运行下一个脚本后,它会再次要求进行身份验证。

按照我在 Fortnet 中验证的代码,在登录后,在 globo.com 站点中发出请求

############################################
######## Enable Fortinet Firewall ##########
############################################

$YourURL = "https://authenticator.mpms.mp.br/"

# Adds the path for ChromeDriver.exe to the environmental variable 
$env:PATH += ";C:\Util\PSL\" 

# Adding Selenium's .NET assembly (dll) to access it's classes in this PowerShell session
Add-Type -Path "C:\Util\PSL\WebDriver.dll" 

$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)
$ChromeDriver.Capabilities.BrowserName

# Browse to the specified website
$ChromeDriver.Navigate().GoToURL($YourURL) 

# Methods to find the input textbox for google search and then to type something in it
$ChromeDriver.FindElementByName("username").SendKeys("username")
$ChromeDriver.FindElementByName("password").SendKeys("password") 
$ChromeDriver.FindElementsByClassName("submit").Submit() 


#### New page #####
$YourURL = "https://www.globo.com/"
$ChromeDriver.Navigate().GoToURL($YourURL) 

Function Stop-ChromeDriver {Get-Process -Name chromedriver -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue}

# Close selenium browser session method
$ChromeDriver.Close() 

# End ChromeDriver process method
$ChromeDriver.Quit() 

# Function to make double sure the Chromedriver process is finito (double-tap!)
Stop-ChromeDriver

当我们手动执行此操作时,接下来的步骤将正常运行,根据我的结论,这使得 Selenium 驱动程序不是真正的 Google Chrome 浏览器,而是它自己的浏览器,这意味着它不识别身份验证。.

剩下的问题是:是否可以在脚本中添加此选项,以便我们可以对 Fortinet Web 进行身份验证,从而避免手动步骤并自动化我们的工作?我希望我可以为 Powershell 执行此任务,但我还有另一种选择。

谢谢!

标签: powershellseleniumbatch-fileselenium-webdrivervbscript

解决方案


为什么不在 PowerShell 中使用vbs发送所需的授权

$wshell = New-Object -ComObject wscript.shell; $obj = New-Object -com Wscript.Shell;
$wshell.AppActivate('Chrome');
pathping 127.0.0.1 -n -q 1 -p 300 >$null
$obj.SendKeys('paulogoncalves');
pathping 127.0.0.1 -n -q 1 -p 150 >$null
$obj.SendKeys("{TAB}")
pathping 127.0.0.1 -n -q 1 -p 150 >$null
$obj.SendKeys('senhasecreta')
pathping 127.0.0.1 -n -q 1 -p 150 >$null
$obj.SendKeys('{ENTER}');```


推荐阅读