在terminal works中逐行输入时运行我的powershell脚本错误

在terminal works中逐行输入时运行我的powershell脚本错误,powershell,powershell-2.0,Powershell,Powershell 2.0,我正在尝试创建一个powershell脚本,它将帮助实现自动化(从网站收集数据) 我在windows powershell ISE中的脚本(.ps1)中保存了几行命令(我在下面裁剪了一小部分) 我的问题是,当我从上面逐行复制粘贴到终端时,它会返回我想要的结果 但是,当我使用ise运行实际脚本时,会出现各种错误,例如 You cannot call a method on a null-valued expression. At C:\Users\--\--\script.ps1:13 char:

我正在尝试创建一个powershell脚本,它将帮助实现自动化(从网站收集数据)

我在windows powershell ISE中的脚本(.ps1)中保存了几行命令(我在下面裁剪了一小部分)

我的问题是,当我从上面逐行复制粘贴到终端时,它会返回我想要的结果

但是,当我使用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
不能对空值表达式调用方法。
在C:\Users\--\script.ps1:13 char:47

+$currentDocument.IHTMLDocument3_getElementById实例尚未收到所有数据。加

do{Start Sleep-Seconds 1}直到($ieObject.ReadyState-eq 4)

在实例化$ieObject之后,这似乎起到了作用


我建议不要自动化Internet Explorer,这是上世纪的技术。如果需要从PowerShell脚本解析HTML文档,使用内置的Invoke WebRequest是最简单的方法。

在导航后添加2秒延迟,然后重试。@Drew感谢您的建议!现在为我工作!嗨,谢谢你的回复。我肯定会使用webRequest功能,但是我一直在VM环境中使用powershell 2.0,所以我认为这是唯一的方法。我尝试插入您列出的do-until循环,我是否在实例化$ieobject下面执行此操作?或者在下面的行中。谢谢编辑:我使用的是powershell 2.0,我不确定是否支持此暂停?我使用了
启动睡眠-s2
,在我按照drew上面所说的导航之后,它现在似乎正在工作。谢谢你的帮助!当然,不用担心——如果答案有帮助,请向上投票;)
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