Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

AD script giving error message

Posted on 2007-10-04
19
Medium Priority
?
936 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:Ayara
  • 7
  • 7
  • 3
  • +1
19 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20017002
Hi ,

Can you let me know what is the error message you get?
0
 
LVL 1

Author Comment

by:Ayara
ID: 20017034
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
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20017045
... 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:chandru_sol
ID: 20017078
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
 
LVL 1

Author Comment

by:Ayara
ID: 20017087
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
 
LVL 1

Author Comment

by:Ayara
ID: 20017180
Chandru:  Tried having him overwrite with what you have, and now he gets the error: expected end of statement
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20017181

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
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20017223
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
 
LVL 1

Author Comment

by:Ayara
ID: 20017601
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
 
LVL 9

Expert Comment

by:MSE-dwells
ID: 20017675
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
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20017697
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
 
LVL 1

Author Comment

by:Ayara
ID: 20017708
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 20018121
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
 
LVL 1

Author Comment

by:Ayara
ID: 20025147
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
 
LVL 12

Assisted Solution

by:chandru_sol
chandru_sol earned 400 total points
ID: 20027230
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 600 total points
ID: 20032180
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
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20032351
Thanks Rob!
0
 
LVL 1

Author Comment

by:Ayara
ID: 20034834
That did it guys!  Thanks so much!!  You all are the best :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20037527
Good to hear.  Thanks Ayara.

Regards,

Rob.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question