RE: Editing extended Active Directory user's informations with VBS by ClaudeLachapelle
ClaudeLachapelle
Tue Jul 22 13:17:04 CDT 2008
Final version (working!):
on error resume next
' Get user's ldap info.
set wsharguments = wscript.arguments
set objuser = getobject( wsharguments( 0 ) )
' Employee number.
set objschemaemployeenumber = getobject("LDAP://schema/employeeNumber")
intmaxlen = objschemaemployeenumber.maxrange
if objUser.employeenumber <> "" then
strempnumber = objUser.employeenumber
else
strempnumber = "empty"
end if
' Preferred Language.
if objUser.preferredLanguage <> "" then
strlanguage = objUser.preferredLanguage
else
strlanguage = "empty"
end if
' Division.
if objUser.division <> "" then
strdivision = objUser.division
else
strdivision = "empty"
end if
' Create Internet Explorer pop up box.
set oie = createobject("InternetExplorer.Application")
with oie
' Configure the IE window
.RegisterAsDropTarget = False
.statusbar = false : .toolbar = false
.menubar = false : .addressbar = false
.Resizable = False
.Navigate "about:blank"
do until .readystate = 4 : wscript.sleep 50 : loop
' Test for IE 7 - cannot remove 'chrome' in that version
sVersion = .document.parentWindow.navigator.appVersion
if instr(sVersion, "MSIE 7.0") = 0 Then .FullScreen = True
.width = 400 : .height = 270
' Create the password box document
with .document
oie.left = .parentWindow.screen.width \ 2 - 200
oie.top = .parentWindow.screen.height\ 2 - 100
.open
.write "<html><head><" & "script>bboxwait=true;</" _
& "script><title>" & objuser.displayname & "'s Extended
informations</title></head>"_
& "<body bgColor=silver scroll=no " _
& "language=vbs style='border-" _
& "style:outset;border-Width:3px'" _
& " onHelp='window.event.returnvalue=false" _
& ":window.event.cancelbubble=true'" _
& " oncontextmenu=" _
& "'window.event.returnvalue=false" _
& ":window.event.cancelbubble=true'" _
& " onkeydown='if ((window.event.keycode>111)"_
& " and (window.event.keycode<117)) or" _
& " window.event.ctrlkey then" _
& " window.event.keycode=0" _
& ":window.event.cancelbubble=true" _
& ":window.event.returnvalue=false'" _
& " onkeypress='if window.event.keycode=13" _
& " then bboxwait=false'><center>" _
& "<div style='padding:10px;background-color:lightblue'>" _
& "<b> " & objuser.displayname & "'s Extended
Informations<b> </div><p>" _
& "<table bgcolor=cornsilk cellspacing=10><tr><td>" _
& " <b>Employee Number:</b></td><td>" _
& "<input type=text size=10 id=empnumber value=" & strempnumber &
">" _
& "</td><tr><td> <b>Preferred Language:</b></td><td>" _
& "<input type=text size=10 id=language value=" & strlanguage &
">" _
& "</td><tr><td> <b>Division:</b></td><td>" _
& "<input type=text size=10 id=division value=" & strdivision &
">" _
& "</td></tr></table><br>" _
& "<button onclick='bboxwait=false;'>" _
& " Okay " _
& "</button> <button onclick=" _
& "'document.all.empnumber.value=""CANCELLED"";" _
& "document.all.language.value="""";" _
& "document.all.division.value="""";" _
& "bboxwait=false;'>Cancel" _
& "</button></center></body></html>"
.close
do Until .readyState = "complete" : wscript.sleep 100 : loop
.all.empnumber.focus
.all.empnumber.select
oie.Visible = True
createobject("wscript.shell").appactivate "Users Extended informations"
stremployeid = "CANCELLED"
do While .parentWindow.bBoxWait
if err then wscript.quit
wscript.sleep 100
loop
oie.Visible = False
strempnumber = .all.empnumber.value
strlanguage = .all.language.value
strdivision = .all.division.value
end with ' document
end with ' IE
if strEmpNumber = "CANCELLED" then
wscript.quit
else
luserchange = false
' Apply changes only if required.
if Len(strEmpNumber) > intMaxLen then
MsgBox "The new Employee Number was too long and it was not saved.",
vbCritical, "Error Occurred"
else
Err.Clear
if strempnumber <> "empty" then
objUser.employeeNumber = strEmpNumber
luserchange = true
end if
end if
if strlanguage <> "empty" then
objUser.preferredLanguage = strlanguage
luserchange = true
end if
if strdivision <> "empty" then
objUser.division = strdivision
luserchange = true
end if
if luserchange then
objUser.SetInfo
If Err Then MsgBox "The new Employee Number was not saved.",
vbCritical, "Error Occurred"
end if
end If
Use it at your own risk! ;-)
"Claude Lachapelle" wrote:
> Now I have my three fields, with "Ok" and "Cancel" push button, but I'm
> unable to get those button to works with my code:
>
> ' Create an IE object
> set objie = createobject( "InternetExplorer.Application" )
>
> ' specify some of the IE window's settings
> objie.navigate "about:blank"
> objie.document.title = "User's extended informations"
> objie.toolBar = False
> objie.resizable = False
> objie.statusbar = False
> objie.width = 500
> objie.height = 240
>
> ' Center the dialog window on the screen
> with objie.document.parentwindow.screen
> objie.left = (.AvailWidth - objIE.Width ) \ 2
> objie.top = (.Availheight - objIE.Height) \ 2
> end with
>
> ' Wait till IE is ready
> do while objie.busy
> wScript.sleep 200
> loop
>
> ' Insert the HTML code to prompt for user input
> objie.document.body.innerHTML = "Employee number (ex: 123456): " _
> & "<INPUT TYPE=""text"" SIZE=""20"" " _
> & "ID=""EmpNumber"">" & vbCrLf _
> & "<p>Preferred Language (F=French,
> E=English): " _
> & "<INPUT TYPE=""text"" SIZE=""20"" " _
> & "ID=""PrefLang""></p>" & vbCrLf _
> & "<p>Division (ex: 123456): " _
> & "<INPUT TYPE=""text"" SIZE=""20"" " _
> & "ID=""Division""></p>" & vbCrLf _
> & "<center><button onclick='bboxwait=false;'>
> Ok </button>" _
> & "<button onclick='bboxwait=cancel;'> Cancel
> </button>" _
> & "</center>"
>
> ' Make the window visible
> objie.visible = true
>
> ' Watch user action
> with objie.document
>
> do while objie.document.bboxwait <----- Error! Property or method not
> supported!
>
> if err then quit
>
> wscript.sleep 100
>
> if objie.document.bboxwait = "cancel" then <----- Error!
> objie.visible = false
> msgbox( objie.document.bboxwait ) <----- Error!
> exit do
> end if
> loop
>
> objie.visible = false
>
> ' Read the user input from the dialog window
> strEmpNumber = objIE.Document.All.EmpNumber.Value
> strPrefLang = objIE.Document.All.PrefLang.Value
> end with
>
> ' Close and release the object
> objie.quit
>
> set objie = nothing
>
> I'm unable to access the bboxwait variable which is set when user is
> clicking on "Ok" or "Cancel", how we can accomplish that???
>
> Thanks.
>
> "Claude Lachapelle" wrote:
>
> > I finally got it to work, but when using a different method:
> >
> > strUserInput = GetUserInput()
> >
> > Function GetUserInput()
> >
> > Dim objIE
> > ' Create an IE object
> > Set objIE = CreateObject( "InternetExplorer.Application" )
> > ' specify some of the IE window's settings
> > objIE.Navigate "about:blank"
> > objIE.Document.Title = "User's extended informations"
> > objIE.ToolBar = False
> > objIE.Resizable = False
> > objIE.StatusBar = False
> > objIE.Width = 320
> > objIE.Height = 180
> > ' Center the dialog window on the screen
> > With objIE.Document.ParentWindow.Screen
> > objIE.Left = (.AvailWidth - objIE.Width ) \ 2
> > objIE.Top = (.Availheight - objIE.Height) \ 2
> > End With
> > ' Wait till IE is ready
> > Do While objIE.Busy
> > WScript.Sleep 200
> > Loop
> >
> > ' Insert the HTML code to prompt for user input
> > objIE.Document.Body.InnerHTML = "<DIV align=""center""><P>Employee
> > number:</P>" & vbCrLf _
> > & "<P><INPUT TYPE=""text"" SIZE=""20"" " _
> > & "ID=""UserInput""></P>" & vbCrLf _
> > & "<P><INPUT TYPE=""hidden"" ID=""OK"" " _
> > & "NAME=""OK"" VALUE=""0"">" _
> > & "<INPUT TYPE=""submit"" VALUE="" OK "" " _
> > &
> > "OnClick=""VBScript:OK.Value=1""></P></DIV>"
> >
> > ' Make the window visible
> > objIE.Visible = True
> > ' Wait till the OK button has been clicked
> > Do While objIE.Document.All.OK.Value = 0
> > WScript.Sleep 200
> > Loop
> >
> > ' Read the user input from the dialog window
> > GetUserInput = objIE.Document.All.UserInput.Value
> > ' Close and release the object
> > objIE.Quit
> > Set objIE = Nothing
> > End Function
> >
> > But now, I need to insert 2 more fields, how I should proceed?
> >
> > "Claude Lachapelle" wrote:
> >
> > > Since with VBScript we are limited to input one field at a time using the
> > > inputbox function, I'm trying to write down something that will open an IE
> > > form where I will get multiple extended user's information:
> > >
> > > - employeeNumber
> > > - preferredLanguage
> > > - division
> > >
> > > I found some samples, but I'm always getting a lot of errors trying to make
> > > them works, actually my VBScript looks like this (initially get from a MVP):
> > >
> > > Function ExtendedInfo()
> > >
> > > set oSH = CreateObject("Wscript.Shell")
> > > set oIE = CreateObject("InternetExplorer.Application")
> > >
> > > With oIE
> > >
> > > .RegisterAsDropTarget = False
> > > .FullScreen = True
> > > .width = 400 : .height = 200
> > > .Navigate "about:blank"
> > >
> > > Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
> > >
> > > .document.open
> > > .document.write _
> > > "<html><head><" & "script>bboxwait=true;</" _
> > > & "script><title>User's Extended Informations</title></head>"_
> > > & "<body bgColor=Silver scroll=no language=vbs" _
> > > & " onkeypress='if window.event.keycode=13 Then" _
> > > & " bboxwait=false'><center>" _
> > > & "<b> Enter user's extended informations<b> <p>" _
> > > & "<table><tr><td> <b>Employee #:</b></td><td>" _
> > > & "<input type=text id=emp value='" & strEmpNumber & "'>" _
> > > & "<button onclick='bboxwait=false;'> Ok </button>" _
> > > & "<button onclick='bboxwait=cancel;'> Cancel </button>" _
> > > & "</center></body></html>"
> > > .document.close
> > >
> > > Do Until .ReadyState = 4 : WScript.Sleep 100 : Loop
> > >
> > > With .document
> > > oIE.left = .parentWindow.screen.width \ 2 - 200
> > > oIE.top = .parentWindow.screen.height\ 2 - 100
> > > .body.style.borderStyle = "outset"
> > > .body.style.borderWidth = "3px"
> > > .all.user.focus
> > > .all.user.select
> > >
> > > ExtendedInfo = Array("CANCELLED")
> > >
> > > Do While .parentWindow.bBoxWait
> > >
> > > oSH.Appactivate "User's Extended Informations"
> > > oIE.Visible = True
> > >
> > > if Err Then Exit Function
> > >
> > > WScript.Sleep 100
> > >
> > > If .parentWindow.bBoxWait = "cancel" Then
> > >
> > > oIE.Visible = False
> > > MsgBox(.parentWindow.bBoxWait)
> > > Exit Do
> > > End If
> > > Loop
> > > oIE.Visible = False
> > > ExtendedInfo = Split(.all.user.value & "|" & .all.pass.value, "|")
> > > End With ' document
> > > End With ' IE
> > > end function
> > >
> > > I just modified the function name and where the form is written (for
> > > Employee #), and I'm always getting the following error message:
> > >
> > > Object doesn't support this property or method: 'all.user'
> > >
> > > and the referred line is .all.user.focus
> > >
> > > What's wrong with my VBScript???
> > >
> > > Thanks.
> > >
> > > Claude Lachapelle
> > > Systems Administrator, MCSE
> > >