windows - Powershell脚本中的通用故障错误

标签 windows powershell error-handling powershell-remoting

我有一个Powershell脚本可以从本地服务器获取另一个PS脚本并运行PS脚本
当服务器不可用时,网络中的所有系统都会给出“通用故障”错误,或者一个错误异常带有“1”参数的调用“DownloadString”“远程服务器返回错误:(404)未找到。”

start-sleep 120
$i=0    
While ($true) {
    if ($StatusCode = Test-Connection -ComputerName 192.168.1.1 -Quiet) {
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('http://192.168.1.2/service/ok.ps1'))
Break

     }
     }
friend ,当服务器不可用或内部网络上的系统不可用时,该怎么办?此消息没有出现此错误,请等待系统访问本地网络或服务器?
谢谢
问候

最佳答案

如果服务器在线,请首先ping到chck,如果服务器答复则继续。但是您要ping 192.168.1.1,但是要从192.168.1.2下载文件,因此必须ping 192.168.1.2。还要在测试连接中将ErrorAction设置为SilentlyContinue

start-sleep 120
$i=0    
While ($true) {
    if (Test-Connection -ComputerName 192.168.1.2 -Quiet -ErrorAction SilentlyContinue) {
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('http://192.168.1.2/service/ok.ps1'))
Break

     }
Else {
  "Generic Failure"
     }
当您收到404错误时,服务器上不存在请求的文件。检查URL。如果URL有效,则文件可能已被移动,重命名或删除。服务器似乎在本地网络中,因此请与服务器管理员联系。

关于windows - Powershell脚本中的通用故障错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63096230/

相关文章:

json - ConvertFrom-Json 输出不适用于 Select-Object

php - 使用 JavaScript 和 PHP 的动态表单问题

windows - 使用 mingw 编译器错误 : putenv not declared 在 Windows 上进行增强测试

java-COM 互操作 : Implement COM interface in Java

PowerShell - 两个字符之间的小写文本

ruby-on-rails-3 - 如何避免损坏的Arel查询归档RAM

c++ - 如何从 boost::filesystem::filesystem_error 获取错误代码的含义

linux - 如何在 Windows 上 chmod 0777 文件并按原样提交给 Git?

node.js - 如何在 Windows 上运行 Slack botkit?

powershell - 导入csv在PowerShell中获取内存不足异常