[英]Powershell function “if test-connection” true
嘿伙計們,
我有一個 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
}
現在我想用 function 擴展該腳本,首先檢查服務器是否可用。 如果不是,它應該使用永久測試連接,直到它可用。 當服務器可用時,腳本應該繼續運行。
你可以試試這個:
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 類型的錯誤發生時,腳本才會 go 退出此循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.