Link to home
Start Free TrialLog in
Avatar of ieden
ieden

asked on

Help with fairly complex login script

Would like someone to design a script for the environment I manage that does a number of things. These items must take place in numerical order. May be a .bat file however, .vbs, .cmd  and .ps1 are acceptible.
The script needs to be able to do the following:
1. run at logon via AD GPO.
2. immediately sleep for 10 seconds.
3. determine day of week:
 * on specified day of week displays pop up message.
 * on day message displayed, message must be visible for minimum 5 seconds.
 * message can contain an "OK" button to confirm.
4. script must contain a section that does the following:
 * ping an IP address
 * if ping unsuccesful, ping again.
 * if ping successful, continue script.
5. Script must run IPCONFIG /REGISTERDNS
6. script must map multiple drives.
 * F:, P:, S:, and V:
 * group membership determines drive mapping.
 * Share path varies with group membership.
7. script must copy several files from one of the mapped drives to a location on the hard disk.
 * copy F:\txt1\1.txt C:\folder1
 * copy F:\txt1\2.txt C:\folder1
 * copy F:\txt2\3.txt C:\folder2
Avatar of BillBondo
BillBondo
Flag of United States of America image

kixtart, simple and I believe free
What does your existing script look like?  Is it something that you could share, so that we have a starting point moving forward?  

Example variables that you can use.
 
Dim objFSO
Dim fServer
Dim pServer
Dim objNetwork
Dim objWinntUser

Const strSG1 = "Billing"
Const strSG2 = "HR"

Open in new window


Put me to sleep for 10 seconds.
 
WScript.Sleep 10000

Open in new window


Day of Week
simple case statement
 
dtmToday = Date()
dtmDOW = DatePart("w", dtmToday)

Select Case dtmDOW
    Case 1 MsgBox "Sunday"
    Case 2 MsgBox "Monday"
ETC...

Open in new window



Simple Ping
objShell.Run can also be used to /registerdns
 
boolPing = Not CBool(objShell.run("ping -n 2 " & strComputer, 0, True))
      If boolPing = True Then
         
      Else

Open in new window


More Variables for Shares and Copies
 
fServer = "\\fServer"
pServer = "\\pServer"
strSrc1 = "\\Server_1\Share_2"
strDest1 = "C:\Folder1"
strSrc2 = "\\Server_2\Share_3"
strDest2 = "C:\Folder2"

Open in new window


AD Connection
 
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

Open in new window


Verify Group Membership, Map and Copy
 
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG1) = True Then
    objNetwork.MapNetworkDrive "F:", "\\Server_1\Share_1", 0, True
    objNetwork.MapNetworkDrive "P:", "\\Server_1\Share_2", 0, True
    objFSO.CopyFile strSrc1, strDest1
End If

Open in new window

Avatar of ieden
ieden

ASKER

Did I mention I'm not the sharpest crayon in the box?
@yelbaglf: although I understand your descriptions of what the snippets do, I wouldn't know what to edit... I mean, if you could highlight or bold the word or phrase I may need to change...
Currently, we map network drives with .bat files applied by GPO's
@echo off
net use F: /delete
net use F: \\serve.domain.prvr\share
copy F:\dir\file.txt c:\dir /y /s
next
\\domain.prv\sysvol\ifmember.exe group2
net use S: /delete
net use S: \\server\share2
exit
Something like the below might do the trick, but I haven't tested it.  Everything in bold will need to be changed to their appropriate values/names, and if you need more of something, just add them.  For example if you need more strSrc (Source Copy) and strDest (Destination Copy), just add them to the script underneath what's existing.

If you need more Security Groups, then just add more strSG constants.  Remember that everything listed as 'Define variables or 'Something is a comment, and for display purposes, they are italicized.  Hopefully this helps and makes more sense.

Please take a stab at updating it and testing it, and let me know how it goes.  Also, let me know if you run across any questions.

'Basic error handling
On Error Resume Next

'Define variables
Dim objFSO
Dim fServer
Dim pServer
Dim objNetwork
Dim objWinntUser

'Define Constants
Const strSG1 = "Billing"
Const strSG2 = "HR"

Set objShell=CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Put me to sleep for 10 seconds
WScript.Sleep 10000

'Message box display DOW with OK
dtmToday = Date()
dtmDOW = DatePart("w", dtmToday)

Select Case dtmDOW
    Case 1 MsgBox "Sunday"
    Case 2 MsgBox "Monday"
    Case 3 MsgBox "Tuesday"
    Case 4 MsgBox "Wednesday"
    Case 5 MsgBox "Thursday"
    Case 6 MsgBox "Friday"
    Case 7 MsgBox "Saturday"
