AD script giving error message

I am trying to help someone with an Active Directory script, but since its been years since I've done one of these I'm not quite finding the exact problem.

The current script we have is as follows:

*** START SCRIPT Copyhelm.VBS ***

Const IT_GROUP = "ESFIT"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME NEXT

If InStr(strGroups, IT_GROUP) Then

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe "  "c:\remotehelm\bin", OverwriteExisting
End If

** END SCRIPT Copyhelm.VBS ***

I'm pretty sure that the error is in the last line before EndIf, but am unsure of what I'm missing.  Any assistance would be greatly appreciated!
LVL 1
AyaraAsked:
Who is Participating?
 
RobSampsonCommented:
Chandru, that's not far off, but you have left the part that copies the file outside of the loop, which means it will copy the file whether the user is a member of the group or not.

This should work:

'==================
Const IT_GROUP = "ESFIT"
Const strSourceFile = "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe"
Const strDestFolder = "c:\remotehelm\bin\"
' Make sure that the trailing slash exists
If Right(strDestFolder, 1) <> "\" Then strDestFolder = strDestFolder & "\"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME Next

Wscript.echo IT_Group

If InStr(LCase(strGroups), LCase(IT_GROUP)) > 0 Then

      Const OverwriteExisting = True
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      If objFSO.FolderExists(strDestFolder) = True Then
            objFSO.CopyFile strSourceFile, strDestFolder, OverwriteExisting
      Else
            WScript.Echo "The folder " & strDestFolder & " does not exist.  Cannot copy file."
      End If
      
      'MsgBox "File copied."

Else

      'MsgBox "You are not in the group " & IT_GROUP
      
End If
'==================

Regards,

Rob.
0
 
chandru_solCommented:
Hi ,

Can you let me know what is the error message you get?
0
 
AyaraAuthor Commented:
Sorry, I had the error in the tag :)

Microsoft VBScript compilation error: expected statement

A moment ago I thought it was because he had a space between End and If, but that's not it either...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
MSE-dwellsCommented:
... I've really not even digested the logic but I did notice there's a space trailing the source file name ... it could be there in reality or perhaps it's an artifact of the paste??
0
 
chandru_solCommented:
Can you try this....

Const IT_GROUP = "ESFIT"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME NEXT

If InStr(strGroups, IT_GROUP) Then

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe"  "c:\remotehelm\bin", OverwriteExisting

End If
0
 
