Skip to content

Instantly share code, notes, and snippets.

@jtrent238
Created April 30, 2018 17:37
Show Gist options
  • Save jtrent238/2c72b3d826adab6c2cb83244a157e483 to your computer and use it in GitHub Desktop.
Save jtrent238/2c72b3d826adab6c2cb83244a157e483 to your computer and use it in GitHub Desktop.
On Error Resume Next
Dim WSH, FSO, RunIPConfig, TempDir, CMD, OpenFile, AllText, IntStr1, IntCounter
Dim FileExist, IPText, IntStr2, IPStart, IPEnd, IPDiff, IPAddress, StartPos, IntStr3, IPAddress1
Dim strComputerName, FinalIP
Set WSH = WSCript.CreateObject("WScript.Shell")
Set WshNtwk = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
TempDir = WSH.ExpandEnvironmentStrings("%TEMP%")
CMD = WSH.ExpandEnvironmentStrings("%Comspec% /C")
StartPos = 1
' Silently run ipconfig; output to temporary file
RunIPConfig = WSH.run(CMD & " Ipconfig > %TEMP%\000001.tmp", 0, True)
WSCript.Sleep 200
FileExist = FSO.FileExists(TempDir & "\000001.tmp")
' Read through ipconfig output in temp file; strip the IP address from the text
StartPos = 1
For IntCounter = 1 to 6
If FileExist = True Then
Set OpenFile = FSO.OpenTextFile(TempDir & "\000001.tmp", 1, False, 0)
OpenFile.Skip(StartPos)
Do While NOT OpenFile.AtEndOfStream
AllText = OpenFile.ReadAll
Loop
IntStr1 = Instr(StartPos, AllText, "IPv4 Address", 1)
IntStr2 = InStr(IntStr1, AllText, ": ", 1)
IPStart = IntStr2 + 2
IPEnd = IPStart + 15
IPDiff = IPEnd - IPStart
IPAddress = Mid(AllText, IPStart, IPDiff)
IntStr3 = InStr(1, IPAddress, "0.0.0.0", 1)
If IntStr3 = "1" Then
StartPos = IPEnd
End If
If NOT IntStr3 = "1" Then
IntCounter = 6
End If
End If
Next
' Remove spacings and carriage returns
IPAddress1 = trim(IPAddress)
FinalIP = Replace(IPAddress1, vbCr, "")
' Display the IP address and computer name in user-friendly message box
MsgBox "Computer Name:" & vbTab & Ucase(WshNtwk.ComputerName) & vbCrLf & "IP Address:" & vbTab & FinalIP, vbOkOnly , "Computer Details"
On Error Goto 0
WScript.Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment