Re: setting an environment variable to the current IP

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



OK, final (sortta) script

---------------------
On Error Resume Next

DIM EnvName

EnvName = "CurrentIPs" ' environment variable name

set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!"&_
"root\cimv2").ExecQuery("SELECT IPAddress, DefaultIPGateway FROM "&_
"Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")

Set WshShell = Wscript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment ("System")

If Err <> 0 Then
if err.number = -2147217405 then
MsgBox(" Error 0x80041003: Access Denied: Check permissions and file
security for this ASP file.")
else
MsgBox("Error description:"& Err.description &"error number "&
Err.number)
end if

end if

' First set the variable to null
WshEnv (EnvName) =""

for each IPConfig in IPConfigSet

if Not IsNull(IPConfig.IPAddress) then
for i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
' MsgBox(" IP Address:"&IPConfig.IPAddress(i))
' now append the environment variable with IP info
WshEnv (EnvName) = WshEnv (EnvName) + IPConfig.IPAddress(i) + "/"
next
end if


if Not IsNull(IPConfig.DefaultIPGateway) then
for i=LBound(IPConfig.DefaultIPGateway) to
UBound(IPConfig.DefaultIPGateway)
' MsgBox("Default IP Gateway: "& IPConfig.DefaultIPGateway(i))
' and append gateway info
WshEnv (EnvName) = WshEnv (EnvName) + IPConfig.DefaultIPGateway(i) + " "
next
end if
next



If Err <> 0 Then
MsgBox("error description:"& Err.description &"error number "&
Err.number)
end if
---------------------

Once this has been executed there is an environment variable "CurrentIPs" in
the format:
IP1/gateway IP2/gateway etc...
Eg CurrentIPs=10.0.0.1/10.0.0.138 192.168.250.100/192.168.250.2

tks ur assistance


"SuperGumby [SBS MVP]" <not@xxxxxxxxxxx> wrote in message
news:OT4iabEUGHA.4792@xxxxxxxxxxxxxxxxxxxxxxx
tks Slim,

I have enough vbs to have been able to see what the script does, repair
the wraps, and she runs, giving me dialogue boxes for IP and gateway (an
add that may be handy, tks).

I suppose I need to do some legwork to replace 'MsgBox(" IP
Address:"&IPConfig.IPAddress(i))' with code that will write the info to an
environment variable. Assuming, of course, that vbs can, which in my
ignorance I have no idea about.

don't use Google for the next while, it'll be busy satisfying my
queries :-)

"Slim" <me@xxxxxxxx> wrote in message
news:%23TW%23hTDUGHA.3888@xxxxxxxxxxxxxxxxxxxxxxx
Sorry the first answer was for ASP

forgot what group I was on



On Error Resume Next
set IPConfigSet =
GetObject("winmgmts:{impersonationLevel=impersonate}!"&_
"root\cimv2").ExecQuery("SELECT IPAddress, DefaultIPGateway FROM "&_
"Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE")

If Err <> 0 Then
if err.number = -2147217405 then
MsgBox(" Error 0x80041003: Access Denied: Check permissions and
file security for this ASP file.")
else
MsgBox("Error description:"& Err.description &"error number "&
Err.number)
end if

end if

for each IPConfig in IPConfigSet

if Not IsNull(IPConfig.IPAddress) then
for i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
MsgBox(" IP Address:"&IPConfig.IPAddress(i))
next
end if


if Not IsNull(IPConfig.DefaultIPGateway) then
for i=LBound(IPConfig.DefaultIPGateway) to
UBound(IPConfig.DefaultIPGateway)
MsgBox("Default IP Gateway: "& IPConfig.DefaultIPGateway(i))
next
end if
next

If Err <> 0 Then
MsgBox("error description:"& Err.description &"error number "&
Err.number)
end if








"SuperGumby [SBS MVP]" <not@xxxxxxxxxxx> wrote in message
news:e7%23Knj5TGHA.2444@xxxxxxxxxxxxxxxxxxxxxxx
hopefully something quick and easy for you guys, I'm totally unfamiliar
with scripting.

all I need is to set an environment variable, say,
currentIP=192.168.1.123 when the PC is getting an IP via DHCP

Handy if it works on 2000/XP/2003
Handy if it handles multiple interfaces (eg. currentIP=192.168.0.11,
192.168.32.345)

TIA








.



Relevant Pages

  • Re: setting an environment variable to the current IP
    ... Set WshEnv = WshShell.Environment ... If Err 0 Then ... for each IPConfig in IPConfigSet ... I suppose I need to do some legwork to replace 'MsgBox(" IP ...
    (microsoft.public.scripting.vbscript)
  • Re: The system cannot find the file specified - Win32_NetworkAdapt
    ... I'm pretty sure I've confirmed it was a 'fasle positive' by Kaspersky. ... The script is running stand-alone - although you are correct that it ... For Each IPConfig in IPConfigSet ... Dim m_WshNetwork ...
    (microsoft.public.scripting.vbscript)
  • Re: The system cannot find the file specified - Win32_NetworkAdapt
    ... The script is running stand-alone - although you are correct that it was ... I've confirmed this error is generated the moment variable IPConfigSet is ... because the code you pasted doesn't dim intCtr, ... Dim m_WshNetwork ...
    (microsoft.public.scripting.vbscript)
  • Re: The system cannot find the file specified - Win32_NetworkAdapt
    ... The script is running stand-alone - although you are correct that it was ... I've confirmed this error is generated the moment variable IPConfigSet is ... because the code you pasted doesn't dim intCtr, ... Dim m_WshNetwork ...
    (microsoft.public.scripting.vbscript)
  • Re: Error handling in vbscript.
    ... Here is my standard schpeal on VBS error handling. ... The Err object appears to be somewhat incorrectly documented in the MS VBS ... look back along the call stack to the global script, ...
    (microsoft.public.scripting.vbscript)