Что не так с моим скриптом powershell?:( - PullRequest
0 голосов
/ 30 мая 2018

Я пытаюсь создать автоматический сценарий, который автоматически направляет на защищенный веб-сайт в 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()

 }

1 Ответ

0 голосов
/ 01 июня 2018

Вот мой законченный сценарий.Это не так элегантно, как хотелось бы, но оно делает то, что мне нужно, сейчас!Спасибо!

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()

 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...