Powershell脚本访问网站,获取一些信息并将其写入平面文件 . 如果我在Powershell中运行脚本它可以很好地工作,但是当我从SQL Server Agent运行它时,它会失败 . 我已经在服务器中向运行包的SQL Server代理用户提供了读/写权限 . 我也尝试过创建一个Step Type:PowerShell,然后从作业中运行脚本;操作系统(CmdExec)使用文件名作为参数来执行powershell命令;操作系统(CmdExec)运行调用Powershell脚本的批处理文件 .

这是powershell脚本:

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $false
$ie.Navigate("https://www.frbservices.org/EPaymentsDirectory/fpddir.txt")
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$Link=$ie.Document.getElementsByTagName("button") | where-object {$_.value -eq "Agree"}
If($Link){
    $Link.click()
}
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$destination = ".\FDRTestFile\FDRFile.txt"
$content = (@($ie.Document.getElementsByTagName("pre"))[0].innerText | Out-
File $destination)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
Remove-Variable ie

工作完成后我收到此错误:

New-Object:由于以下错误,从IClassFactory创建具有CLSID {0002DF01-0000-0000-C000-000000000046}的COM组件实例失败:80004005未指定错误(HRESULT异常:0x80004005(E_FAIL)) . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:1 char:7 $ ie = New-Object -ComObject'internetExplorer.Application'~~~~~~~~~~~~~~~~~~~~~~分类信息:ResourceUnavailable:(:) [New-Object] COMExcept ion FullyQualifiedErrorId:NoCOMClassIdentifiedMicrosoft.PowerShell ~~~~~~~~~~~~~~~~~~ .Comman ds.NewObjectCommandProperty'Visible'在此对象上找不到;确保它存在且可设置 . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:3 char:1 $ ie.Visible = $ true ~~~~~~~~~~~~~~~~~~分类信息:InvalidOperation:(:) [] RuntimeException FullyQualifiedErrorId:PropertyNotFound您无法在空值表达式上调用方法 . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:4 char:1 $ ie.Navigate(“https://www.frbservices.org/EPaymentsDirectory/fpddir.txt”)~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 〜:~~~~~~~~~~ CategoryInfo:InvalidOperation:(:) [] RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:8 char:1 $ Link = $ ie.Document.getElementsByTagName(“button”)| where-object {$ _ .value -eq“... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )[] RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull您不能在空值表达式上调用方法 . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:17 char:15 $ content =(@($ ie.Document.getElementsByTagName(“pre “))[0] .innerText | Out-File ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~ CategoryInfo:InvalidOperation:(:) [] RuntimeException FullyQualifiedErrorId:InvokeMethodOnNull使用“1”参数调用“ReleaseComObject”的异常:“对象引用未设置为对象的实例 . ”在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:18 char:1 [System.Runtime.Interopservices.Marshal] :: ReleaseComObject($ ie)~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~分类信息:未指定:(:) [] MethodInvocationException FullyQualifiedErrorId:NullReferenceException Remove-Variable:找不到变量名字'ie' . 在D:\ SSIS \ FDR \ DownloadFDRFile.ps1:19 char:1删除 - 变量即~~~~~~~~~~~~~~~~~~分类信息:ObjectNotFound :(即:字符串)[删除 - 变量] I temNotFoundException FullyQualifiedErrorId:VariableNotFoundMicrosoft.PowerShell.Commands.R emoveVariableCommand . 处理退出代码0.步骤成功 . ,00:00:02,0,0 ,,,, 0

任何想法为什么会这样?