Powershell函数“if test-connection”true

rjzwgtxy  于 2023-05-18  发布在  Shell
关注(0)|答案(2)|浏览(164)

嘿伙计们
我有一个powershell脚本,这是工作正常,但我想扩大它与另一行。这是我有的。

$Path = "\\xyz\trigger1.txt"
$Path2 = "$env:userprofile\xyz\trigger2.txt"

if ((Test-Path $Path -PathType Leaf) -and (-not(Test-Path $Path2 -PathType Leaf))){ 


if((get-process "XXX" -ea SilentlyContinue) -eq $Null)
{ 
    "not Running"
    copy-item "\\xyz\xyz\*" "$env:userprofile\def\" -recurse -ErrorAction SilentlyContinue
    start-sleep -s 5
     
}
else
{
    "running"
    stop-process -name "XXX" -force
    start-sleep -s 5
    copy-item "\\xyz\xyz\*" "$env:userprofile\def\" -recurse -ErrorAction SilentlyContinue
    start-sleep -s 5
     
}
}
else
{

start-sleep -s 5
}

现在我想用一个函数来扩展这个脚本,首先检查服务器是否可用。如果不是,则应使用永久测试连接,直到其可用为止。当服务器可用时,脚本应该继续运行。

8e2ybdfx

8e2ybdfx1#

你可以试试这个:

Do
{
    Test-Connection -ComputerName $computerName -ErrorAction SilentlyContinue -ErrorVariable pingError | Out-Null
    $pingError = $pingError | Where-Object { $_.CategoryInfo.Category -Eq 'ResourceUnavailable' }
    Start-Sleep -Seconds 5
}
While($pingError -Ne $Null)

只有当不发生ResourceUnavailable类型的错误时,脚本才会退出此循环。

5jvtdoz2

5jvtdoz22#

使用Test-Connection和-Quiet选项将返回true或false:

Test-Connection $MachineName -Count 1 -Quiet

相关问题