AyaraAuthor Commented:
No space there...must have been an extra from pasting it here :(

I'm sure this is something easy that I'm overlooking...those are the ones that get ya every time...
0
 
AyaraAuthor Commented:
Chandru:  Tried having him overwrite with what you have, and now he gets the error: expected end of statement
0
 
chandru_solCommented:

Can you try this code? Change the group name and copy location

Const IT_GROUP = "CHE_IT"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME NEXT

Wscript.echo IT_Group

If Ucase(InStr(strGroups, IT_GROUP)) Then

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\logs\test.txt" ,"C:\scripts\", OverwriteExisting

End If
0
 
chandru_solCommented:
Hi,

Modified to suit your script....

Const IT_GROUP = "ESFIT"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME NEXT

Wscript.echo IT_Group

If Ucase(InStr(strGroups, IT_GROUP)) Then

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe" ,"c:\remotehelm\bin\", OverwriteExisting

End If
0
 
AyaraAuthor Commented:
No errors this time, but the script did not copy the files.  On a whim I had him also try changing the UCASE in the IF statement to LCASE but that did nothing either.

Basically the point of this script (if it helps) is that he wants members of certain groups (but not organizational units) to have files copied to their profiles on login.
0
 
MSE-dwellsCommented:
This is better suited to shell script, say -

[BEGIN SCRIPT]
ifmember ESFIT
if errorlevel 1 (
   copy "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe" "c:\remotehelm\bin" /y
)
[END SCRIPT]

NOTE - ifmember is available in the Windows Resource kit

0
 
chandru_solCommented:
Hi,

Can you try this version?

ON ERROR RESUME NEXT

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

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
'Find the Windows Directory
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'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)

'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName

'Declare variables

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Now check for group memberships and map appropriate drives
'Note that this checks Global Groups and not domain local groups.
For Each GroupObj In UserObj.Groups
'Force upper case comparison of the group names, otherwise this is case sensitive.
wscript.echo GroupObj.Name
    Select Case UCase(GroupObj.Name)

    'Check for group memberships and take needed action
    'In this example below, ADMIN and WORKERB are groups.
    'Note the use of all upper case letters as mentioned above.
    'Note also that the groups must be Global Groups.
        Case "ESFIT"
            objFSO.CopyFile "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe" ,"c:\remotehelm\bin\", OverwriteExisting

    End Select
Next

wscript.echo "Completed"

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

regards
Chandru
0
 
AyaraAuthor Commented:
I think you may have just hit on what I was trying to get a response from him about.  I was looking at his code and not thinking out the logic.  Once I get his response I'll have him try that, because I think that's more what he is trying to accomplish.

Will hopefully know in a few - thanks for the continuing help!
0
 
RobSampsonCommented:
Hi guys, if I can just offer my input, in Chandru's script at post ID 20017223 the UCase is in the wrong spot, and so is not making the correct string comparison.  The thing is, you originally had:
   strGroups = LCase(Join(CurrentUser.MemberOf))
which means you have a string of groups in lower-case only.  But then you defined
   Const IT_GROUP = "ESFIT"
as having only upper-case letters, and the InStr function is case sensitive, so the
   If InStr(strGroups, IT_GROUP) Then
would never return True.

Similarly, in Chandru's new code, in that post I mentioned, there is
   If Ucase(InStr(strGroups, IT_GROUP)) Then
which is *almost* right....but, if you're in a situation where you don't need case sensitivity, you should explicitly force the comparison to a specific case, by using
   If InStr(LCase(strGroups), LCase(IT_GROUP)) > 0 Then

I have also added a check to see if the target folder exists....so the code should look like this:

'==================
Const IT_GROUP = "ESFIT"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

ON ERROR RESUME NEXT

Wscript.echo IT_Group

If InStr(LCase(strGroups), LCase(IT_GROUP)) > 0 Then

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\remotehelm\bin") = True Then
      objFSO.CopyFile "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe", "c:\remotehelm\bin\", OverwriteExisting
Else
      WScript.Echo "The folder C:\Remotehelm\bin\ does not exist.  Cannot copy file."
End If

End If
'==================

Regards,

Rob.
0
 
AyaraAuthor Commented:
I've been working with him on and off all day on this, trying the options you all provided above as well as a complete rewrite on my end.  He keeps getting a variety of errors, and since I'm not familiar enough with Active Directory I've pretty much lost my mind on this one.  As of the latest revision, we are not getting any expected end of statement errors, but we are getting a variety of unrecognized character ones now that I can't figure out.  The latest code is as follows:

Const strDomGroup = "ESFIT"

Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

If strGroups = lcase(strDomGroup) then
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

If Not objFileSystem.FolderExists("c:\remotehelm\bin\") Then
      objFileSystem.CreateFolder("c:\remotehelm\bin\")
End If

Set objFile = objFileSystem.GetFile (\\originations2\Win2k50\remotehelm\bin\remotehelm.exe")
objFile.Copy " c:\remotehelm\bin\", true
Set objFile = Nothing

wscript.echo "Completed"

End If

Set objShell = Nothing
Set objFileSystem = Nothing


From how I understand it (I'm not a server person, so my terminology may be wrong here), he is going to be using a GPO to (push these scripts?).  However, the GPO is pushing to the OU, and he doesn't want to have this file copied to everyone in the OU, just one of the groups.  

And I'm increasing the value of this question, simply because I know I'm out of my league now and feeling pretty dumb ;)
0
 
chandru_solCommented:
Hi Ayara,
You can try this version....

I have checked this and it works when the user is member of group ESFIT

Const strDomGroup = "ESFIT"

Set WshNetwork = CreateObject("WScript.Network")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysinfo.UserName)
strGroups = LCase(Join(CurrentUser.MemberOf))

If InStr(LCase(strGroups), LCase(strDomGroup)) > 0 Then

'If strGroups = lcase(strDomGroup) then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strDirectory = "C:\remotehelm\bin\"

' Create the File System Object objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check whether the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
' Here is the key method .CreateFolder
Set objFolder = objFSO.CreateFolder(strDirectory)

End If

Const OVERWRITE_EXISTING = True
'Global variables
Source = "\\originations2\Win2k50\remotehelm\bin\remotehelm.exe"
Dest = strDirectory
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
Wscript.echo Source & " " & Dest
End If
wscript.echo "Completed"
'Clean up
Set objShell = Nothing
Set objFileSystem = Nothing
0
 
chandru_solCommented:
Thanks Rob!
0
 
AyaraAuthor Commented:
That did it guys!  Thanks so much!!  You all are the best :)
0
 
RobSampsonCommented:
Good to hear.  Thanks Ayara.

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.