End Select

'Ping node
strComputer = "ComputerName_Ping"
boolPing = Not CBool(objShell.run("ping -n 2 " & strComputer, 0, True))
      If boolPing = True Then
         
      Else
         boolPing = Not CBool(objShell.run("ping -n 2 " & strComputer, 0, True))
         If boolPing = True Then
         
         Else
              WScript.Quit
         End If
      End If

'registerdns
ipReg = objShell.run("ipconfig /registerdns" & strComputer, 0, True)

'Set variable values
fServer = "\\fServer"
pServer = "\\pServer"
strSrc1 = "\\Server_1\Share_2"
strDest1 = "C:\Folder1"
strSrc2 = "\\Server_2\Share_3"
strDest2 = "C:\Folder2"

'**** AD connection ****
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

'**** Begin group verification, map printers or shares, and copy files ****
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG1) = True Then
    objNetwork.MapNetworkDrive "F:", "\\Server_1\Share_1", 0, True
    objNetwork.MapNetworkDrive "P:", "\\Server_1\Share_2", 0, True
    objFSO.CopyFile strSrc1, strDest1
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG2) = True Then
    objNetwork.MapNetworkDrive "V:", "\\Server_2\Share_3", 0, True
    objNetwork.MapNetworkDrive "S:", "\\Server_1\Share_2", 0, True
    objFSO.CopyFile strSrc2, strDest2
End If
ASKER CERTIFIED SOLUTION
Avatar of Darren Collins
Darren Collins
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Daz_1234 makes a good point in that you can call it as a function, but this isn't necessary.  The idea here was to do something simple, so that it would be easier to understand, and it actually works just fine without being called as a function.  When the script runs, it connects to AD, and then it works through the If, Else statements, mapping appropriately.
Scratch that...just ran it, and it failed. :-)  I knew I should have tested it first.  Add this to the bottom of your script, as stated by Daz_1234.

 
Function IsMemberOfGroup(strUserDomain, objUser, strGroup)
    IsMemberOfGroup = False
    Dim objGroup
    On Error Resume Next
    Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
    If Err.Number Then
        IsMemberOfGroup = "Error"
    Else
        IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
    End If
End Function

Open in new window

Avatar of ieden

ASKER


Yup, I'm confused...
So that last part has me as close to migraine as I've ever been.

I'm working away trying to incorporate our environment and I'm not sure what I'm doing will work anyway.


Back to my original question for starters;

Day of week...
I'm looking for the message to pop on a day of week for a particular group of users.

Monday, users in an AD group named Sales

Tuesday, users in a group named Finance and another group named Billing

Wednesday, users in Executives.

Thursday, etc...



Each Group is also in an OU of their own if that helps...
OU=ARBilling,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
OU=ARExecutive,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
OU=ARFinance,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
OU=ARSales,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV

Of corse the users are in groups within those OU's like this one...
CN=AllBillingUsers,OU=ARBilling,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
CN=AllExecutives,OU=ARExecutive,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
CN=AllFinanceUsers,OU=ARFinance,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV
CN=AllSalesUsers,OU=ARSales,OU=Dallas,OU=GRP,OU=My Corp,DC=LARD,DC=CSG,DC=PRV





Now, The message should display for at least 5 seconds or until the user clicks OK.


I think the Ping part is pretty straight forward and have tested that working.



There is a section that looks like this:
'Defines Constants

Const strSG1 = "Sales"

Const strSG2 = "Billing"
How many of these Const strSG#'s can I use? strSG20 - strSG30?

Should the Const strSG# = "name" be the same as AD groups?

The ipconfig /registerdns seems pretty straightforward as well.

Because I am not the brightest bulb on the tree, I have to ask:
What is the difference between a fServer and a pServer?
The day of the week shouldn't be a problem.  I'm thinking we can use vbscript's popup with a timeout for this.  I'll see what I can put together.

As for the constants, you can define as many as you need to use in the various parts of your script.  When declaring constants or variables, it's always easiest to choose something that makes sense, and then as you script in later life, reuse those same constants and variables.  So in this scenario, where we have strSG# = 'name', you'll want 'name' = 'Full Security Group Name'.  So if one group is 'Billing', then strSG# = "Billing".  If another is 'Bill and Ted', then strSG# = "Bill and Ted".  

