kev*_*vin 6 html powershell powershell-5.0
我一直在运行密码过期脚本 6 个月没有任何问题。该脚本将读取静态 html 文件并更改内存中的一些内容,然后将向所有密码过期的用户发送 html 电子邮件。
脚本似乎在过去一周左右的时间里坏了。经过进一步调查,我将错误范围缩小到 Powershell 应该创建新 ComObject 并将该 HTML 文件写入 ComObject 的部分。
我现在收到错误:
No coercion operator is defined between types 'System.Array' and 'System.String'
At line:1 char:1
+ $html.write($source);
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException
Run Code Online (Sandbox Code Playgroud)
当我在代码行下方运行时会发生上述错误:
$html = New-Object -ComObject "HTMLFile"
$src = Get-Content -path "./passwordreminder.html" -Raw
$html.write($src)
Run Code Online (Sandbox Code Playgroud)
当我调用该write()方法时,出现错误。
由于它在过去 6 个月中一直运行良好,我能想到的唯一改变是我的 powershell 版本。我相信当我开始运行这个脚本时,我使用的是 Powershell v4.0,但在 Windows 更新之后,我猜 Powershell 现在是 v5.0。见下文 :
Name Value
---- -----
PSVersion 5.0.10105.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 10.0.10105.0
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
Run Code Online (Sandbox Code Playgroud)
该脚本在 Windows Server 2012 R2 操作系统上运行。
谁有想法?
我在其他问题中看到了一些建议,要求IHTMLDocument2_write()在 ComObject 上使用该方法,但是当我尝试调用它时,该方法不存在。
更新 :
我能够确认这在我的 Powershell 版本中确实已损坏。
我只能在具有相同操作系统但低于 Powershell 版本的不同服务器上测试相同的代码:
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34014
BuildVersion 6.3.9600.17090
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
Run Code Online (Sandbox Code Playgroud)
并且代码按预期工作。
有人知道这个新版本的 Powershell 可以使用什么吗?
如果您提供 UCS-2 字节数组而不是字符串,这似乎可以正常工作:
$html = New-Object -ComObject "HTMLFile"
$src = Get-Content -path "./passwordreminder.html" -Raw
$src = [System.Text.Encoding]::Unicode.GetBytes($src)
try
{
# This works in PowerShell 4
$html.IHTMLDocument2_write($src)
}
catch
{
# This works in PowerShell 5
$html.write($src)
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用 Internet Explorer COM 对象:
$ie = New-Object -COM 'InternetExplorer.Application'
$ie.Navigate("file://$($PWD.Path)/passwordreminder.html")
do {
Start-Sleep -Milliseconds 100
} until ($ie.ReadyState -eq 4)
# do stuff
Run Code Online (Sandbox Code Playgroud)
不过,我没有 PowerShell v5,所以无法测试。如果 HTMLFile 损坏,这也可能是这样。
如果需要重复运行该方法,您可以在外循环中调用该Navigate()方法(以及等待其完成加载页面的循环)。
$ie = New-Object -COM 'InternetExplorer.Application'
foreach (...) {
$ie.Navigate("file://$($PWD.Path)/passwordreminder.html")
do {
Start-Sleep -Milliseconds 100
} until ($ie.ReadyState -eq 4)
# do stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10020 次 |
| 最近记录: |