08
2024
08
02:00:25

PowerShell ISE: Test-Connection 主要的问题是参数"-TargetName“是不可检测的。

mprop="name" style="box-sizing: border-box; margin-top: 0px; line-height: 1.2; font-size: 2rem; color: rgb(33, 37, 41); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; text-wrap: wrap; background-color: rgb(255, 255, 255); margin-bottom: 1rem !important;">PowerShell ISE: Test-Connection


我正在尝试通过PowerShell ISE编写一个简单的脚本。这是下面的代码。。。

$var1 = Test-Connection -TargetName www.google.comIF($var1 -eq "true") {
   Write-Host Connection successful
}ELSE {
   Write-Host Connection failed
}

所以主要的问题是参数"-TargetName“是不可检测的。

当我尝试运行这个脚本时,它显示以下错误Test-Connection:错误

我不知道怎么解决这个问题。为什么没有建立参数?

第一行:Test-Connection:没有找到与参数名“TargetName”对应的参数。。




如注释中所述,根据您的Powershell版本,参数-TargetName对您不可用。

您还需要针对字符串检查boolean值。要让它返回boolean值True或False,请提供-Quiet参数。

$var1 = Test-Connection -ComputerName www.google.com -Quiet -Count 1IF($var1 -eq "true") {
   Write-Host Connection successful
}ELSE {
   Write-Host Connection failed
}

ICMP可能关闭了,不允许您“ping”一些Test-Connection依赖的服务器。解决方法是使用Test-NetConnection测试该服务器上的特定端口;例如:Test-NetConnection -Port 80

也可以在trycatch块中使用它:

Try{
    Test-Connection -ComputerName www.google.com -Count 1 -ErrorAction Stop | Out-Null
    "Connection Successful"} Catch [System.Net.NetworkInformation.PingException]{    "Connection Failed"





推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/7477.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: