I have this code that I am running:
Public Function checkhost(hostname As String)
    'bla bal...
    strexec = "powershell -WindowStyle Hidden -Command ping " & hostname & " -n 1"
    Set oExec = WshShell.exec(strexec)
    'bla bla...
End Function
From my Excel document, the shell does not close - it remains open. The same code does work, if I use it in Powershell.
With cmd it works fine but I want to hide the cmd window.
strexec = "%comspec% /C %WINDIR%\system32\ping.exe " & hostname & " -n 1"
Set oExec = WshShell.exec(strexec)
AFAIK the only way to hide CMD is to use WshShell.Run but I don't know how to change my existing code. Can anyone suggest a solution?
Complete code
    Public Function checkhost(hostname As String)
    Dim WshShell As Variant
    Set WshShell = CreateObject("Wscript.Shell")
    Dim ReturnErrorLevel As Variant
    strexec = "powershell -WindowStyle Hidden -Command ping " & hostname & " -n 1"
    Set oExec = WshShell.exec(strexec)
    Do Until oExec.Status = 1
        Application.Wait (Now + TimeValue("00:00:01"))
    Loop
    Result = Split(oExec.StdOut.readAll, vbCrLf, , vbTextCompare)
    For Each Item In Result
        Debug.Print Item
        If InStr(1, Item, "Ping request could not find host", vbTextCompare) > 0 Then
            checkhoststatus = "0.0.0.0"
            Exit For
        End If
        If InStr(1, Item, "Request timed out.", vbTextCompare) > 0 Then
            checkhoststatus = "0.0.0.0"
           Exit For
        End If
        If InStr(1, Item, "Reply from ", vbTextCompare) > 0 Then
            checkhoststatus = Mid(Item, 12, InStr(1, Item, ":", vbTextCompare) - 12)
            Exit For
        End If
    Next Item
End Function
 
    