首页 文章

运行Powershell脚本时出错New-Object:检索COM类工厂

提问于
浏览
0

在一些SO尊敬的成员的帮助下,我达到了可以在代码下运行的程度 . 但碰巧这个脚本在中间耗尽 . The ERROR I get is pasted at last.

直到现在:我重新开始我的系统次数只是为了确保它不是一些系统问题但没有运气 .

请注意我在代码中添加了以下三行,再次确保它是因为一些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

ERROR

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

UPDATE 根据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

2 回答

  • 1

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

  • 2

    这应该是一个评论,但我没有足够的评论 . 基本上是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()
    

相关问题