0

I wrote some code to return the ip address of the local computer. Basically I created a powershell script which has just 1 line of code in it:

Invoke-RestMethod -Uri ('https://ipinfo.io/')

Then in vba, I use WScript host to run the file, then read the StdOut. From this I look for the line with the ip, do some stripping and return the bare ip. All good and well on the first computer that I tried it on but on the next, which is where it is really needed, the return of StdOut is empty, and if I open powershell and run that same command, I do get the expected info. Can anyone here tell me why this is. The vba code is pretty simple:

Function IPGet()
    Dim Wsh As Object, WExec As Object
    Dim Result$, cmd$, LT$
    Dim Cnt%, i%
    
    cmd = "Powershell -file ""c:\working\access\all county\scripts\IpInfo.ps1"""
    Set Wsh = CreateObject("WScript.Shell")
    Set WExec = Wsh.exec(cmd)
    Result = WExec.StdOut.ReadAll
    Cnt = StringListGetCount(Result, vbCrLf)                'This is just an old function that returns a number telling how many items are listed 
    
    For i = 1 To Cnt
        LT = Trim(StringListGetItem(Result, i, vbCrLf))     'This is just an old function that returns the item at the specified position in a delimited list
        
        If LT Like "ip*:*" Then
            IPGet = Trim(Mid(LT, InStr(1, LT, ":") + 1))
            Exit For
        End If
    Next
End Function
3
  • 1
    Is the powershell script executed at all (and only the output is missing) or is there an issue with the call of the script?
    – FunThomas
    Commented Jan 31 at 12:12
  • 1
    What does WExec.StdErr.ReadAll return?
    – mklement0
    Commented Jan 31 at 13:34
  • The console pops up but result is empty after it closes. I will give stdErr a shot when I get to the computer again
    – JonWayn
    Commented Jan 31 at 18:14

0

Browse other questions tagged or ask your own question.