INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Automating reports that are generated from a website

Automating reports that are generated from a website

(OP)
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 http://www.MYSITE.com -SessionVariable test
$test
$Form = $R.Forms[0]
$Form | Format-List
$Form.fields
$Form.Fields["userid"]="name"
$Form.Fields["password"]="pass"
$R=Invoke-WebRequest -Uri ("http://www.mysite.com" + $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

RE: Automating reports that are generated from a website

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.

RE: Automating reports that are generated from a website

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

RE: Automating reports that are generated from a website

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.

RE: Automating reports that are generated from a website

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.

RE: Automating reports that are generated from a website

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




RE: Automating reports that are generated from a website

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 http://www.mysite.com -Credential $user_creds 


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

RE: Automating reports that are generated from a website

(OP)
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 http://mysite it will automatically direct me to http://mysite/web/login.
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 http://mysite/web/login 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 "http://mysite/" -Credential $user_creds

I am using v5 powershell... and I believe the website is made with javascript if that matters

RE: Automating reports that are generated from a website

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close