Lastly, fServer and pServer are just variable names that I made up, and they can be changed to anything you'd like really.  But in this case, I have them representing File Server and Print Server respectively.
As for the last part regarding the Function that was posted, if this isn't in the script, then the script will map all shares or printers or whatever, irregardless of group membership.  So in order for us to test group membership with IsMember, then we'll need the function included.  I reposted the script with this included.  Notice all I did was put it at the end of the script.

 
'Basic error handling
On Error Resume Next 

'Define variables
Dim objFSO
Dim fServer
Dim pServer
Dim objNetwork
Dim objWinntUser

'Define Constants
Const strSG1 = "Billing"
Const strSG2 = "HR"

Set objShell=CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Put me to sleep for 10 seconds
WScript.Sleep 10000

'Message box display DOW with OK
dtmToday = Date()
dtmDOW = DatePart("w", dtmToday)

Select Case dtmDOW
    Case 1 MsgBox "Sunday"
    Case 2 MsgBox "Monday"
    Case 3 MsgBox "Tuesday"
    Case 4 MsgBox "Wednesday"
    Case 5 MsgBox "Thursday"
    Case 6 MsgBox "Friday"
    Case 7 MsgBox "Saturday"
End Select

'Ping node
strComputer = "ComputerName_Ping"
boolPing = Not CBool(objShell.run("ping -n 2 " & strComputer, 0, True))
      If boolPing = True Then
         
      Else
         boolPing = Not CBool(objShell.run("ping -n 2 " & strComputer, 0, True))
         If boolPing = True Then
         
         Else
              WScript.Quit
         End If
      End If

'registerdns
ipReg = objShell.run("ipconfig /registerdns" & strComputer, 0, True)

'Set variable values
fServer = "\\fServer"
pServer = "\\pServer"
strSrc1 = "\\Server_1\Share_2"
strDest1 = "C:\Folder1"
strSrc2 = "\\Server_2\Share_3"
strDest2 = "C:\Folder2"

'**** AD connection ****
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

'**** Begin group verification, map printers or shares, and copy files ****
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG1) = True Then
    objNetwork.MapNetworkDrive "F:", "\\Server_1\Share_1", 0, True
    objNetwork.MapNetworkDrive "P:", "\\Server_1\Share_2", 0, True
    objFSO.CopyFile strSrc1, strDest1
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG2) = True Then
    objNetwork.MapNetworkDrive "V:", "\\Server_2\Share_3", 0, True
    objNetwork.MapNetworkDrive "S:", "\\Server_1\Share_2", 0, True
    objFSO.CopyFile strSrc2, strDest2
End If 

Function IsMemberOfGroup(strUserDomain, objUser, strGroup)
    IsMemberOfGroup = False
    Dim objGroup
    On Error Resume Next
    Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
    If Err.Number Then
        IsMemberOfGroup = "Error"
    Else
        IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
    End If
End Function

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ieden

ASKER

Only issue I see with what you have above is the DOW popup needs to occur before the mapping of the drive. It's a timing thing that we need to have in place.

I think I may be able to figure something out on what you have written above,

The script will undoubtedly be very large, several hundred lines to incorporate everything I'm looking for.

