运行 Powershell 脚本新建对象时出错:检索 COM class 工厂

Error while running Powershell script New-Object : Retrieving the COM class factory

在一些 SO 尊敬的成员的帮助下,我达到了可以 运行 下面代码的地步。但碰巧这个脚本 运行 在中间。 我得到的ERROR终于贴出来了

TILL NOW : 我重新启动了我的系统很多次只是为了确保它不是一些系统问题但没有运气。

请注意,我在代码中添加了以下三行,再次确保它是因为某些 IE。

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

代码从这里开始:

 $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

错误

新对象:为 CLSID 为 {0002DF01-0000-0000-C000-000000000046} 的组件检索 COM class 工厂失败 由于以下错误:800706bf 远程过程调用失败且未执行。 (HRESULT 的异常: 0x800706BF)。 在 D:\script.ps1:22 char:12 + $ie = 新对象 -COMObject InternetExplorer.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : ResourceUnavailable: (:) [新对象], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

更新 根据 Micky 的建议,我将 -COMobject 置于 Foreach 循环之外。但是脚本在几次循环后仍然出错:

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

这应该是评论,但我没有足够的评论点。 基本上是 Micky 所说的……只创建一次 $ie 对象并在循环中重复使用它,如下所示:

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

注意:$ie.Quit() 是一个函数,因此它需要空括号。

此外,您可以在脚本末尾触发垃圾回收以处理未使用的对象:

[System.GC]::Collect()

这意味着 $link 是 $null。因此,要么 $control 中存储的值有误,要么该特定产品名称不再可用。在调用其 click() 方法并执行之后的所有操作之前,您应该检查 $link 的值,因为它们都是基于单击 link 的假设。