แสดงบทความที่มีป้ายกำกับ VBScript แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ VBScript แสดงบทความทั้งหมด

วันศุกร์ที่ ๑๖ ตุลาคม พ.ศ. ๒๕๕๒

VBScript อ่านค่า User Account เพื่อเปิด Website อัตโนมัติ

ผมได้รับคำสั่งให้ทำอย่างไรก็ได้ เมื่อ user ทำการ log in เข้า Windows แล้วให้ตรวจสอบว่าเป็น user ที่อยู่ในกลุ่มพิเศษหรือไม่ ถ้าใช่ให้เปิด Web Application ขี้นมา เพื่อแสดงสถานะงานคงค้าง (เรื่องสำคัญมากคือการทวงหนี้ลูกค้า)

ไปนั่งคิดนอนคิดก็เลยคิดว่าน่าจะเขียน Log on script ด้วย VBScript มาทำงานนี้ครับ เริ่มแรกผมเก็บข้อมูลรายชื่อ user ไว้ใน database ก่อน แล้วพอ Log on script ทำงานจะไปตรวจสอบว่า User นั้นตรงกับที่อยู่ใน database หรือไม่ ทีนี้เราต้องใช้ Windows Scripting Host (WScript) มาร่วมด้วยครับ


ON ERROR RESUME NEXT

'*******************************************************
'// GET LOGON USER ACCOUNT
'*******************************************************
Dim WSHNetwork
Set WSHNetwork = CreateObject("Wscript.Network")

Dim userAccount
userAccount = WSHNetwork.UserDomain & "\" & WSHNetwork.UserName
Set WSHNetwork = nothing

'*******************************************************
'// VALIDATE USER
'*******************************************************

Set cnn = CreateObject("adodb.connection")
Set rstUser = CreateObject("adodb.recordset")

cnn.connectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=xxxx;Password=yyyy;Initial Catalog=UserList;Data Source=xxSQLServer"

cnn.open

Dim strSQL
strSQL = "select employeeid from dbo.tblUser a where aduser = '" & userAccount "'"
rstUser.open strsql, cnn

If not rstUser.eof Then
'// If valid user, execute IE
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "iexplore.exe -new http://myHost/myApplication"
Set WSHShell = nothing
End If


rstUser.close
cnn.close

Set rstUser = Nothing
Set cnn = Nothing



ทดสอบการทำงานก็ได้ผลโอเคครับ แต่โดน Network Admin ติงมานิดหน่อยว่าไม่อยากให้ไปติดต่อ database เพราะมันช้า และต้องใช้ traffic ของ network เนื่องจาก SQLServer กับ AD อยู่คนละ Server กัน ดังนั้นเลยเปลี่ยนเอา list ของ user มาเก็บเป็น Text file แทนครับ ลองดูโค้ดเฉพาะตรงนี้กัน ส่วนหา user log on ก็เหมือนโค้ดด้านบน


const ForReading = 1, TristateTrue = -1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("\\myserver\netlogon\UserList.txt", ForReading, false, TristateTrue)

Dim user
Dim isFound
isFound = 0

Do Until objFile.AtEndOfStream
user = trim(objFile.ReadLine)
If lcase(user) = lcase(useraccount) Then
isFound = 1
'msgbox isFound
Exit Do
End If
Loop

objFile.Close
Set objFile = Nothing
Set objFSO = Nothing

If isFound = 1 Then
'// If valid user, execute IE
Dim WSHShell
Set WSHShell = CreateObject("WScript.Shell")
WSHShell.Run "iexplore.exe -new http://myHost/myApplication"
Set WSHShell = nothing
End If

วันศุกร์ที่ ๑๗ กรกฎาคม พ.ศ. ๒๕๕๒

VBScript ดึงชื่อและอีเมลล์จาก Active Directory

เมื่อหลายวันก่อนผมต้องทำการดึง ชื่อพนักงาน และ email address ที่เก็บไว้ใน AD ออกมาเพื่อตรวจสอบ และ Import เข้า Database สำหรับไว้ใช้ในการทำ mailling list ใช้ภายในครับ ลอง search ดูใน google ก็พบว่ามีหลายวิธี และที่ปิ๊งที่สุดก็คือการเขียน VBScript เพื่อไปดึงข้อมูลจาก LDAP มาเขียนใส่ text file ครับ


Dim FileSystem
'Initialize global variables
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileSystem.CreateTextFile("email.txt", True)
Set oContainer=GetObject("LDAP://OU=Department Users,DC=MyDomainName,DC=com")
'Enumerate Container
EnumerateUsers oContainer
'Clean up
OutPutFile.Close
Set FileSystem = Nothing
Set oContainer = Nothing
WScript.Echo "Finished"
WScript.Quit(0)

Sub EnumerateUsers(oCont)
Dim oUser
For Each oUser In oCont
Select Case LCase(oUser.Class)
Case "user"
OutPutFile.WriteLine oUser.givenname & " " & oUser.sn & vbtab & oUser.mail
Case "organizationalunit" , "container"
EnumerateUsers oUser
End Select
Next
End Sub


ถ้าอยากรู้ว่า oUser (User Class) มี property อะไรบ้าง ดูตามนี้เลยครับ ไม่แน่ใจว่าครบหรือเปล่านะ


'// User Property
'SamAccountName = oUser.samAccountName
'Cn = oUser.CN
'FirstName = oUser.GivenName
'LastName = oUser.sn
'initials = oUser.initials
'Descrip = oUser.description
'Office = oUser.physicalDeliveryOfficeName
'Telephone = oUser.telephonenumber
'EmailAddr = oUser.mail
'WebPage = oUser.wwwHomePage
'Addr1 = oUser.streetAddress
'City = oUser.l
'State = oUser.st
'ZipCode = oUser.postalCode
'Title = oUser.Title
'Department = oUser.Department
'Company = oUser.Company
'Manager = oUser.Manager
'Profile = oUser.profilePath
'LoginScript = oUser.scriptpath
'HomeDirectory = oUser.HomeDirectory
'HomeDrive = oUser.homeDrive
'AdsPath = oUser.Adspath
'LastLogin = oUser.LastLogin