Answered by:
Blank 'cmd' screen on VBS script execution

Question
-
Hi guys, hopefully a nice easy one. I'm not much of a programmer but just creating a simple desktop script for our users to run to provide the basic info we need when they call for support.
Script is running fine but in the background it is displaying a blank CMD screen. If I try and use objShell.run (command).0,True it will hid it but it also hides the msgbox. I'm sure its something simple im missing but here is the code:
Dim Shell Dim CompName Dim IPAddr Set objShell = CreateObject("Wscript.Shell") objShell.Run ("%comspec% /c set /p IPADDR = ipconfig | findstr IPv4") CompName = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%") IPAddr = objShell.ExpandEnvironmentStrings("%IPAddr%") Msgbox("Your Computer name is " & CompName & VbCrLf & VbCrLf & "Your IP Address is " & IPAddr)
So to recap all I need to do is hide the blank CMD window that comes up.
Any help is greatly appreciated
Thursday, June 16, 2011 1:25 AM
Answers
-
The following works for me when I double click the *.vbs file:
Option Explicit
Dim objNetwork, strComputer, objWMIService
Dim colItems, objItem, strIP, strMessage
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("SELECT IPAddress " _
& "FROM Win32_NetworkAdapterConfiguration " _
& "WHERE IPEnabled='True'")
' Retrieve IP address of local computer.
strMessage = "Computer: " & strComputer & vbCrLf & "IP Address:"
For Each objItem In colItems
For Each strIP In objItem.IPAddress
strMessage = strMessage & vbCrLf & strIP
Next
Next
Call MsgBox(strMessage, vbOKOnly + vbInformation, "Computer Information")Doesn't matter if cscript or wscript host is used.
Richard Mueller - MVP Directory Services- Proposed as answer by jrv Sunday, June 26, 2011 1:55 AM
- Marked as answer by Bill_Stewart Sunday, July 3, 2011 3:35 AM
Sunday, June 26, 2011 12:30 AM
All replies
-
Never mind, I think I sorted it.
I just used 'objShell.exec' instead of 'run'. The CMD window still pops up but only for a brief second so no big drama.
I would be curious to know why though. If I use
objShell.Run ("%comspec% /c set /p IPADDR = ipconfig | findstr IPv4"),0,True
It seems to run, and silently, but never exits the cmd.exe, so the msgbox never shows. If I go into taskmgr and kill the cmd.exe it progresses and the msgbox will show up.
It's all running so like I said no dramas, I just feel like I was doing something wrong there still!
Thursday, June 16, 2011 3:31 AM -
Get all necessary support info for NIC and computer
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True") For Each objItem in colItems WScript.Echo "Caption: " & objItem.Caption for each propValue in objItem.DefaultIPGateway WScript.Echo "DefaultIPGateway: " & propValue next WScript.Echo "DHCPEnabled: " & objItem.DHCPEnabled WScript.Echo "DNSDomain: " & objItem.DNSDomain WScript.Echo "DNSEnabledForWINSResolution: " & objItem.DNSEnabledForWINSResolution WScript.Echo "DNSHostName: " & objItem.DNSHostName for each propValue in objItem.DNSServerSearchOrder WScript.Echo "DNSServerSearchOrder: " & propValue next for each propValue in objItem.IPAddress WScript.Echo "IPAddress: " & propValue next WScript.Echo "IPEnabled: " & objItem.IPEnabled for each propValue in objItem.IPSubnet WScript.Echo "IPSubnet: " & propValue next WScript.Echo "MACAddress: " & objItem.MACAddress WScript.Echo "MTU: " & objItem.MTU WScript.Echo "WINSPrimaryServer: " & objItem.WINSPrimaryServer WScript.Echo "WINSScopeID: " & objItem.WINSScopeID WScript.Echo "WINSSecondaryServer: " & objItem.WINSSecondaryServer Next WScript.Echo "Hit <ENTER> to close window"
x=WScript.StdIn.Read( 1)Place in file called GetNet.vbs
Execute as:
CSCRIPT GetNet.vbs
OR
Be sure default engine is set to CSCRIPT and execute by double clicking on file.
jvThursday, June 16, 2011 6:35 AM -
objShell.Run ("%comspec% /c set /p IPADDR = ipconfig | findstr IPv4"),0,True
try this instead
objShell.Run ("%comspec% /c set /p IPADDR = ipconfig | findstr IPv4"),7,True
Thursday, June 16, 2011 12:06 PM -
Hi,
As others have noted, there are better ways in VBScript to get a computer's IP address than parsing the output of the ipconfig utility. I haven't tested it, but jrv's script posted above looks like it should do the job.
Bill
Thursday, June 16, 2011 3:29 PM -
Thanks Bill but it really isn't my scrip at all. It was generated by teh PrimalScript WMI Wizard and edited down to a few simple items.
A better way to do this would be with an HTA as it would provide a very nice GUI-like interface that can esasily be printed, saved or emailed to teh help desk.
I have used this same technique t oplace teh information permanently (dynamically) on all users desktops so it is always visible.
jvThursday, June 16, 2011 4:37 PM -
The following works for me when I double click the *.vbs file:
Option Explicit
Dim objNetwork, strComputer, objWMIService
Dim colItems, objItem, strIP, strMessage
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("SELECT IPAddress " _
& "FROM Win32_NetworkAdapterConfiguration " _
& "WHERE IPEnabled='True'")
' Retrieve IP address of local computer.
strMessage = "Computer: " & strComputer & vbCrLf & "IP Address:"
For Each objItem In colItems
For Each strIP In objItem.IPAddress
strMessage = strMessage & vbCrLf & strIP
Next
Next
Call MsgBox(strMessage, vbOKOnly + vbInformation, "Computer Information")Doesn't matter if cscript or wscript host is used.
Richard Mueller - MVP Directory Services- Proposed as answer by jrv Sunday, June 26, 2011 1:55 AM
- Marked as answer by Bill_Stewart Sunday, July 3, 2011 3:35 AM
Sunday, June 26, 2011 12:30 AM -
Things have been hectic around here didn't really have a chance to test any of these, but all worked as advertised!
Richards was the simplest and was exactly what I was after thanks!
I had made the mistake of listening to someone on the internet who told me that there were no WMI entries for IP data... my mistake!
Wednesday, July 6, 2011 1:21 AM