Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

After help from some of SO esteemed members I reached to the point where I can run below code. But it happens that this script runs out in middle. The ERROR I get is pasted at last.

TILL NOW : I re-started my system number of times just to make sure it isn't some system problem but no luck.

Please note I added following three lines in code, again to make sure that it is something because of some IE.

**# try few things 
$result = $null
$ie.quit
get-process iexplore | stop-process**

CODE STARTS HERE :

 $controls   = Get-Content ("d:\users\yarora\desktop\hkd1000.txt")
    Add-Type -AssemblyName 'System.Web'

    Function getStringMatch
    {


       Foreach ($control In $controls)
        {
         $ie = New-Object -COMObject InternetExplorer.Application


    #for testing purpose
    $control

    #encode the special characters in URL like %,& etc.

    $controlUri = [System.Web.HttpUtility]::UrlEncode($control)
    $site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")

    #$ie.ReadyState

        while ($ie.Busy){ sleep -Milliseconds 100 }

            $link = $null
            $link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
            $link.click()

        while ($ie.Busy){ sleep -Milliseconds 100 }

    [regex]$regex = 
    @'
    (?s).+?<A href="/generics/.*?">(.*?)</A>
    '@

    $result = $regex.Matches($ie.Document.body.outerHTML) |
    foreach {
              [PSCustomObject]@{
              Name = $control
              Saltname = $_.Groups[1].value 
             }


    }

    $result | out-file d:\result.txt -append 

    # try few things 
    $result = $null
    $ie.quit
    get-process iexplore | stop-process

    }
    }
    getStringMatch

ERROR

New-Object : Retrieving the COM class factory for component with CLSID {0002DF01-0000-0000-C000-000000000046} failed due to the following error: 800706bf The remote procedure call failed and did not execute. (Exception from HRESULT: 0x800706BF). At D:\script.ps1:22 char:12 + $ie = New-Object -COMObject InternetExplorer.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

UPDATE As per suggestion from Micky, I placed -COMobject out of Foreach loop. But the script still errors out after few loops :

You cannot call a method on a null-valued expression.
At D:\desktop\hkdforsalt1000.ps1:41 char:9
+         $link.click()
+         ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
share|improve this question
1  
Looks like you are running out of resources, try putting $ie = New-Object -COMObject InternetExplorer.Application before the foreach loop. –  Micky Balladelli Jan 12 at 18:18
1  
That means that $link is $null. So either there is an error in the value stored in $control, or that specific product name is no longer available. You should check for the value of $link prior to invoking its click() method and performing all the actions after that, since they are all based on the assumption that the link was clicked. –  Micky Balladelli Jan 13 at 8:18

2 Answers 2

up vote 1 down vote accepted

That means that $link is $null. So either there is an error in the value stored in $control, or that specific product name is no longer available. You should check for the value of $link prior to invoking its click() method and performing all the actions after that, since they are all based on the assumption that the link was clicked.

share|improve this answer

This should probably be a comment, but I do not have enough points to comment. Basically what Micky said… Create your $ie object only once and reuse it inside the loop like this:

Function getStringMatc {
    $controls   = Get-Content ("d:\users\yarora\desktop\hkdforsalt1000.txt")
    $ie = New-Object -COMObject InternetExplorer.Application

    Foreach ($control In $controls){
        $controlUri = [System.Web.HttpUtility]::UrlEncode($control)
        $site = $ie.Navigate("https://www.healthkartplus.com/search/all?name=$controlUri")
        #rest of your code here...
    }
    # more code here...

    $ie.quit()
}

NOTE: $ie.Quit() is a function so it needs empty brackets.

Also, you can trigger a garbage collection at the end of your script to dispose of unused objects:

[System.GC]::Collect()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.