Hi all,
Having got my code doing what I wanted it to do in its basic format, I am now making it more efficient and so am converting it to using loops and procedures etc.
However, in my FOR EACH NEXT loop, I am getting a type mismatch on line 74, chr 5
The array with the items I am trying to work through are simply strings (each the name of a sub directory)
Please note there is only 1 for loop and it is at the end of the if statements.
My Code is:
I look forward to your assistance.
Many thanks,
Simon
Having got my code doing what I wanted it to do in its basic format, I am now making it more efficient and so am converting it to using loops and procedures etc.
However, in my FOR EACH NEXT loop, I am getting a type mismatch on line 74, chr 5
The array with the items I am trying to work through are simply strings (each the name of a sub directory)
Please note there is only 1 for loop and it is at the end of the if statements.
My Code is:
Code:
'------------------------------------------------------------------------------------------------------------
'---------------------------- Directories and Permissions creation Script -----------------------------------
'------------------------------- Copyright (C) Simon Barrett 7/8/2013 ---------------------------------------
'------------------------------------------------------------------------------------------------------------
'
' This script is designed with one thing in mind: to create a directory structure and assign permissions to
' parent and child folders accordingly.
'
' The script will ask for the full directory path of the folder to be created and will set a series of subfolders up also
'
' The script will also make security groups based on the folders created (these are hard coded in a standard format).
Option Explicit
'-----------------------------------------------------------
'------------- VARIABLE DECLARATIONS SECTION ---------------
'-----------------------------------------------------------
'----Definitions for file creation ----
Dim objFSO 'file system object
Dim objFolder 'folder object
Dim strPath 'THIS IS THE VARIABLE FOR THE EXISTING DIRECTORY
Dim strNewFolder 'THIS IS THE VARIABLE FOR THE NEW FOLDER
Dim strDirectory 'THIS IS THE EXISTING LOCATION PLUS THE NEW FOLDER NAME
Dim strSubDirectory 'THIS IS THE VARIABLE FOR THE SUBFOLDERS TO BE CREATED UNDER THE NEW FOLDER
Dim SubDirArray(3) ' THIS IS AN ARRAY WHICH WILL HOLD ALL THE SUB DIRECTORIES OF THE NEW FOLDER
SubDirArray(0) = "Section A" 'Specification of Standard Sub Directory
SubDirArray(1) = "Section B" 'Specification of Standard Sub Directory
SubDirArray(2) = "Section C" 'Specification of Standard Sub Directory
SubDirArray(3) = "Section D" 'Specification of Standard Sub Directory
Dim strSubDir 'Used in the array function
'----Other Definitions ----
Dim objShell 'used for running other commands
Dim strShellCommand 'used for storing what the command to be executed will be
'-----------------------------------------------------------
'--------------- Begin directory creation ------------------
'-----------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Ask for an input of the file to be created
strPath = InputBox( "Please enter the full path of where you would like to create the new folder:" )
if strPath = "" then 'if user input is blank
WScript.Echo "You entered nothing, this script will now close"
WScript.Quit
Else
WScript.Echo "You Entered: " & strPath
strNewFolder = InputBox( "Please enter the name of the new folder:" )
if strNewFolder = "" then
WScript.Echo "You entered nothing, this script will now close"
WScript.Quit
Else
WScript.Echo "You Entered: " & strNewFolder
strDirectory = strPath & "\" & strNewFolder
'Now that we have confirmation that stuff has been entered, we can create the actual directory:
if objFSO.FolderExists(strDirectory) then 'check if the directory exists
WScript.Echo "Folder " & strDirectory & " already exists, therefore exiting script"
WScript.Quit
else
Set objFolder = objFSO.CreateFolder(strDirectory) 'create specified directory
For Each strSubDir In SubDirArray 'loop through and create sub dirs for each one defined above
strSubDirectory = strDirectory & "\" & SubDirArray
WScript.Echo strSubDirectory
Set objFolder = objFSO.CreateFolder(strSubDirectory) 'create specified subdirectory
CreateSecurityGroups 'Call Procedure for creating security groups
Set strSubDirectory = Nothing 'Reset to empty.
Next
end if
End If
End If
Sub CreateSecurityGroups()
'-----------------------------------------------------------
'----------- Security Groups Creation Sub-Routine ----------
'-----------------------------------------------------------
'----Definitions----
Const ADS_GROUP_TYPE_GLOBAL_GROUP = &h2
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &h80000000
Dim objOU 'organisational unit
Dim objGroup 'group
Dim strGroupModify 'used for PRE 2000 NAME of modify sec group
Dim strGroupRestricted ' used for PRE 2000 NAME of restricted sec group
Dim strGroupModifyLong 'used for LONG NAME of modify sec group (basically it has "CN=" prepended)
Dim strGroupRestrictedLong 'used for LONG NAME of restricted sec group (basically it has "CN=" prepended)
Set objOU = GetObject("LDAP://ou=Security Groups,dc=barrett,dc=lan") 'Set location in Active Directory where groups will be created
strGroupModify = strNewFolder & " - Modify Access"
strGroupRestricted = strNewFolder & " - Restricted"
strGroupModifyLong = "cn=" & strGroupModify
strGroupRestrictedLong = "cn=" & strGroupRestricted
'Modify Group ****************
Set objGroup = objOU.Create("Group", strGroupModifyLong)
objGroup.Put "sAMAccountName", strGroupModify
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.SetInfo
Set objGroup = Nothing
'Restricted Group *************
Set objGroup = objOU.Create("Group", strGroupRestrictedLong)
objGroup.Put "sAMAccountName", strGroupRestricted
objGroup.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or ADS_GROUP_TYPE_SECURITY_ENABLED
objGroup.SetInfo
Set objGroup = Nothing
End Sub
'-----------------------------------------------------------
'-------------- Use ICACLS to set permissions --------------
'-----------------------------------------------------------
'remove inheritance on new folder
Set objShell = WScript.CreateObject("WScript.Shell")
'strShellCommand = "ICACLS " & chr(34) & strDirectory & chr(34) & " /inheritance:r"
'WScript.Echo "About to run the following command: " & strShellCommand
objShell.Run "ICACLS " & chr(34) & strDirectory & chr(34) & " /inheritance:r"
Set objShell = Nothing
'grant administrators full control on new folder RECURSIVELY
Set objShell = WScript.CreateObject("WScript.Shell")
'strShellCommand = "ICACLS " & chr(34) & strDirectory & chr(34) & " /grant administrators:(OI)(CI)F"
'WScript.Echo "About to run the following command: " & strShellCommand
objShell.Run "ICACLS " & chr(34) & strDirectory & chr(34) & " /grant administrators:(OI)(CI)F"
Set objShell = Nothing
'grant specific group full control on new folder RECURSIVELY
Set objShell = WScript.CreateObject("WScript.Shell")
strShellCommand = "ICACLS " & chr(34) & strDirectory & chr(34) & " /grant " & chr(34) & strGroupModify & chr(34) & ":(OI)(CI)F"
WScript.Echo "About to run the following command: " & strShellCommand
objShell.Run strShellCommand
Set objShell = Nothing
'grant specific read only control on new folder ONLY
Set objShell = WScript.CreateObject("WScript.Shell")
strShellCommand = "ICACLS " & chr(34) & strDirectory & chr(34) & " /grant " & chr(34) & strGroupRestricted & chr(34) & ":(NP)(RX)"
WScript.Echo "About to run the following command: " & strShellCommand
objShell.Run strShellCommand
Set objShell = Nothing
'-----------------------------------------------------------
'--------------------------- END ---------------------------
'-----------------------------------------------------------
Set strDirectory = Nothing 'reset strDirectory to blank
Set strPath = Nothing 'reset strPath to blank
Set strNewFolder = Nothing 'reset strNewFolder to blank
'WScript.Echo "Resetting Path Values"
WScript.QuitI look forward to your assistance.
Many thanks,
Simon