"Green-Thumb" <GreenThumb@discussions.microsoft.com> wrote in message
news:7C22ACD4-56C1-4C17-8A53-C4FDD69F1B86@microsoft.com...
>I have been trying to write a script for retrieving the IP from a client.
> I want to be able to send an email with the script as an attachement and
> my
> client then would receive the email and run the script. The script would
> send me an email back containing the clients IP.
>
> Can someone me please?
Someone else can give ideas on emailing, as that can get very complex. It
depends on what you have, such as Outlook, or other software.
Retrieving the IP address can quickly get complicated (especially on Vista
clients). Do you want IPv4 or IPv6? What if the computer has more than one
address? I've used several VBScript functions in the past, but they all give
different results on my Vista computer. The one I've used longest and is
fine for most clients (even Win9x):
==============
Option Explicit
Dim strIP
strIP = Join(GetIPAddresses())
Wscript.Echo strIP
Function GetIPAddresses()
' Based on a Michael Harris script, modified by Torgeir Bakken
'
' Returns array of IP Addresses as output
' by IPConfig or WinIPCfg...
'
' Win98/WinNT have IPConfig (Win95 doesn't)
' Win98/Win95 have WinIPCfg (WinNt doesn't)
'
' Note: The PPP Adapter (Dial Up Adapter) is
' excluded if not connected (IP address will be 0.0.0.0)
' and included if it is connected.
Dim objShell, objFSO, objEnv, strWorkFile, objFile
Dim arrData, intIndex, n, arrIPAddresses, arrParts
Set objShell = CreateObject("wscript.shell")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objEnv = objShell.Environment("PROCESS")
If (objEnv("OS") = "Windows_NT") Then
strWorkFile = objEnv("TEMP") & "\" & objFSO.GetTempName
objShell.Run "%comspec% /c IPConfig >" & Chr(34) _
& strWorkFile & Chr(34), 0, True
Else
' WinIPCfg in batch mode sends output to
' filename WinIPCfg.out
strWorkFile = "WinIPCfg.out"
objShell.Run "WinIPCfg /batch", 0, True
End If
Set objShell = Nothing
Set objFile = objFSO.OpenTextFile(strWorkFile)
arrData = Split(objFile. ReadAll, vbCrLf)
objFile.Close
Set objFile = Nothing
objFSO.DeleteFile strWorkFile
Set objFSO = Nothing
arrIPAddresses = Array()
intIndex = -1
For n = 0 To UBound(arrData)
If (InStr(arrData(n), "IPv4 Address") > 0) Then
arrParts = Split(arrData(n), ":")
If (InStr(Trim(arrParts(1)), "0.0.0.0") = 0) Then
intIndex = intIndex + 1
ReDim Preserve arrIPAddresses(intIndex)
arrIPAddresses(intIndex)= Trim(CStr(arrParts(1)))
End If
End If
Next
GetIPAddresses = arrIPAddresses
End Function
==============
If the client has more than one address, they are shown space delimited.
Another method that requires WMI (will run on most clients with W2k or
above), but outputs IPv6 on Vista:
================
Option Explicit
Dim objNetwork, strComputer
Dim objWMIService, colComputers, objComputer
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery _
("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")
For Each objComputer In colComputers
Wscript.Echo objComputer.ProtocolAddress
Next
==============
And another requiring WMI that gives different results:
============
Option Explicit
Dim objNetwork, strComputer
Dim objWMIService, colItems, objItem
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE
IPEnabled='True'")
For Each objItem In colItems
Wscript.Echo Join(objItem.IPAddress, ",")
Next
===========
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab -
http://www.rlmueller.net
--