ThrowingFitz
ThrowingFitz

Reputation: 17

Create VBS that creates a message box with the current user's - username, computer name, and multiple IP addresses for (wired/wireless)

I've been looking around for a bit now, and I have come up with this mix of script...it does pretty much what I need but it does not look pretty on the message box when it comes to the IP address...currently they are displayed together with no space example (wiredIPwirelessIP). Ideally what I would like is to have the message box say Username: user Computer Name: Computer1 Wired IP: X.X.X.X Wireless IP: X.X.X.X

Any help would be appreciated.

Here is what I have so far

On Error Resume Next
Dim WSH
Dim strComputerName, FinalIP
Set WSH = WSCript.CreateObject("WScript.Shell") 
Set WshNtwk = WScript.CreateObject("WScript.Network") 
strMsg = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPAdapterSet = objWMIService.ExecQuery("Select AdapterType, NetConnectionID, MACAddress from Win32_NetworkAdapter")
For Each IPConfig in IPAdapterSet
Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "'")
For Each IPConf in IPConfigSet
''strMsg = strMsg
If Not IsNull(IPConf.IPAddress) Then
For i = LBound(IPConf.IPAddress) to UBound(IPConf.IPAddress)
If Not ((Instr(IPConf.IPAddress(i), ":") > 0) or (Instr(IPConf.IPAddress(i), "none") > 0)) Then
if i=0 then
strMsg = strMsg & IPConf.IPAddress(i)
else
strMsg = strMsg & ", " & IPConf.IPAddress(i) 
end if
End If
Next
End If
next
Next
''WScript.Echo strMsg
Dim infroStr
infoStr = "Please provide the following information to the IT Technician." & vbCRLF & vbCRLF & _
"               Username :" & vbTab & WshNtwk.UserName & vbCRLF & _
"   Computer Name :" & vbTab & Ucase(WshNtwk.ComputerName) & vbCRLF & _
"        IP Address(es) :" & vbTab & strMsg
'"Domain:" & WshNtwk.UserDomain
' Display the IP address and computer name in user-friendly message box 
MsgBox infoStr, 64, "Some Title"
'"Computer Name:" & vbTab & Ucase(WshNtwk.ComputerName) & vbCrLf & "IP Address:" & vbTab & FinalIP, vbOkOnly , "SCCCMHA PC Information"
On Error Goto 0
WScript.Quit

Upvotes: 0

Views: 1634

Answers (1)

Theo
Theo

Reputation: 61188

I think this will help you:

Option Explicit

On Error Resume Next
Dim WSH, WshNtwk, objWMIService, IPAdapterSet, IPConfig, IPConfigSet, IPConf
Dim strMsg, strComputer, infoStr, FinalIP

Set WSH = WSCript.CreateObject("WScript.Shell")
Set WshNtwk = WScript.CreateObject("WScript.Network")
strMsg = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPAdapterSet = objWMIService.ExecQuery("Select AdapterTypeID, NetConnectionID, MACAddress from Win32_NetworkAdapter")

' values for AdapterTypeID: https://learn.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkadapter
For Each IPConfig in IPAdapterSet
    Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "'")
    For Each IPConf in IPConfigSet
        If Not IsNull(IPConf.IPAddress) Then
            If Instr(1, IPConfig.NetConnectionID, "Wireless", vbTextCompare) > 0 Then
                strMsg = strMsg & "- Wireless IP: " & vbTab
            Else
                strMsg = strMsg & "- Wired IP:    " & vbTab
            End If

            strMsg = strMsg & Join(IPConf.IPAddress, ", ") & vbCrLf
            strMsg = strMsg & "- MAC Address: " & vbTab & IPConfig.MACAddress & vbCrLf & vbCrLf
        End If
    Next
Next

''WScript.Echo strMsg
infoStr = "Please provide the following information to the IT Technician." & vbCrLf & vbCrLf & _
"Username :" & vbTab & WshNtwk.UserName & vbCrLf & _
"Computer Name :" & vbTab & Ucase(WshNtwk.ComputerName) & vbCrLf & _
"IP Addresses:" & vbCrLf & strMsg
'"Domain:" & WshNtwk.UserDomain
' Display the IP address and computer name in user-friendly message box
MsgBox infoStr, 64, "Some Title"
'"Computer Name:" & vbTab & Ucase(WshNtwk.ComputerName) & vbCrLf & "IP Address:" & vbTab & FinalIP, vbOkOnly , "SCCCMHA PC Information"
On Error Goto 0

'cleanup
Set IPConf = Nothing
Set IPConfig = Nothing
Set objWMIService = Nothing
Set IPAdapterSet = Nothing
Set WSH = Nothing
Set WshNtwk = Nothing

WScript.Quit

P.s. The messagebox may be informative, but does not allow users to copy the info as text and send it to IT in a mail for instance. Maybe you should add a line telling them to press Alt + PrtScn and paste that in an email with Ctrl + V

Upvotes: 1

Related Questions