执行PowerShell脚本触发0xc0000409异常崩溃求助
PowerShell执行远程脚本时因webio.dll崩溃的解决方法
问题背景
执行以下CMD命令运行PowerShell脚本时,PowerShell频繁崩溃:
powershell.exe -ExecutionPolicy remotesigned C:\Temp\psScript\test_01.ps1
事件日志记录显示崩溃源于webio.dll,异常代码为0xc0000409,涉及的脚本内容为:
$password = ConvertTo-SecureString "5up" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential("root",$password) invoke-command -computername "192.168.2.254" -credential $cred -scriptblock { ping.exe 192.168.2.1 -n 15 -l 32 }
解决方法
调整远程命令的输出处理逻辑
webio.dll负责网络IO操作,崩溃大概率和远程命令的实时输出传输有关。可以先在远程端捕获输出再返回,避免流传输异常:invoke-command -computername "192.168.2.254" -credential $cred -scriptblock { # 将ping输出转为字符串后返回 $pingOutput = ping.exe 192.168.2.1 -n 15 -l 32 | Out-String return $pingOutput }或者直接将输出写入远程机器的本地文件,后续按需获取:
invoke-command -computername "192.168.2.254" -credential $cred -scriptblock { ping.exe 192.168.2.1 -n 15 -l 32 > C:\temp\ping_result.txt }修复或更新系统组件
webio.dll是系统核心文件,版本不兼容或损坏会引发崩溃:- 执行系统文件检查命令修复损坏文件:
sfc /scannow - 安装最新的Windows累积更新,将系统组件(包括
webio.dll)更新到匹配版本。
- 执行系统文件检查命令修复损坏文件:
修改执行策略的设置方式
避免在CMD命令行传递执行策略参数,改为在脚本内部设置进程级执行策略:
修改test_01.ps1开头添加:Set-ExecutionPolicy RemoteSigned -Scope Process -Force之后在CMD中直接执行:
powershell.exe C:\Temp\psScript\test_01.ps1改用持久化会话执行命令
先建立持久PSSession再执行远程命令,可能规避临时会话的IO异常:$password = ConvertTo-SecureString "5up" -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential("root",$password) # 建立持久会话 $session = New-PSSession -ComputerName "192.168.2.254" -Credential $cred # 在会话中执行命令 Invoke-Command -Session $session -ScriptBlock { ping.exe 192.168.2.1 -n 15 -l 32 } # 关闭会话 Remove-PSSession $session检查远程机器的WinRM配置
远程端WinRM服务异常可能导致传输错误,在远程机器上重新配置WinRM:winrm quickconfig -quiet确保WinRM服务正常运行,端口(默认5985/5986)未被防火墙阻断。
内容的提问来源于stack exchange,提问作者lan yu
相关产品推荐
相关产品推荐

