Re: How to allow user Input in script?

From: Torgeir Bakken (MVP) (Torgeir.Bakken-spam_at_hydro.com)
Date: 02/29/04


Date: Sun, 29 Feb 2004 01:55:51 +0100

Brian wrote:

> Torgeir, Thanks for the links. One of them had a code
> that seems to fit what I need, but I cant seem to get it
> working. Here is the original code I found and the code I
> have now that works.
>
> What I would like to accomplish is to have the code on
> every desktop in my domain. (This part is not my issue.)
>
> The code would call from a text file on my server (one
> central location to make changes), populate the combo box
> with all names from the text file. When the user selects
> the name and clicks "SEND", thier IP Address is sent to
> the selected person.

Hi

Here is a ready script for you, it populates the list box with the person
names in the text file, and then sends (using net.exe send) the IP address
information to the corresponding user name for the selected person name.

The only thing you need to change is the path in the value strHDeskFile.

(I also added a function GetIPAddresses to obtain the IP address that does
not use Exec to avoid the command prompt as well as the WSH 5.6 requirement)

Option Explicit

Const ForReading = 1
Const OpenAsASCII = 0
Const FailIfNotExist = 0

Dim ie, doc, list, opt, btn
Dim n, selectedValue
Dim arrNICstatus, strIp, objFSO, strHDeskFile, fHDesk, arrHDesk
Dim dicHDesk, arrPerson, strPerson, strTitle, objShell

' get IP address
arrNICstatus = GetIPAddresses
strIp = arrNICstatus(UBound(arrNICstatus))

' get list of help desk persons

' you can use a UNC path here as well
strHDeskFile = "L:\test\techname.txt"

set objFSO = createobject("scripting.filesystemobject")

set fHDesk = objFSO.OpenTextFile(strHDeskFile, _
                  ForReading, FailIfNotExist, OpenAsASCII)

arrHDesk = Split(fHDesk.ReadAll, vbCrLf)
fHDesk.Close

' create a dictionary object with and populate it
Set dicHDesk = createobject("scripting.dictionary")
For n = 0 To UBound(arrHDesk)
  arrPerson = Split(arrHDesk(n), ",")
  dicHDesk.Add Trim(arrPerson(0)), Trim(arrPerson(1))
Next

strTitle = "Send Computer Address to Helpdesk"

' Create an IE application object
set ie = createobject("InternetExplorer.Application")

' Start at a blank page and wait for IE to be ready, grabbing a
' handle to the document when it is
ie.navigate "about:blank"
Do While (ie.busy) : WScript.Sleep 100 : Loop

Set doc = ie.document
doc.title = strTitle

' build the list DHTML
Set list = doc.createElement("SELECT")
list.style.Width = "10em"
doc.body.appendChild(list)
n = 0
For Each strPerson In dicHDesk
  Set opt = doc.createElement("OPTION")
  opt.value = n
  opt.text = strPerson
  list.options.Add(opt)
  n = n + 1
Next

' give us some spacing
doc.body.appendChild doc.createElement("BR")
doc.body.appendChild doc.createElement("BR")

' add a button
Set btn = doc.createElement("BUTTON")
btn.onclick = getref("ok_click") ' bind to our function
btn.style.width = "10em"
btn.innerText = "OK"
doc.body.appendChild btn

' size and move the window
doc.parentWindow.resizeTo 300, 275
doc.parentWindow.moveTo 150, 150

' adjust the IE user interface
doc.body.scroll = "no" ' no scroll bars
ie.document.Close

ie.toolbar = 0 ' no toolbar
ie.statusbar = 0 ' no status bar
ie.resizable = 0 ' not resizable
ie.menubar = False ' no menubar
ie.visible = 1 ' now visible
WScript.Sleep 200
createobject("wscript.shell").appActivate strTitle
' Wait for a selection
selectedvalue = ""
Do until TypeName(doc) = "Object" : wscript.sleep 50 : Loop

If selectedValue <> "" Then
  Set objShell = CreateObject("WScript.Shell")
  objShell.Run "net.exe send " & selectedvalue & " """ _
               & strIp & """", 0, False

  MsgBox "The IP address " & strIp & " is now sent to " _
         & selectedValue, vbSystemModal
Else
  MsgBox "No selection done!", vbSystemModal
End If

' React when the OK button is clicked by grabbing the value and
' shutting down our IE instance
Sub ok_click()
  selectedvalue = dicHDesk(list.options(list.selectedIndex).text)
  ie.quit
End Sub

Function GetIPAddresses()
'=====
' Based on a Michael Harris script, modified be 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 sh, fso, Env, workfile, ts, data, arIPAddress
  dim index, n, parts
  set sh = createobject("wscript.shell")
  set fso = createobject("scripting.filesystemobject")
  set Env = sh.Environment("PROCESS")
  if Env("OS") = "Windows_NT" then
    workfile = Env("TEMP") & "\" & fso.gettempname
    sh.run "%comspec% /c ipconfig >" & Chr(34) & workfile & Chr(34),0,true
  else
    'winipcfg in batch mode sends output to
    'filename winipcfg.out
    workfile = "winipcfg.out"
    sh.run "winipcfg /batch" ,0,true
  end if
  set sh = nothing
  set ts = fso.opentextfile(workfile)
  data = split(ts.readall,vbcrlf)
  ts.close
  set ts = nothing
  fso.deletefile workfile
  set fso = nothing
  arIPAddress = array()
  index = -1
  for n = 0 to ubound(data)
    data(n) = replace(data(n),vbCr,"")
    if instr(data(n),"IP Address") then
      parts = split(data(n),":")
      'if trim(parts(1)) <> "0.0.0.0" then
      if instr(trim(parts(1)), "0.0.0.0") = 0 then
        index = index + 1
        ReDim Preserve arIPAddress(index)
        arIPAddress(index)= trim(cstr(parts(1)))
      end if
    end if
  next
  GetIPAddresses = arIPAddress
End Function

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter

Quantcast