locked
help finding out when ReadyState = 4 in .NET alpplication using powershell RRS feed

  • Question


  • i have a website written in #c.NET when I try and use powershell to log into it I run into isues with DOM and the code below.

        $ie = New-Object -ComObject 'internetExplorer.Application'
        $ie.navigate("https://fasttrng.oursite.com/Login.aspx?ReturnUrl=%2f")
    Write-Host "start" + $ie.ReadyState
        while($ie.ReadyState -ne 4 ) 
        {
         $rs=$ie.ReadyState
         $crs=$rs
         Write-Host "1:" + ">" + $ie.ReadyState + "<"
         start-sleep -m 10
        } 
    $usernamefield = $ie.Document.getElementByID('ctl00_pageContentPlaceHolder_txtUserID')

    what happens here is the first Write-Host "start" + $ie.ReadyState is 0 then the subsequent one are all blank so the page never fully renders and then the getElementByID fails since the page doesn't render this works with static pages and PHP pages any ideas why .NET is different?

    • Moved by 宝宝徐 Monday, September 11, 2017 2:46 AM
    Friday, September 8, 2017 9:01 PM

All replies

  • Hi rm99,

    This forum is discussing Visual Studio Setup and Installation, since your issue related to MSDN subscription, I help you move this case to Scripting > Windows PowerShell  to get a better help.

    Thanks for your understanding.

    Best Regards,



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 11, 2017 2:43 AM
  • Try it like this:

    $ie = New-Object -ComObject 'internetExplorer.Application' $ie.navigate("https://fasttrng.oursite.com/Login.aspx?ReturnUrl=%2f") while ($ie.IsBusy){ sleep -Milliseconds 100} Write-Host $ie.ReadyState
    Write-Host $ie.Document.readyState



    \_(ツ)_/

    Monday, September 11, 2017 3:16 AM
  • Hi rm99,

    You could try the following method:

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, September 11, 2017 7:30 AM
  • Please do not post images of code.  They are not useful.  Use the code posting tool provided.


    \_(ツ)_/

    Monday, September 11, 2017 7:33 AM
  • Hi ,

    Thanks for jrv's kind reminder.

    Please try the following method:

    $username = "username" 
    $password = "password" 
    $ie = New-Object -com InternetExplorer.Application 
    $ie.visible=$false
    $ie.navigate("https://fasttrng.oursite.com/Login.aspx?ReturnUrl=%2f") 
    while($ie.ReadyState -ne 4) {start-sleep -m 100} 
    $ie.document.getElementById("username").value= "$username" 
    $ie.document.getElementById("pass").value = "$password" 
    $ie.document.getElementById("loginform").submit()
    start-sleep 20 
    $ie.Document.body | Out-File -FilePath c:\web.txt

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, September 11, 2017 7:54 AM
  • that doesn't work either the main issue is It never makes it to ReadyState 4

    I can illustrate it by adding some write-host lines see below when it gois into the -ne 4 loop it gets stuck since ReadyState only ends up at blank value...does that make sense?

    $username='username'
    $password='password'
    $ie = New-Object -com InternetExplorer.Application
    $ie.visible=$false
    $ie.navigate("https://fasttrng.oursite.com/Login.aspx?ReturnUrl=%2f")
    Write-Host "before sleep" + $ie.ReadyState
    while($ie.ReadyState -ne 4)
    {
     Write-Host "during sleep" + $ie.ReadyState
     Start-Sleep -seconds 2;
    }
    Write-Host "after sleep" + $ie.ReadyState
    $ie.document.getElementById("username").value= "$username"
    $ie.document.getElementById("pass").value = "$password"
    $ie.document.getElementById("loginform").submit()
    start-sleep 20
    $ie.Document.body | Out-File -FilePath T:\work\scripts\web.txt

    PS T:\work\scripts> T:\work\scripts\Untitled6.ps1
    before sleep + 0
    during sleep + 0
    during sleep +
    during sleep +
    during sleep +
    during sleep +
    during sleep +
    during sleep +
    during sleep +

    what is that page waiting for to render?


    also just for fun I changed the loop to -eq 0 so it did finish and the web.txt was completely blank does that mean anything?


    • Edited by rm99 Monday, September 11, 2017 2:16 PM
    Monday, September 11, 2017 1:48 PM
  • Not a scripting issue.  There is either something wrong with your IE or the website.


    \_(ツ)_/

    Monday, September 11, 2017 5:04 PM
  • Hi rm99

    We know that READYSTATE_COMPLETE (4) indicates “All the data has been received.“

    https://msdn.microsoft.com/en-us/library/ms534361(v=vs.85).aspx

    >> what is that page waiting for to render?

    We don’t know what your page is waiting for.

    Maybe you could contact with the developer of this webpage?

    Or, change to another website try.

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    • Proposed as answer by Hello_2018 Monday, September 18, 2017 7:21 AM
    Tuesday, September 12, 2017 9:30 AM
  • thanks Lana
    Tuesday, September 12, 2017 2:49 PM
  • this is weird here I what i get with that code (slightly modified more Write-Host)

    $ie = New-Object -ComObject 'internetExplorer.Application'
    $ie.navigate("https://fasttrng.usask.ca/Login.aspx?ReturnUrl=%2f")
    Write-Host "before sleep" + $ie.ReadyState
    Write-Host "Document Readystate:" + $ie.Document.ReadyState
    while ($ie.IsBusy){ sleep -Milliseconds 100}
    Write-Host "after sleep" + $ie.ReadyState
    Write-Host "Document Readystate:" + $ie.Document.ReadyState

    I get


    PS C:\Users\rwm132> T:\work\scripts\Untitled7.ps1
    before sleep + 0
    Document Readystate: +
    after sleep +
    Document Readystate: +

    Tuesday, September 12, 2017 2:57 PM
  • Thanks JRV I think you are right it is the website , it is a standard .NET forms login however

    maybe you can have a look at this markup and tell me if you see something odd:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head><title>
    	FAST Single Sign On Suite
    </title><link href="CSS/FASTStyles.css" rel="stylesheet" /><link href="CSS/ClientStyles.css" rel="stylesheet" /></head>
    <body style="background-color: #FFFFFF; border: 1px solid #FFFFFF; margin: 0px 0px 0px 0px;">
        <form name="aspnetForm" method="post" action="./Login.aspx?ReturnUrl=%2f" id="aspnetForm" autocomplete="off">
    <div>
    <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTkyOTc2NDI1Mw9kFgJmD2QWAgIDD2QWBAIBD2QWBGYPFgQeA3NyYwU7aHR0cHM6Ly9mYXN0dHJuZy51c2Fzay5jYTo0NDMvQ2xpZW50X0ltYWdlcy9iYW5uZXJfbG9nby5qcGceA2FsdAUZUmV0dXJuIHRvIFBvcnRhbCBIb21lcGFnZWQCAQ9kFgJmD2QWCGYPZBYCZg8PFgIeBFRleHQFCFZlcnNpb246ZGQCAg9kFgJmDw8WAh8CBQk0LjMuMDguMDlkZAIED2QWAmYPDxYCHwIFDEVudmlyb25tZW50OmRkAgYPZBYCZg8PFgIfAgUFRkFTVEVkZAIFD2QWAgIBD2QWAmYPZBYEZg9kFgYCAQ8PFgIfAgUKU2lnbiBJbiB0b2RkAgMPDxYCHwIFBEZBU1RkZAIFD2QWBgIBDw8WAh8CBQlVc2VyIE5hbWVkZAIFDw8WAh8CBQhQYXNzd29yZGRkAgkPDxYCHwIFB1NpZ24gSW5kZAICD2QWAgIBDxYCHwJlZGSaROouAN8xZf6xMlwT/66QcTspnA==" />
    </div>
    
    <script type="text/javascript">
    //<![CDATA[
    var theForm = document.forms['aspnetForm'];
    if (!theForm) {
        theForm = document.aspnetForm;
    }
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
    //]]>
    </script>
    
    
    <script src="/WebResource.axd?d=CuhVLA9FpqvmBK5NEvEdGJit8d6dK5bHNu7zbcitAheposGhqok-0cXF9BExt5kcy3s-UmSjZnSh9QbSUqHEo-BDR2o1&amp;t=636288738966629520" type="text/javascript"></script>
    
    
    <script src="/WebResource.axd?d=Xy37fFNMURuv5YqqwTwDbSofLDHeoNNRrQibDAuWCkm05EZ-0W7tEfzmAhK4spTjMZAfHJSrx59kFsZwv9UgRU32-vk1&amp;t=636288738966629520" type="text/javascript"></script>
    <div>
    
    	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C2EE9ABB" />
    	<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
    	<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
    </div>
    
        <div>
            
            
    
    <script type="text/javascript">
    
        function BANNER_ToggleEnvInfo() {
            var m_divEnvInfo = $get('ctl00_ctl04_divEnvInfo');
            if (m_divEnvInfo.style.display == '') m_divEnvInfo.style.display = 'none';
            else m_divEnvInfo.style.display = '';
        }
    
    </script>
    
    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="BannerBackground" style="height: 40px;">
        <tr style="height: 40px">
            <td><a title="Return to Portal Homepage" href="https://fasttrng.usask.ca:443/DesktopDefault.aspx"><img src="https://fasttrng.usask.ca:443/Client_Images/banner_logo.jpg" id="ctl00_ctl04_imgClientLogo" style="border: 0px; height: 40px; display:block;" alt="Return to Portal Homepage" /></a></td>
            <td align="right">
                <table id="ctl00_ctl04_tblEnvironment" border="0" cellpadding="0" cellspacing="0" style="height: 40px;">
    	<tr>
    		<td><span id="ctl00_ctl04_lblVersionDescription" class="BannerSettingsText" style="font-weight: bold;">Version:</span>
    		</td>
    		<td style="width: 10px;"></td>
    		<td><span id="ctl00_ctl04_lblVersion" class="BannerSettingsText">4.3.08.09</span></td>
    		<td style="width: 20px;"></td>
    		<td><span id="ctl00_ctl04_lblEnvironmentDescription" class="BannerSettingsText" style="font-weight: bold;">Environment:</span>
    		</td>
    		<td style="width: 10px;"></td>
    		<td><span id="ctl00_ctl04_lblEnvironment" class="BannerSettingsText">FASTE</span></td>
    	</tr>
    </table>
    
                <div id="ctl00_ctl04_divEnvInfo" style="; display: none; z-index: 100000;">
                    <div style="; width: 100%; top: 8px; left: 0px;">
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" style="background-color: #000; opacity: 0.9; padding:10px;">
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">.NET version</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblNETVersion" class="BannerSettingsText"></span></td>
                        </tr>
                        <tr style="height: 4px;"><td colspan="4"></td></tr>
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">OS Verion</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblOSVerion" class="BannerSettingsText"></span></td>
                        </tr>
                        <tr style="height: 4px;"><td colspan="4"></td></tr>
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">Machine Name</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblMachineName" class="BannerSettingsText"></span></td>
                        </tr>
                        <tr style="height: 4px;"><td colspan="4"></td></tr>
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">Processor Count</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblProcessorCount" class="BannerSettingsText"></span></td>
                        </tr>
                        <tr style="height: 4px;"><td colspan="4"></td></tr>
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">User Domain Name</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblUserDomainName" class="BannerSettingsText"></span></td>
                        </tr>
                        <tr style="height: 4px;"><td colspan="4"></td></tr>
                        <tr>
                            <td align="right"><span class="BannerSettingsText" style="font-weight: bold;">User Name</span></td>
                            <td style="width: 10px;"></td>
                            <td><span id="ctl00_ctl04_lblUserName" class="BannerSettingsText"></span></td>
                        </tr>
                        </table>
                    </div>
                </div>
            </td>
           <td style="width: 10px"></td>
        </tr>
    </table>
    
            
            <table width="100%" border="0" cellpadding="0" cellspacing="0" style="height: 1px"><tr><td></td></tr></table>
            <table width="100%" border="0" cellpadding="0" cellspacing="0" style="height: 1px; background-color: #CCCCCC"><tr><td></td></tr></table>
            
            <table width="100%" border="0" cellpadding="0" cellspacing="0" style="background-color: #F2F1F3">
            <tr style="height: 25px">
                <td>
                    
                    
                </td>
                <td align="right">                
                </td>
            </tr>
            </table>
            
            <table width="100%" border="0" cellpadding="0" cellspacing="0" style="height: 1px; background-color: #CCCCCC"><tr><td></td></tr></table>
            <table width="100%" border="0" cellpadding="0" cellspacing="0" style="height: 1px"><tr><td></td></tr></table>
            
            
        
        <table id="ctl00_pageContentPlaceHolder_tblContent" width="100%" border="0" cellpadding="0" cellspacing="0">
    	<tr>
    		<td valign="top">
                    <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr style="height: 40px;"><td></td></tr></table>
    
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" class="TopicHeader">
                        <tr style="height: 35px">
                            <td style="width: 20px"></td>
                            <td class="TopicHeaderText"><span id="ctl00_pageContentPlaceHolder_lblSignIn">Sign In to</span>
    		 <b><i><span id="ctl00_pageContentPlaceHolder_lblApplicationName">FAST</span>
    		</i></b></td>
                        </tr>
                    </table>
                    
                    <table border="0" cellpadding="0" cellspacing="0">
                        <tr style="height:35px">
                            <td></td>
                        </tr>
                    <div id="ctl00_pageContentPlaceHolder_pnlLogin">
    			
                        <tr>
                            <td>
                                <table border="0" width="100%" cellpadding="0" cellspacing="0">
                                    <tr style="height:150px">
                                        <td style="width:25px"></td>
                                        <td><img src="CLIENT_IMAGES/application_login.jpg" alt="" /></td>
                                        <td style="width:25px"></td>
                                        <td style="border-right:solid 1px #cccccc">&nbsp;</td>
                                        <td style="width:35px"></td>
                                        <td>
                                            <table cellpadding="0" cellspacing="0" border="0" class="TopicBody">
                                                <tr>
                                                    <td><span id="ctl00_pageContentPlaceHolder_lblUserName">User Name</span>
    			:</td>
                                                    <td style="width:10px"></td>
                                                    <td style="width:200px"><input name="ctl00$pageContentPlaceHolder$txtUserID" type="text" maxlength="30" id="ctl00_pageContentPlaceHolder_txtUserID" AutoComplete="off" style="width:200px;" /></td>
                                                </tr>
                                                <tr>
                                                    <td><span id="ctl00_pageContentPlaceHolder_lblPassword">Password</span>
    			:</td>
                                                    <td style="width:10px"></td>
                                                    <td style="width:200px"><input name="ctl00$pageContentPlaceHolder$txtPassword" type="password" maxlength="256" id="ctl00_pageContentPlaceHolder_txtPassword" autocomplete="off" style="width:200px;" /></td>
                                                </tr>
                                                <tr style="height:4px"><td></td></tr>
                                                <tr>
                                                    <td colspan="3" align="right">
                                                        <input type="submit" name="ctl00$pageContentPlaceHolder$btnGo" value="Sign In" id="ctl00_pageContentPlaceHolder_btnGo" style="width: 200px;" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    
    		</div>
                    
                    </table>
                    
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" style="height: 50px"><tr><td></td></tr></table>
    
                </td>
    		<td style="width: 1px; background-color: #FFFFFF;"></td>
    		<td style="width: 275px; border-left: 1px solid #CCCCCC; background-color: #F2F1F3" valign="top" align="center">
    				<table width="250px" border="0" cellpadding="0" cellspacing="0">
    				    <tr style="height: 90px;">
    				        <td></td>
    				    </tr>
    				    <tr>
    				        <td valign="top">
    				            
    				        </td>
    				    </tr>
    				</table>
                </td>
    	</tr>
    </table>
    
    
    
            
            <table id="ctl00_tblMasterFooter" width="100%" border="0" cellpadding="0" cellspacing="0" class="FooterBackground">
    	<tr style="height: 55px">
    		<td style="width: 10px;"></td>
    		<td><a href="http://www.mcsl.com" target="_blank"><img src="https://fasttrng.usask.ca:443/CLIENT_IMAGES/footer_logo_millennium.gif" alt="Millennium Computer Systems Ltd." style="border: 0px" /></a></td>
    		<td align="right"><img src="https://fasttrng.usask.ca:443/CLIENT_IMAGES/footer_logo_fast.gif" alt="" style="border: 0px" /></td>
    		<td style="width: 10px;"></td>
    	</tr>
    </table>
    
            
        </div>
        
    
    <script type="text/javascript">
    //<![CDATA[
    WebForm_AutoFocus('ctl00_pageContentPlaceHolder_txtUserID');//]]>
    </script>
    </form>
    </body>
    </html>
    

    Tuesday, September 12, 2017 3:29 PM
  • Post IIS issues here:  http://forums.iis.net

    Post ASP.Net issues here: http://forums.asp.net 


    \_(ツ)_/



    • Edited by jrv Tuesday, September 12, 2017 6:25 PM
    • Proposed as answer by Hello_2018 Monday, September 18, 2017 7:21 AM
    Tuesday, September 12, 2017 6:23 PM