我试图创建一个自动指向自动脚本的脚本 IE中的安全网站,输入登录凭据,然后转到数据库选项卡 和一个数据库链接下载。这将不需要用户交互和 通过Windows任务调度程序启动。
然而,它约占75%的时间。我是新来的 powershell并犯了新手错误,所以任何帮助或方向都会 非常感谢。谢谢!
PowerShell.exe -windowstyle hidden {
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
Write-Output "$(Get-TimeStamp) Script Executed $dc" | Out-file C:\Users
\JohnSmith\Desktop\Script\ScriptLog.txt -append
$username = "admin"
$password = "admin123"
$ie = new-object -com InternetExplorer.Application
#Navigate to the login page
$ie.navigate("Login Page")
#Wait for the page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$ie.visible = $true #comment this line after debugging
#Assigning DOM to $doc variable
$doc = $ie.document
try {
$usernameField = $doc.getElementById('userName')
#write-host $usernameField
$usernameField.value = $username
write-host $username
$passwordField = $doc.getElementById('password')
$passwordField.value = $password
write-host $pass
#Find and click the submit button
$submitButton = $doc.getElementById('login')
write-host $submitButton
$submitButton.click()
#Wait until login is complete
do {sleep 1} until (-not ($ie.Busy))
} catch {$null}
do {sleep 1} until (-not ($ie.Busy))
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
$ie = new-object -com internetexplorer.application
$ie.visible=$true
$ie.navigate('Database Download' )
while($ie.busy) {sleep 1}
$link = $ie.Document.getElementsByTagName('A') | where-object
{$_.innerText -eq 'Download the Complete Database'}
$link.click()
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Internet Explorer')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("{TAB}");
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Internet Explorer')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
(New-Object -COM 'Shell.Application').Windows() | Where-Object {
$_.Name -like '*Internet Explorer*'
} | ForEach-Object
{
$_.Quit()
[Runtime.Interopservices.Marshal]::ReleaseComObject($_)
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
答案 0 :(得分:0)
这是我完成的脚本。它并不像我希望的那样优雅,但是现在它完成了我需要做的事情!谢谢!
PowerShell.exe -windowstyle hidden {
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
#Creates appending log file for everytime script runs
Write-Output "$(Get-TimeStamp) Script Executed $dc" | Out-file C:\Users
\JohnSmith\Desktop\DatabaseScript\DatabaseScriptLog.txt -append
$username = "admin"
$password = "admin123"
#write-host $pass
#Create the IE com object
$ie = new-object -com InternetExplorer.Application
#Navigate to the login page
Start-Process 'https://www.LoginPageToTheSecureSite'
<#$ie.navigate("https://www.LoginPageToTheSecureSite")
#Wait for the page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$ie.visible = $true
#Assign the DOM to the $doc variable
$doc = $ie.document
try {
#Find the username field and set the value to that of variable
$usernameField = $doc.getElementById('User ID')
#write-host $usernameField
$usernameField.value = $username
write-host $username
#Find the password field and set the value to that of the result
#of a call to the get-password function with the parameter defined at
top
$passwordField = $doc.getElementById('Password')
$passwordField.value = $password
write-host $pass
#Submit button
$submitButton = $doc.getElementById('Login')
write-host $submitButton
$submitButton.click()
#Wait until login is complete
do {sleep 1} until (-not ($ie.Busy))
} catch {$null}
#Wait for page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
$wshell.AppActivate('Internet Explorer')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
Start-Process 'https://www.DownloadTheDatabaseSelectionTab'
$wshell.AppActivate('Internet Explorer')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
Start-Process 'https://www.DownloadThisAsZipFile.zip'
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Opening Database Zip File')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Internet Explorer')
Sleep 1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("{TAB 2}");
#Closes every instance of IE
(New-Object -COM 'Shell.Application').Windows() | Where-Object {
$_.Name -like '*Internet Explorer*'
} | ForEach-Object {
$_.Quit()
}
#Releases COM Object, cleans up.
(New-Object -COM 'Shell.Application').Windows() | Where-Object {
$_.Name -like 'Internet Explorer'
} | ForEach-Object {
$_.Quit()
[Runtime.Interopservices.Marshal]::ReleaseComObject($_)
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}