运行 我的 powershell 脚本错误,在逐行输入终端时有效
Running my powershell script errors, while entering line by line into terminal works
我正在尝试创建一个强大的 shell 脚本来帮助实现自动化(从网站收集数据)。
我在 windows powershell ISE 的脚本 (.ps1) 中保存了几行命令(我在下面修剪了一小部分)
$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('---')
$currentDocument = $ieObject.Document
$currentDocument.IHTMLDocument3_getElementById("serialno")
我的问题是,当我从上方逐行复制粘贴到终端时,它 returns 我想要的结果。
然而,当我 运行 使用 ise 的实际脚本时,我收到各种错误,例如
You cannot call a method on a null-valued expression.
At C:\Users\--\--\script.ps1:13 char:47
+ $currentDocument.IHTMLDocument3_getElementById <<<< ("serialno")
+ CategoryInfo : InvalidOperation: (IHTMLDocument3_getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我觉得 powershell 脚本的执行方式与我需要的不同,任何帮助都将非常有用。谢谢
实例尚未收到所有数据。添加
do { Start-Sleep -Seconds 1} until ($ieObject.ReadyState -eq 4)
在实例化 $ieObject 之后,这似乎可以解决问题。
我建议不要自动化 Internet Explorer,那完全是上个世纪的技术。如果您需要从 PowerShell 脚本解析 HTML 文档,使用内置的 Invoke-WebRequest 是最简单的方法。
我正在尝试创建一个强大的 shell 脚本来帮助实现自动化(从网站收集数据)。
我在 windows powershell ISE 的脚本 (.ps1) 中保存了几行命令(我在下面修剪了一小部分)
$ieObject = New-Object -ComObject 'InternetExplorer.Application'
$ieObject.Visible = $true
$ieObject.Navigate('---')
$currentDocument = $ieObject.Document
$currentDocument.IHTMLDocument3_getElementById("serialno")
我的问题是,当我从上方逐行复制粘贴到终端时,它 returns 我想要的结果。
然而,当我 运行 使用 ise 的实际脚本时,我收到各种错误,例如
You cannot call a method on a null-valued expression.
At C:\Users\--\--\script.ps1:13 char:47
+ $currentDocument.IHTMLDocument3_getElementById <<<< ("serialno")
+ CategoryInfo : InvalidOperation: (IHTMLDocument3_getElementById:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我觉得 powershell 脚本的执行方式与我需要的不同,任何帮助都将非常有用。谢谢
实例尚未收到所有数据。添加
do { Start-Sleep -Seconds 1} until ($ieObject.ReadyState -eq 4)
在实例化 $ieObject 之后,这似乎可以解决问题。
我建议不要自动化 Internet Explorer,那完全是上个世纪的技术。如果您需要从 PowerShell 脚本解析 HTML 文档,使用内置的 Invoke-WebRequest 是最简单的方法。