Sleep time in power shell script

I have the script below that I am running on a 2008R2 server with Powershell 3 installed. I am trying to use the script to log in to one of our web servers and write the logged in pass fail to host.

It does not log into the site and I believe it is because the log in page has not finished loading when it tries to log in.

I there any way to break it up and add sleep time to let the pages load before entering id an pw and writing pass fail to host? Also is there any way to make IE viewable while this is processing?

$r = Invoke-WebRequest $url -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"

#sleep -seconds 15

$r | out-file "C:\monitorFolder\httpcopy.html"
if ($r.Content -match $contains)
{
    Write-Host "Success($deviceName)=The web page contains the string '$contains'.";
}
else
{
    Write-Host "Failure($deviceName)=The web page does not contain the string '$contains'.";
}
LVL 1
jimmylew52Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
Well, addressing what should be the easy half of the question...
> I there any way to break it up and add sleep time to let the pages load before entering id an pw and writing pass fail to host?

In Powershell 3.0, you can use Start-Sleep...
jimmylew52Author Commented:
Where in this line would it be put or where would it be put? When I put it anywhere all I get is an error.

$r = Invoke-WebRequest $url -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"
Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
I believe it would be something along the lines of:
$r = Invoke-WebRequest $url -SessionVariable Sess

Start-Sleep -seconds 15

#set $formfields, and adjust $url value

$r = Invoke-WebRequest $url -WebSession $Sess -Method Post -Body $formFields -ContentType "application/x-www-form-urlencoded"

Open in new window

Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

jimmylew52Author Commented:
Yep, just like what I keep getting, errors:

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument
is null or empty. Supply an argument that is not null or empty and then try
the command again.
At C:\MonitorFolder\Invoke.ps1:17 char:24
+ $r = Invoke-WebRequest $url -SessionVariable Sess
+                        ~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-WebRequest], ParameterB
   indingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.InvokeWebRequestCommand

Invoke-WebRequest : Cannot validate argument on parameter 'Uri'. The argument
is null or empty. Supply an argument that is not null or empty and then try
the command again.
At C:\MonitorFolder\Invoke.ps1:23 char:24
+ $r = Invoke-WebRequest $url -WebSession $Sess -Method Post -Body $formFields
-Co ...
+                        ~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-WebRequest], ParameterB
   indingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Commands.InvokeWebRequestCommand
Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
I apologize.  What value did you have in $url originally?  I believe you'd use that value in the first request, and in the second, reference the action property on the form.
jimmylew52Author Commented:
Thanks, that resolved the error but  does not appear to log in to the website. Not the question I asked but important for what I am trying to do.

Any way to make ie visable is this code> Might help see if I am getting anywhere.
jimmylew52Author Commented:
Whole code:

$myUrl = "HTTPS://mysite.com"
$formFields = @{username='UID';password='PW!'}
$r = Invoke-WebRequest $myUrl -SessionVariable Sess
Start-Sleep -seconds 15
$r = Invoke-WebRequest $myUrl -WebSession $Sess -Method Post -Body $formFields
Start-Sleep -seconds 15
$r | out-file "C:\Folder\invoke.html"
Rich WeisslerProfessional Troublemaker^h^h^h^h^hshooterCommented:
> Any way to make ie visable is this code> Might help see if I am getting anywhere.

You can output the value of $r at any stage, and read off what it's saying.... and you can kind easily get that into IE for testing purposes...
$r.RawContent | out-file <tempfilepathandname.htm>
invoke-expression <tempfilepathandname.htm> 

Open in new window


It's ugly, but it'll get you there.  But I'm making a host of assumptions, including that IE is your default browser, and you don't mind seeing the HTTP information on the top, and you don't need to view graphics, etc.

A more knowledgeable expert could steer you down the path of creating an IE object with
$myIE=New-object -COM InternetExplorer.Application
$myIE.Visible=$TRUE

Open in new window

Then sending the responses powershell is receiving into that object.  Unfortunately, that I can't help with more than pointing in that direction.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimmylew52Author Commented:
Thank You,  You have been most helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.