powershell - 如何通过 Powershell / Selenium-Chrome 驱动程序对防火墙网页进行身份验证?
问题描述
我在 Powershell 中开发了几个脚本来自动配置我工作的机器,简化并避免手动配置大量工作站的繁重工作。基本上他们使用禁用 UAC、自动登录和任务计划程序。
图片:https ://i.stack.imgur.com/pkuDl.png
最大的问题是使用要求通过浏览器进行身份验证的 Fortinet 防火墙,因为许多应用程序使用通过 SMB 安装在服务器上的文件,因此有必要进行此身份验证,因为没有它就不可能向服务器进行身份验证的算法,导致它在课程中失败。
图片:https ://i.stack.imgur.com/bqZAR.png
但是,通过 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 执行此任务,但我还有另一种选择。
谢谢!
解决方案
为什么不在 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}');```
推荐阅读
- python-3.x - python中sqlite3中的“没有这样的列错误”
- html - 使写在文本区域中的文本显示在另一页上
- json - 仅在有效时从 Flask 请求中获取 JSON
- javascript - 如何清除表单字段一次而不是每次触发焦点事件时
- php - $.post 仅在 loo jquery 中提交第一行结果
- wpf - 是否可以更改在 XAML 中使用 StaticResource 元素实例化的资源的属性?
- c++ - 卡在 PyObject_GetAttrString() :如何从我的 c++ 代码中获取我的 python 函数脚本
- node.js - Node JS:如何创建窗口?(没有 HTML)
- java - 如何按日期/时间对 Uri 数组进行排序?
- unity3d - Visual Studio 2019 经常失去打开 Unity 项目的能力(不支持)