Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script Question 2

Status
Not open for further replies.

edcoder

MIS
Jan 26, 2005
5
0
0
US
Hello all, and a special thanks to markdmac for you AWESOME login script.

I would like to add a line to markdmac's script, but I am not sure how to do it. In his script using the addon to the Windows Speech API we are able to greet users by using their username. I would like to do this, but instead of using username, I would rather have display name. Our naming convention that I inherited is b_smith so it just sounds kind of funny. I think it would be cool if we could have it say the actual display name like Bob Smith instead. Does anyone know how to do this?

Thanks in advance!
 
Yes, you can do this but it is a bit more code than just a single line as the WSHNETWORK can only give you the UserName, UserDomain or ComputerName.

You will need to first bind to the user object using LDAP and then you can user the context of:
Code:
set objUser = [red]Code to bind to user object here[/red]
oVo.speak "Good Morning " & objUser.displayName

So what you will wnat to do is use a function that takes the UserString from my script and binds to the user object in AD via an LDAP query.

Sounds scary but all the code you need is already written in an FAQ. You will find excellent examples of the code you need to bind to the user object in faq329-5688.

Keep an eye on my FAQ faq329-5798 and you may see this as an update in the next few weeks. Work schedule is a bit too busy right now for me to do it.

I hope you find this post helpful.

Regards,

Mark
 
markdmac,

You are the MAN! Thanks so much for your help! I got it to work! People in the office think you are genius!

Best regards!
 
:-D
Glad you like it. I think that speach API is pretty cool too. Here is a hint for you. If you install the free Microsoft SAPI SDK, you can change the voice too!



I hope you find this post helpful.

Regards,

Mark
 
MArk,

I attempted to put in the right code to bind the user obhect to make this work and it didn't work. Could you possibly post it? I did check for an update on your faq but as you said you've been busy so I didn't expect to find it there yet.

Thanks,

Aaron
 
Sorry, I won't have time till after next wednesday probably. Edcoder, would you mind sharing your solution with the group?

I've been delaying doing this because I want it to "do more" in the way I will implement it. So, it should be worth the wait. :)

I hope you find this post helpful.

Regards,

Mark
 
Sure markdmac!

Before I post this, please note that I am in no way as good at VBS as markdmac. Everything I know I have taught myself, and I do not consider myself proficient in any way! Also, I used all of markdmac's code and the link to the FAQ and that code to implement. I hope I have not opened any security holes!

Here is what I used:
'==========================================================================
'
' NAME: LogonScript.vbs
'
' AUTHOR: Mark D. MacLachlan, The Spider's Parlor
' URL : ' DATE : 4/10/2003
'
' COMMENT: Enumerates current users' group memberships in given domain.
' Maps and disconnects drives and printers
'
'==========================================================================


ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path

Public Function SearchGivenName(ByVal vSAN)
' Function: SearchGivenName
' Description: Searches the Given Name for a given SamAccountName
' Parameters: ByVal vSAN - The SamAccountName to search
' Returns: The Given Name
Dim oRootDSE, oConnection, oCommand, oRecordSet

Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
">;(&(objectCategory=User)(samAccountName=" & vSAN & "));name;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchGivenName = oRecordSet.Fields("name")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'Synchronizes the time with Server our NTP Server
WSHShell.Run "NET TIME \\Server /set /y"

'Disconnect any drive mappings as needed.
WSHNetwork.RemoveNetworkDrive "F:", True, True

'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next

'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 300

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
WSHNetwork.MapNetworkDrive "H:", "\\server\users\" & UserString,True
WSHNetwork.MapNetworkDrive "U:", "\\server\users",True
WSHNetwork.MapNetworkDrive "X:", "\\server\executables",True

'Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups
Select Case GroupObj.Name
'Check for group memberships and take needed action
'In this example below, ADMIN and WORKERB are groups.
Case "Admin"
WSHNetwork.MapNetworkDrive "w:", "\\Server\Admin Stuff",True
Case "WorkerB"
WSHNetwork.MapNetworkDrive "w:", "\\Server\Shared Documents",True
'Below is an example of how to set the default printer
WSHNetwork.SetDefaultPrinter "\\ServerName\PrinterName"
End Select
Next

'Remove ALL old printers
'Enumerate all printers first, after that you can select the printers you want by performing some string checks
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
End If
'To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
'WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
Next

'Remove a specific printer
WSHNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True

'Install Printers
WSHNetwork.AddWindowsPrinterConnection "\\Server\HP5si"

'Add On Code goes below this line
'=====================================

' This Add On demonstates the Microsoft Speach API (SAPI)
'=====================================
Dim oVo
Set oVo = Wscript.CreateObject("SAPI.SpVoice")
oVo.speak "Good Morning " & SearchGivenName(UserObj.displayName)


'=====================================
'Add On Code goes above this line

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing


'Quit the Script
wscript.quit
 
Thanks Edcoder,

I am like you teaching myself as I go and gladly ran into Mark on this forum and have learned and used much of what he has created.

Thanks everyone,

Aaron
 
Edcoder, thanks for posting.

Some quick feedback, and this is in no way a critisism but more an example of why I have not had time to do this the way I would like to yet. There is a lot of unnecessary code here. As an example take a look and you will see that both my script and the SearchGivenName function both declare the RootDSE. Our variable names are different but we are grabbing the same thing. While there is nothing wrong with this, it makes the code longer than needed and therefore a bit harder to follow and over time (and size) a bit slower to execute. Nothing you would notice at this time however.

When I get a chance some time next week I will clean this up a bit to re-use the bits of code I already have in my script. I also like to put functions at the end but they can go anywhere. I just like to be able to read through the script from top to bottom that way. Just my preference. For my FAQ I will instruct people to add that function within my Add-On Code section for uniformity.

I hope you find this post helpful.

Regards,

Mark
 
OK Guys, I actually had a break at work and decided to tackle this. There wasn't as much duplicated code as I had thought there was, but I think you will like my enhancements. Rather than hard coding Good Morning, my code will determine if it is morning, afternoon or evening and give the appropriate greeting. You can also select if you want the computer to say:

Good Morning Mark
Good Morning MacLachlan
Good Morning Mark MacLachlan

One thing to consider is that the way I have altered the function could be used for other purposes. For example, suppose you had folders named by last name and not user ID. You could map drives using the last name as a variable by calling the function as I have demonstrated.



I hope you find this post helpful.

Regards,

Mark
 
markdmac,

You are truly a gentlemen and a scholar! Thanks for the info, and the constructive criticism! I can’t wait to see your good-morning, afternoon, and evening code! That is AWESOME! I thought about doing that, but that is above my knowledge! Anyway, I understand what you mean when you say unnecessary code. I didn’t notice that at first, or maybe I did, but it didn’t hit me. In large apps that would be very cumbersome, but you are right in it’s form right now it is not. However, I want to be a good coder someday and with good people like you in the world offering assistance and my desire to be a good coder, I believe that I will.

Thanks so much for you help, and I can’t wait to see your revised code!

You are the man!
 
Just stick with it and you will find you can do most anything with the scripting.

Best advice I can offer is think about anything you need to do. If you will need to ever do it again, script it. If you say to yourself "it will be faster to just do it manually" then pinch yourself and write the script. In the long run you will be thankful you took the extra time to do it.

This is such a powerful tool and I am thankful that I have learned it. It really helps me stand out as a consultant above others in my profession. My customers are so amazed at the types of automation I can help them with. It really is quite gratifying.

I hope you find this post helpful.

Regards,

Mark
 
Now if I could just write a VBscript to do my whole job I could retire =)

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top