Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automating reports that are generated from a website

Status
Not open for further replies.

cojast08

Technical User
Apr 4, 2008
25
0
0
US
I am fairly new to Powershell but thought I would give it a try to attempt to automate some reporting that we have to do. So if you could please explain your answers for what it is doing i would appreciate it.

These are the steps that we doing manual. Go to website and log in using our AD account. Then navigate through a few menus until we get to the page to run the report. The report requires input for range of date and which dept. before it can run.

So my first step is to get the script to log into the website. I first tried to use Get-Credentials which worked great on other scripts that i have used but i can not figure out how to input the username and password into the website fields and for it to then login without me click the login button. The below code is where i started.

#Get Admin Credentials
Function Get-Login {
Clear-Host
Write-Host "Please provide admin credentials (for example DOMAIN\admin.user and your password)"
$Global:Credential = Get-Credential
}
Get-Login

I then found where i could try using invoke-webrequest instead of get-credentials. The problem with that is 1. it appears the username and password has to be hard coded in the script i need it to prompt the user so they use their own credentials and 2. when i did try testing i just kept getting a null array error. Below is the code i was using along with the error...

$R=Invoke-WebRequest -SessionVariable test
$test
$Form = $R.Forms[0]
$Form | Format-List
$Form.fields
$Form.Fields["userid"]="name"
$Form.Fields["password"]="pass"
$R=Invoke-WebRequest -Uri (" + $Form.Action) -WebSession $test -Method POST -Body $Form.Fields

USERID is the name of the input field and Password is the name for the second input field
I am getting this error

Cannot index into a null array.
At C:\logon test.ps1:6 char:1
+ $Form.Fields["userid"]="name"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: :)) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\logon test.ps1:7 char:1
+ $Form.Fields["password"]="pass"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: :)) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

I know i can use a simple variable to get the web page to work but the issue with that is it shows the password in plain text and i need it to be hidden/secured since it is the username AD account i do not want anyone else to see it.

Any help would be greatly appreciated
 
Here is how you can get the user credentials:
1. The user name can be added to the end of the "Domain\" or you can omit "Domain\" altogether if you wanted. If you keep it change "Domain\" to match your domain or server. I use it so that the users don't need to remember, plus it help user know which password to use if they have different passwords for different systems.
2. You will want to change $message to match the system to which you're requesting credentials.

Code:
$title = "User Authentication Required"
$message = "Enter user credentials for Web Server"
$user = "Domain\"
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Blister

Thank you for your response.
Since I am still learning powershell my question now is where does this code go?
Do I still use the invoke-webrequest line to launch the website?
Does this code automatically know which field is the userid and which is the password?
 
It depends on what you are using it. Some cmdlets will take $user_creds as an input. If you need to supply the user name and password separately, you can reference them like this:

I don't know much about the web cmdlets. But you can use this instead of your Get-Login function.




Code:
$title = "User Authentication Required"
$message = "Enter user credentials for Web Server"
$user = "Domain\"
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")


$Form.Fields["userid"]=$user_creds.UserName
$Form.Fields["password"]=$user_creds.Password


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
You could have done the same thing with your Get-Login function. However, your statement to enter the admin credentials is in the Powershell window, not the pop-up requesting the credentials.


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Blister

Thank you again for the help. I was able to get the pop up to request the username but it is not putting it in the fields. Below is what I have. The website opens up and goes to the main page.

# URL of the site that will be launched.
$title = "User Authentication Required"​
$message = "Enter user credentials for MYSITE"​
$user = ""​
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")​
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;​
$IE.navigate($url);​

# Wait a few seconds and then launch the executable.

while ($IE.Busy -eq $true)

{

Start-Sleep -Milliseconds 2000;

}

# The following UsernameElement, PasswordElement, and LoginElement need to be modified first. See the notes at the top
# of the script for more details.

$IE.Document.getElementById(“userid”).value = $User_creds.Username​
$IE.Document.getElementByID(“password”).value = $User_creds.password​
$IE.Document.getElementById(“button-1034-btnIconEl”).Click()​

This is the error I am getting now...

The property 'value' cannot be found on this object. Verify that the property
exists and can be set.
At C:\logon test.ps1:27 char:1
+ $IE.Document.getElementById("userid").value = $User_creds.Username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: :)) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

The property 'value' cannot be found on this object. Verify that the property
exists and can be set.
At C:\logon test.ps1:28 char:1
+ $IE.Document.getElementByID("password").value = $User_creds.password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: :)) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
 
What version of PowerShell are you using? If it's v3 or greater, I think you can use the Invoke-WebRequest cmdlet, like you showed in the original post. I'm still on v2, so I cannot test any of this to know if it works or not.

And, again, I'm unfamiliar with the web cmdlets. That being said, could you do something like this?

Code:
$title = "User Authentication Required"
$message = "Enter user credentials for MYSITE"
$user = ""
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")

$R = Invoke-WebRequest [URL unfurl="true"]http://www.mysite.com[/URL] -Credential $user_creds


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
I tried that too and it takes me to an about webspage instead of the login page. what I mean is when I open up IE and I type in it will automatically direct me to However in the script below when the invoke runs it takes me to a web address of about:/web/login which is a blank website that says navigation to webpage was canceled.

if I put the website in the invoke-webrequest as then I still get the navigation canceled but the about:/ has nothing behind it.

$title = "User Authentication Required"
$message = "Enter user credentials for mysite"
$user = ""
$user_creds = $host.ui.PromptForCredential($title, $message, $user, "")


Invoke-WebRequest -Uri " -Credential $user_creds

I am using v5 powershell... and I believe the website is made with javascript if that matters
 
I'm not sure what to expect to get back. However, I was able to test it on a system with PowerShell v3, and it appeared to work. I didn't get any errors and I did get a result stored in $R (I tried connecting to the web version of our email). I wonder if it's something with the web page you're trying to access. Also, I didn't have the page visibility set to true.

Hopefully, someone with some knowledge on Invoke-WebRequest will chime in...


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top