?
Solved

VB Script to map IP to user and PC

Posted on 2007-08-06
6
Medium Priority
?
310 Views
Last Modified: 2008-01-09
Is it possible to write a vb script that you can put an IP address into and it will return the user logged in and the PC that is being used?
0
Comment
Question by:urobins
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 19641356
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_LoggedOnUser",,48)
For Each objItem in colItems
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_LoggedOnUser instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Antecedent: " & objItem.Antecedent
Next


strcomputer should be changed to machine name or IP Address.

0
 

Author Comment

by:urobins
ID: 19641828
Is it possible to have this pop up a text box to input the ip address?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 19642684
Hi, try this, I have been using it for a couple of years:
'=============
Option Explicit
'On Error Resume Next

Dim objShell
Dim objExec
Dim strPingResults
Dim strComputer
Dim objWMIService
Dim colComputer
Dim colComputerIP
Dim colSystemInfo
Dim objComputer
Dim strUserName
Dim strHostName
Dim IPConfig
Dim intIPCount
Dim strIPAddress
Dim objItem
Dim strOS_Caption
Dim strOS_SPVersion
Dim strOS_VerNumber



 
strComputer = InputBox("Please enter an IP Address or computer name:", _
    "Get User Login Details","172.16.2.64")

if IsEmpty(strComputer) = True Then wscript.Quit


Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)

strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
  'WScript.Echo strComputer & " responded to ping."
Else
  msgbox strComputer & " did not respond to ping. No action could be taken.",vbOkOnly, "Cannot continue script."
  Wscript.Quit(0)
End If



Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")

Set colComputerIP = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")

Set colSystemInfo = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem",,48)
 
For Each objComputer in colComputer
    strUserName = "User Name: " & objComputer.UserName
    strHostName = "Host Name: " & objComputer.Name
Next

For Each IPConfig in colComputerIP
      If Not IsNull(IPConfig.IPAddress) Then
            'strIPAddress = strIPAddress & IPConfig.Description & ": "
        For intIPCount = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
                  'strIPAddress = strIPAddress & "IP Address: " & IPConfig.IPAddress(intIPCount) & "~"
                  strIPAddress = strIPAddress & IPConfig.Description & ": " & IPConfig.IPAddress(intIPCount) & "~"
            Next
      End If
Next

If Right(strIPAddress, 1) = "~" Then
      strIPAddress = Left(strIPAddress, Len(strIPAddress) - 1)
End If
strIPAddress = Replace(strIPAddress, "~", vbCrLf)

For Each objItem in colSystemInfo
     strOS_Caption = "Caption: " & objItem.Caption
     strOS_SPVersion = "SP Version: " & objItem.CSDVersion
     strOS_VerNumber = "Version Number: " & objItem.Version
Next

MsgBox strUserName & vbcrlf & strHostName & vbcrlf & _
     strIPAddress & vbcrlf & strOS_Caption & vbcrlf & _
     strOS_SPVersion & vbcrlf & strOS_VerNumber, vbOKOnly, "Login Details"
'============

Regards,

Rob.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 19642694
And here is a HTA version of the same script.  Paste this into a text file, and change the extension to HTA (so it becomes "check_login.hta").

'===========
<head>
<title>Login Details of a Machine</title>
<HTA:APPLICATION
     APPLICATIONNAME="Login Details"
     BORDER="thin"
     SCROLL="no"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
>
</head>

<script language="VBScript">

Sub Window_onLoad
      Me.ResizeTo 400,300
    Me.MoveTo ((Screen.Width / 2) - 200),((Screen.Height / 2) - 150)
End Sub

Sub Default_Buttons
      If Window.Event.KeyCode = 13 Then
            btn_logindetails.Click
      End If
End Sub

Sub Get_Login_Details
   
      strComputer = txt_computer.value
      
      if strComputer = "" Then
            msgbox "Please enter a computer name or IP Address",vbOKOnly,"No Computer Name or IP Address"
            Exit Sub
      End If
      
      If Ping(strComputer) = False Then
        msgbox strComputer & " did not respond to ping. No action could be taken.",vbOkOnly, "Cannot continue script."
        Exit Sub
      End If      
      
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      
      Set colComputer = objWMIService.ExecQuery _
          ("Select * from Win32_ComputerSystem")
      
      Set colComputerIP = objWMIService.ExecQuery _
          ("Select * from Win32_NetworkAdapterConfiguration")
      
      Set colSystemInfo = objWMIService.ExecQuery _
          ("Select * from Win32_OperatingSystem",,48)
      
      For Each objComputer in colComputer
          strUserName = "User Name: " & objComputer.UserName
          strHostName = "Host Name: " & objComputer.Name
      Next
      
      For Each IPConfig in colComputerIP
            If Not IsNull(IPConfig.IPAddress) Then
              For intIPCount = LBound(IPConfig.IPAddress) _
                  to UBound(IPConfig.IPAddress)
                      strIPAddress = strIPAddress & "IP Address: " & IPConfig.IPAddress(intIPCount) & "~"
            next
            end if
      Next
      
      If Right(strIPAddress, 1) = "~" Then
            strIPAddress = Left(strIPAddress, Len(strIPAddress) - 1)
      End If
      strIPAddress = Replace(strIPAddress, "~", vbCrLf)
      
      For Each objItem in colSystemInfo
           strOS_Caption = "Caption: " & objItem.Caption
           strOS_SPVersion = "SP Version: " & objItem.CSDVersion
           strOS_VerNumber = "Version Number: " & objItem.Version
      Next
      
      MsgBox strUserName & vbcrlf & strHostName & vbcrlf & _
           strIPAddress & vbcrlf & strOS_Caption & vbcrlf & _
           strOS_SPVersion & vbcrlf & strOS_VerNumber, vbOKOnly, "Login Details"
   
End Sub

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function

</script>

<body STYLE="font:14 pt arial; color:white;filter:progid:DXImageTransform.Microsoft.Gradient
(GradientType=1, StartColorStr='#000033', EndColorStr='#0000FF')" onkeypress='vbs:Default_Buttons'>
      <table width='80%' height = '100%' align='center' border='0'>
            <tr height='20%'>
                  <td>
                  </td>
            </tr>
            <tr height='10%'>
                  <td align='center'>
                        Computer name or IP Address:
                  </td>
            </tr
            <tr height='20%'>
                  <td align='center'>
                        <input type="text" value="172.16.2.48" name="txt_computer" maxlength='15' size='16'>
                  </td>
            </tr>
            <tr height='30%'>
                  <td align='center'>
                        <input type="button" value="Check Machine" name="btn_logindetails"  onClick="Get_Login_Details">
                  </td>
            </tr>
            <tr height='20%'>
                  <td>
                  </td>
            </tr>

      </table>

</body>
'===========

Regards,

Rob.
0
 

Author Comment

by:urobins
ID: 19643006
Thanks, I'll give these a shot!
0
 

Author Comment

by:urobins
ID: 19644458
Thanks RobSampson, that is exactly what I was lookin for, I was having problems getting it to iterate through correctly
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question