Author Posts

December 2, 2015 at 7:35 am

I am having really difficult time doing this and hoping that someone already did something similar and can get me through this "wall" in front of me.

I have various consoles that I am working with and my goal is to schedule a task that Exports reports from these vendors and send them in an email or save them to the drive.

The vendor web consoles I am working with are Trend Micro and Bit9.

In both cases, I am not able to authenticate using invoke-webrequest (unless I am authenticated and just can't tell)

The only way so far, was to use InternetExplorer.Application. This way, I am able to navigate all the way to the "Export" button, but it pulls up a dialog to Save/Save As and requires manual intervention and IE must be visible (this defeats my scheduled task requirement)

Below is my code for IE

$URI = "https://SERVER:4343/officescan/console/html/cgi/cgiChkMasterPwd.exe"
$ie = new-object -com InternetExplorer.Application
$ie.Visible = $true
$ie.navigate($URI)
do {sleep 1} until (-not ($ie.Busy))
$doc = $ie.document
If ($doc.getelementsbytagname('a') | ? {$_.href -like $URI}) {
$link = $doc.getelementsbytagname('a') | ? {$_.href -like $URI}
$link.click()
}
do {sleep 1} until (-not ($ie.Busy))

$UserName = $doc.getElementById('UserName')
$Password = $doc.getElementById('T122')
$Submit = $doc.getElementById('button22')

$UserName.value = 'UserName'
$Password.value = 'Password'
$Submit.Click()
do {sleep 1} until (-not ($ie.Busy))

$ie.navigate('https://SERVER:4343/officescan/console/html/cgi/cgiShowClientAdm.exe?id=1000')
do {sleep 1} until (-not ($ie.Busy))
$ie.Navigate2('javascript:jsCCExport(0)')

My code using Invoke-WebRequest

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#$creds = Get-Credential
$URI = 'https://ServerHostName:4343/officescan/console/html/cgi/cgiChkMasterPwd.exe'
$website = Invoke-WebRequest -URi $URI -SessionVariable Trend
$Form=$website.Forms[0]
$form.fields.UserName = 'UserName'
$form.fields.T122 = 'Password'
$page=Invoke-WebRequest -Uri 'https://ServerHostName:4343/officescan/console/html/cgi/cgiShowClientAdm.exe?id=1000$' -WebSession $Trend -Method POST -Body $form.Fields

December 2, 2015 at 10:00 am

That sort of browser automation can be a real pain in the butt. If your app doesn't offer a web API that is friendly to Invoke-WebRequest or Invoke-RestMethod (which in this case, it doesn't seem to; you shouldn't have to be monkeying around with forms or javascript stuff), then you might have better luck by looking into Selenium (http://www.seleniumhq.org/). It should hopefully give you less of a headache than trying to use the InternetExplorer COM object.

December 3, 2015 at 5:38 am

Thank you Dave,

it looks to me that with SeleriumHQ I would be forced to have the browser window open and therefore I cant have these exports run in the background...

Unless there is something I've missed...