Is this example plausible?
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG1) = True Then
    If dtmDOW = 2 Then
            strMon = objShell.Popup("Monday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If

I'll just have to include a segment like the above for each group and then continue with a segment containing the mappings thereafter. Maybe...
Exactly, you'll have to perform a test for security group membership at the point in the script that you'd like to have the popup.  And then another for the drive mappings, etc.
Avatar of ieden

ASKER

I'm losing it...
Avatar of ieden

ASKER

I'm a little lost here...
According to my limited understanding, this will pop up a message... I don't think it will pop up the message I want but something will show to the users referenced in the strSG## group...
'Message box display DOW with OK
dtmToday = Date()
dtmDOW = DatePart("w", dtmToday)

Select Case dtmDOW
    Case 1 MsgBox "Today is Sunday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 2 MsgBox "Today is Monday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 3 MsgBox "Today is Tuesday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 4 MsgBox "Today is Wednesday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 5 MsgBox "Today is Thursday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 6 MsgBox "Today is Friday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
    Case 7 MsgBox "Today is Saturday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete."
End Select

If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG40) = True Then
    If dtmDOW = 1 Then
            strMon = objShell.Popup("Sunday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG41) = True Then
    If dtmDOW = 2 Then
            strMon = objShell.Popup("Monday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG42) = True Then
    If dtmDOW = 3 Then
            strMon = objShell.Popup("Tuesday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG43) = True Then
    If dtmDOW = 4 Then
            strMon = objShell.Popup("Wednesday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG44) = True Then
    If dtmDOW = 5 Then
            strMon = objShell.Popup("Thursday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG45) = True Then
    If dtmDOW = 6 Then
            strMon = objShell.Popup("Friday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG46) = True Then
    If dtmDOW = 7 Then
            strMon = objShell.Popup("Saturday",5,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If

 

Open in new window

I'd like the messages typed out display in those popup boxes...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ieden

ASKER

You are being very patient and kind with me.
I'm under a little more than some pressure to get this solution in place before the begining of next week. So I am very grateful for your assitance.
Undoubtedly I will be asking for some clarification and a review of the final script. Somethign I guess I could be partly proud of. :-)
You are most welcome!  And I have to say, you are doing a fantastic job, and before you know it, you'll be finding all types of things to script for your environment!

I absolutely will keep an eye out for your next post, and I'll be more than happy to take a final look.  I'm here till we get it working the way you need it.
Avatar of ieden

ASKER

Are we still using the section that looks like this:
'Set variable values
aServer = "\\server\root\Sales"
bServer = "\\server\root\Billing"
cServer = "\\server\root\Finance"
strSrc1 = "F:\Script\Log\docfile.txt"
strDest1 = "C:\logs /y"
strSrc2 = "F:\Script\Log\docattr.txt"
strDest2 = "C:\logs2 /y"

Open in new window

I don't see the variables in any lines... I mean, I see the strSrc1 & 2 and the strDest1 & 2 but not the servers...
I don't recall what aServer, bServer, and cServer are doing.  Is this for the copy portion or drive mapping portion?  All that's required for 'CopyFile' or 'CopyFolder' is a source and destination, which would be the strSrc# and strDest#.

The drive mapping can use a variable as well, or you may choose to just type in the UNC path.  I find variables easier to manage in the long-term.  So if aServer, bServer, and cServer are for this, then yes we can use them.  You can do either of these:


objNetwork.MapNetworkDrive "F:", "\\Server_1\Share_1", 0, True   'Drive mapping with UNC path
objNetwork.MapNetworkDrive "P:", aServer, 0, True   'Drive mapping with defined variable
objFSO.CopyFile strSrc1, strDest1   'This is for copying a file from source to destination
objFSO.CopyFolder strSrc1, strDest1     'This is for copying a folder from source to destination

Open in new window

Avatar of ieden

ASKER

so the variable aserver does not have to be in quotations in order to work but a specific "\\server\share" would be?
Absolutely correct!  I'm not sure if this helps, but here's a quick tutorial about variables.

http://www.w3schools.com/vbscript/vbscript_variables.asp
Avatar of ieden

ASKER

This weekend has been a bit tough to do any testing, our company did a datacenter power down to tighten all electrical connectors and install a new UPS management interface, replaced two core switches and hardware disk controllers on our power iSeries systems.. Lots of work and no freedom to do the other things. Will get back to testing shortly. Plus, I reloaded my computer Wednesday :-) should be back to testing this in a day or two. After all users have had their issues addressed from the power down
Sounds good!  Good Luck!
Avatar of ieden

ASKER

OK, so I've tried testing an abbreviated script. Perhaps I am doing something wrong, IDK. The contents of which are as follows:
'***Basic error handling***
On Error Resume Next 

'***Define variables***
Dim objFSO
Dim fServer
Dim pServer
Dim objNetwork
Dim objWinntUser

'***Define Constants***
Const strSG40 = "NACSun"
Const strSG41 = "NACMon"
Const strSG42 = "NACTue"
Const strSG43 = "NACWed"
Const strSG44 = "NACThu"
Const strSG45 = "NACFri"
Const strSG46 = "NACSat"

Set objShell=CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'***Put me to sleep for 10 seconds***
'WScript.Sleep 10000

'***Message box display DOW with OK***
dtmToday = Date()
dtmDOW = DatePart("w", dtmToday)
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG40) = True Then
    If dtmDOW = 1 Then
            strMon = objShell.Popup("Today is Sunday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG41) = True Then
    If dtmDOW = 2 Then
            strMon = objShell.Popup("Today is Monday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG42) = True Then
    If dtmDOW = 3 Then
            strMon = objShell.Popup("Today is Tuesday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG43) = True Then
    If dtmDOW = 4 Then
            strMon = objShell.Popup("Today is Wednesday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG44) = True Then
    If dtmDOW = 5 Then
            strMon = objShell.Popup("Today is Thursday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG45) = True Then
    If dtmDOW = 6 Then
            strMon = objShell.Popup("Today is Friday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If
If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strSG46) = True Then
    If dtmDOW = 7 Then
            strMon = objShell.Popup("Today is Saturday. Your computer will be scanned by the NAC device. Please be patient as this may take several minutes to complete.",15,"Day of Week",vbOKOnly)
      Else
            'WScript.Echo "Not Monday"
      End If
End If

Open in new window

Now, you know I'm no expert at getting these things working but I figured I would start with the hard stuff first. The groups referenced have other groups in them I am nesting AD groups to get the desired effect and shorten the number of lines required to code. If this is wrong, let me know now so I can start rewritting.
You must include the lines below in your script: I would put them at the end.  This is the function that you are calling to establish if the user is a member of the group.
'Add this function to your code in order to use it!!!
Function IsMemberOfGroup(strUserDomain, objUser, strGroup)
      IsMemberOfGroup = False
      Dim objGroup
      On Error Resume Next
      Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
      If Err.Number Then
            IsMemberOfGroup = "Error"
      Else
            IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
      End If
End Function

Open in new window

If IsMemberOfGroup etc. is not a standard VBScript command, it is a call to the function I have pasted above (originally written by Rob Sampson).

One of the biggest mistakes being made here is using 'On Error Resume Next' for testing and troubleshooting.  Once the script is all perfect and robust, you can think about hiding any errors.

Allow me to demonstrate.  In the following code, there is an error at line 1 "Type mismatch: 'IsMemberOfGroup'"
If IsMemberOfGroup("DOM", "USER", "somestring") = True Then
   MsgBox "True"
Else
   MsgBox "False"
End If

Open in new window

... but in the following code, the If always returns 'True', because the On Error Resume Next is covering up the error (which is that the function does not exist and IsMemberOfGroup is not an array).
On Error Resume Next

On Error Resume Next

If IsMemberOfGroup("DOM", "USER", "somestring") = True Then
   MsgBox "True"
Else
   MsgBox "False"
End If

Open in new window



Regards,
Daz.
Avatar of ieden

ASKER

Got into a little more of the sleep command... It appears that sleep does not work with Windows 7. Whereas timeout does work for both Win7 and XP. So I'm playing around with that a bit. Still can't get DOW to popup in testing. Seems like this may be a bit too complicated or our network is too diverse to get a cohesive script to run on all PC's...
Trying to learn this stuff is a bit more complicated the older you get. I'm conceding my age is a factor in this and I'm only 48...
I tested the very last script post of yours, and it works fine for me on Windows 7 and XP 32-bit.  The 'sleep' command also works for me on Windows 7 and XP.  BUT there are 2 things your last post is missing...

The first is the IsMemberOfGroup function...just add this to the very bottom of your script.

 
Function IsMemberOfGroup(strUserDomain, objUser, strGroup)
    IsMemberOfGroup = False
    Dim objGroup
    On Error Resume Next
    Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
    If Err.Number Then
        IsMemberOfGroup = "Error"
    Else
        IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
    End If
End Function

Open in new window


The second thing you're missing is the Active Directory connection, which is required in order for the IsMemberOfGroup function to test for security group membership.  Just paste this immediately after your 'sleep' command.

 
'**** AD connection ****
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

Open in new window

I meant to ask how you're testing your script?  Are you just running it while logged in, or are you letting it run as a logon script?
Avatar of ieden

ASKER

I have a test ou and a test user with administrative rights to the workstation. Both the computer and user are in the test ou. The test PC is WinXP
I also have linked to the policy to my ou and added myself as the target. The domain is 2008r2 fully upgraded My workstation is Win764bit. I run with admin privs...
Are you seeing that it runs more consistently on XP than on Win 7?  If so, something that we do in our environment, and this has been a challenge for many, is use group policy to force the PC's to wait until the network is initialized.  After setting this GPO, our scripts run flawlessly between disparate OS's.

Here's the KB which talks about this GPO.
http://support.microsoft.com/kb/304970
While I don't have a 64-bit Win 7 machine, I was able to test this on Server 2008 R2 which is 64-bit, and it was successful.  The GPO I posted earlier should help you get this working consistently.

Another way to test would be to copy the script over to your desktop, and then you can just run it by double-clicking it.
Avatar of ieden

ASKER

Although the complete script is unfinished at this time, it is unfair to keep this thread open. You all have been very patient and kind with me. Split points to all answers that contributed.