0

ユーザーが選択したサーバー IP に対して簡単な PING を実行して、到達可能であることを確認しています。

次のコードは、必要なことを正確に実行しますが、コマンド シェル ウィンドウのクイック フラッシュを回避したい場合を除きます。

その厄介な CMD ウィンドウを最小化するには、何を変更する必要がありますか?

SystemReachable (myIP)

If InStr(myStatus, "Reply") > 0 Then
    ' IP is Confirmed Reachable
Else
    ' IP is Not Reachable
End If

''''''''''''''''''''''
Function SystemReachable(ByVal strIP As String)

Dim oShell, oExec As Variant
Dim strText, strCmd As String

strText = ""
strCmd = "ping -n 1 -w 1000 " & strIP

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec(strCmd)

Do While Not oExec.StdOut.AtEndOfStream
    strText = oExec.StdOut.ReadLine()
    If InStr(strText, "Reply") > 0 Then
        myStatus = strText
        Exit Do
    Else
        myStatus = ""
    End If
Loop

End Function
4

3 に答える 3

2

wscript の run メソッドには、最小化して実行するための引数が既に含まれています。したがって、上記のすべての労力を使用せずに、単に使用します

古いコード

oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True

新しいコード

oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 7, True

wscript で run メソッドを使用する方法については、Microsoft のヘルプを参照してください。

よろしく

Yトラックス

于 2015-01-03T13:49:12.637 に答える
2

非常に実行可能でサイレントなアプローチを見つけました:

Dim strCommand as string
Dim strPing As String

strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 -w 500 " & myIP & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
strPing = fShellRun(strCommand)

If strPing = "" Then
    MsgBox "Not Connected"
Else
    MsgBox "Connected!"
End If

'''''''''''''''''''''''''''

Function fShellRun(sCommandStringToExecute)

' This function will accept a string as a DOS command to execute.
' It will then execute the command in a shell, and capture the output into a file.
' That file is then read in and its contents are returned as the value the function returns.

' "myIP" is a user-selected global variable

Dim oShellObject, oFileSystemObject, sShellRndTmpFile
Dim oShellOutputFileToRead, iErr

Set oShellObject = CreateObject("Wscript.Shell")
Set oFileSystemObject = CreateObject("Scripting.FileSystemObject")

    sShellRndTmpFile = oShellObject.ExpandEnvironmentStrings("%temp%") & oFileSystemObject.GetTempName
    On Error Resume Next
    oShellObject.Run sCommandStringToExecute & " > " & sShellRndTmpFile, 0, True
    iErr = Err.Number

    On Error GoTo 0
    If iErr <> 0 Then
        fShellRun = ""
        Exit Function
    End If

    On Error GoTo err_skip
    fShellRun = oFileSystemObject.OpenTextFile(sShellRndTmpFile, 1).ReadAll
    oFileSystemObject.DeleteFile sShellRndTmpFile, True

Exit Function

err_skip:
    fShellRun = ""
    oFileSystemObject.DeleteFile sShellRndTmpFile, True


End Function
于 2013-10-04T00:02:38.093 に答える