I am into devOps for a week. The basic building block is to automate those systems. I have to say the PowerShell is a good tool to do this kind of work from a Win32 box. My first task is to call some services in a web site. I can find those services URL from the web page but need to go through some authentication processes.
The Invoke-WebRequest cmdlet + Chrome's network monitoring turns out to be the perfect solution to do this work.
First step is to understand how the web page performs.
I open the Chrome network monitoring tool by right click the web page and select "inspect". It shows the current DOM structure of displaying page. Click the "network" tab and select 'preserve log". The "preserve log" feature will keep all the logs even the page changes.
The Invoke-WebRequest cmdlet + Chrome's network monitoring turns out to be the perfect solution to do this work.
First step is to understand how the web page performs.
I open the Chrome network monitoring tool by right click the web page and select "inspect". It shows the current DOM structure of displaying page. Click the "network" tab and select 'preserve log". The "preserve log" feature will keep all the logs even the page changes.
Now the target web page address can be pasted to Chrome and lots of log will be generated. I can see many page direction and form submission during this process (sorry, I cannot paste screenshot. 😉)
Step 2: Write down all the pages "network" tab shows after the initial web page is loaded. Please write down those web addresses.
Now the simulation part starts. There are two variables needed for Invoke-WebRequest cmdlet:
- SessionVariable: Browser maintains a session between client and server. So "-sessionVariable" parameter is a must. It will create a mutable variable to store session information between client and server, e.g. cookies.
- MaximumRedirection: PowerShell can automatically redirect to next page if you set the value to this variable > 0. I recommend to use 0 as initial test so you can know what is going on exactly.
Now let's the fun begin.
Step 3: open PowerShell
Step 4: run $r0 = invoke-webrequest -uri
-SessionVariable sessionVarialbe0 -MaximumRedirection 0
-SessionVariable sessionVarialbe0 -MaximumRedirection 0
This will create the session variable "sessionVariable0". There is an error shows the maximum direction step is smaller. Please ignore this error.
The cmdlet returns a $r0 variable, please check the $r0 content and you will find the web page address and redirection address from header field. Make sure the redirection address equals the one you get from Chrome network monitoring page (in step 2)
Step5: $r1 = inovke-webrequest -uri -webSession $sessionVariable0 -MaximumRedirection 0
Repeat the check performed on $r0.
if you cannot find the redirection address, please refer to "Form" variable in the $r1. You can invoke the address in the Form's action field and submit the fields in the HTTP request body part. For example:
$addressInForm = $r1.Forms[0].Action
$r2 = invoke-webrequest -uri $addressInForm -SessionVariable sessionVarialbe0 -MaximumRedirection 0 -Method Post -UseDefaultCredentials -Body $r1.Forms[0].Fields
you might find the default browser opens, that's because PowerShell uses default browser to parse. The browser might show some error message, please ignore.
You can repeat the above steps until you get the final destination. And the sessionVariable0 is the treasure from this journey, it contains all the information you need for future automation work.
Happy hacking automation. ;)
No comments:
Post a Comment