-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADUserFolderCreator.au3
More file actions
235 lines (183 loc) · 7.79 KB
/
Copy pathADUserFolderCreator.au3
File metadata and controls
235 lines (183 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=_Icon.ico
#AutoIt3Wrapper_Change2CUI=n
#AutoIt3Wrapper_Res_Fileversion=1.0.0.76
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#RequireAdmin
#NoTrayIcon
#include <Constants.au3>
#Include <String.au3>
Global $Title = @ScriptName
Global $LogToFile = True
Global $LogFileMaxSize = 1024
_ConsoleWrite("Start - " & $Title)
;Command line parameters
If $CmdLine[0] >= 1 Then
$Parameter1 = $CmdLine[1]
Else
$Parameter1 = ""
EndIf
If $Parameter1 = "update" Then
$Update = True
Else
_ConsoleWrite("Dry Run")
$Update = False
EndIf
;INI
$SettingsFile = StringTrimRight(@ScriptName, 4) & ".ini"
$SettingsFileFullPath = @ScriptDir & "\" & $SettingsFile
While 1
$LoopProgram = IniRead($SettingsFileFullPath, "Settings", "LoopProgram", "0")
Global $LogLevel = IniRead($SettingsFileFullPath, "Settings", "LogLevel", "1")
While 1
$UserFoldersPath = IniRead($SettingsFileFullPath, "Settings", "UsersFolderPath", "notset")
If $UserFoldersPath <> "notset" AND FileExists($UserFoldersPath) Then
If StringRight($UserFoldersPath, 1) = "\" AND StringRight($UserFoldersPath, 2) <> ":\" Then
$UserFoldersPath = StringTrimRight($UserFoldersPath, 1)
EndIf
ExitLoop
Else
$Msg = MsgBox(1, $Title, "Users folder path not set or invalid (" & $UserFoldersPath & "), click ok to select a folder to use", 10)
If $Msg <> 1 Then Exit
$NewFolderPath = FileSelectFolder("Select a folder for users", "", 0, "")
If @error Then Exit
If FileExists($NewFolderPath) Then
IniWrite($SettingsFileFullPath, "Settings", "UsersFolderPath", $NewFolderPath)
MsgBox(0, $Title, "Folder path saved")
Endif
EndIf
Wend
$ExcludeUsers = IniRead($SettingsFileFullPath, "Settings", "ExcludeUsers", "notset")
If $ExcludeUsers = "notset" Then IniWrite($SettingsFileFullPath, "Settings", "ExcludeUsers", "")
_ConsoleWrite("User exluded: " & $ExcludeUsers)
$DefaultExcludeCharacters = "~!@#$%^&*()_+-$1234567890"
$ExcludeCharacters = IniRead($SettingsFileFullPath, "Settings", "ExcludeUsersWithCharacters", $DefaultExcludeCharacters)
If $ExcludeCharacters = $DefaultExcludeCharacters Then IniWrite($SettingsFileFullPath, "Settings", "ExcludeUsersWithCharacters", $DefaultExcludeCharacters)
_ConsoleWrite("Characters exluded: " & $ExcludeCharacters)
$aExcludeCharacters = StringSplit($ExcludeCharacters,"")
$PSCommand = 'Get-ADGroupMember \"Domain users\" -recursive | Get-ADUser | Where { $_.Enabled -eq $True} | Select SamAccountName | Format-Table -HideTableHeaders'
$PSCommand = 'powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -command "&{' & $PSCommand & '}"'
_ConsoleWrite($PSCommand)
$iPid = Run(@ComSpec & ' /c ' & $PSCommand, "", @SW_MINIMIZE, $STDERR_MERGED)
Local $sData
While 1
$sData &= StdoutRead($iPid)
If @error Then ExitLoop
Wend
;Create array from query output
$aData = StringSplit($sData, @CRLF, $STR_ENTIRESPLIT)
_ConsoleWrite("Raw Output:" & @CRLF & $sData, 3)
;Loop array of users
_ConsoleWrite("Loop array", 2)
For $i=2 to $aData[0]-1
$User = $aData[$i]
_ConsoleWrite($User & " - $i=" & $i & " $aData[0]=" & $aData[0], 2)
;Trim raw lines that contain trailing and leading spaces
$User = StringStripWS($User, $STR_STRIPLEADING+$STR_STRIPTRAILING)
;Skip empty
If $User = "" Then ContinueLoop
;Skip if HealthMailbox
If StringInStr($User, "HealthMailbox") Then ContinueLoop
;Skip if special characters
For $r=1 to $aExcludeCharacters[0]
If StringInStr($User, $aExcludeCharacters[$r]) Then
_ConsoleWrite($User & " - skipped because special character", 2)
ContinueLoop 2
endif
Next
;Skip users that exist in skip list
If StringInStr($ExcludeUsers, $User) Then
_ConsoleWrite($User & " - skipped because exclude list", 2)
ContinueLoop
EndIf
;Full path of the users folder to check
$UserFolder = $UserFoldersPath & "\" & $User
; If user folder doesnt exit, create it and set permisions
If NOT FileExists ($UserFolder) Then
_ConsoleWrite($User & " - user folder doesn't exist, creating folder")
If $Update Then
DirCreate($UserFolder)
_ConsoleWrite($User & " - folder created, return: "&@error)
EndIf
Else
_ConsoleWrite($User & " - folder exists")
Endif
If $Update Then
;Set permisions to write (not full)
$Command = 'iCACLS "'&$UserFolder&'" /T /C /Grant "'&$User&'":(OI)(CI)M'
_ConsoleWrite($User & " - raw command: " & $Command, 2)
RunWait(@ComSpec & ' /c ' & $Command,'',@SW_HIDE)
Endif
; If NestedFolder1 is set and does not exist, create folder
$NestedFolder1 = IniRead($SettingsFileFullPath, "Settings", "NestedFolder1", "")
$NestedFolder1FullPath = $UserFolder & "\" & $NestedFolder1
If $NestedFolder1 Then
_ConsoleWrite($User & " - $NestedFolder1="&$NestedFolder1, 2)
If Not FileExists ($NestedFolder1FullPath) Then
_ConsoleWrite($User & " - nested folder doesn't exist, creating folder", 2)
If $Update Then DirCreate($NestedFolder1FullPath)
_ConsoleWrite($User & " - folder created, return: "&@error, 2)
Else
_ConsoleWrite($User & " - nested folder exists", 2)
EndIf
Else
_ConsoleWrite($User & " - nested folder not set", 2)
EndIf
; If NestedFolderUser1 is set and folder exists, update acl
$NestedFolderUser1 = IniRead($SettingsFileFullPath, "Settings", "NestedFolderUser1", "")
If $NestedFolderUser1 Then
_ConsoleWrite($User & " - $NestedFolderUser1="&$NestedFolderUser1, 2)
If FileExists ($NestedFolder1FullPath) Then
_ConsoleWrite($User & " - nested folder exists, setting acl", 2)
$Command = ' iCACLS "'&$NestedFolder1FullPath&'" /Grant "'&$NestedFolderUser1&'":W /T /C'
_ConsoleWrite($User & " - raw command: "&$Command, 2)
If $Update Then RunWait(@ComSpec & ' /c ' & $Command,'',@SW_HIDE)
Else
_ConsoleWrite($User & " - nested folder does not exists", 2)
EndIf
Else
_ConsoleWrite($User & " - NestedFolderUser1 not set", 2)
EndIf
Next
If $LoopProgram = 0 Then
ExitLoop
Else
Sleep($LoopProgram * 1000)
Endif
Wend
_ConsoleWrite("End")
;=================================================================================================
;Common Functions
;=================================================================================================
Func _ConsoleWrite($sMessage, $iLevel=1, $iSameLine=0)
Local $hHandle, $sData
if Eval("LogFilePath")="" Then Global $LogFilePath = StringTrimRight(@ScriptFullPath,4)&"_Log.txt"
if Eval("LogFileMaxSize")="" Then Global $LogFileMaxSize = 0
if Eval("LogToFile")="" Then Global $LogToFile = False
if StringInStr($CmdLineRaw, "-debuglog") Then Global $LogToFile = True
if Eval("LogLevel")="" Then Global $LogLevel = 3 ; The level of message to log - If no level set to 3
If $sMessage=="OPENLOG" Then Return ShellExecute($LogFilePath)
If $iLevel<=$LogLevel then
$sMessage=StringReplace($sMessage,@CRLF&@CRLF,@CRLF) ;Remove Double CR
If StringRight($sMessage,StringLen(@CRLF))=@CRLF Then $sMessage=StringTrimRight($sMessage,StringLen(@CRLF)) ; Remove last CR
Local $sTime=@YEAR&"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&"> " ; Generate Timestamp
$sMessage=StringReplace($sMessage,@CRLF,@CRLF&_StringRepeat(" ",StringLen($sTime))) ; Uses spaces after initial line if string had returns
if NOT $iSameLine then $sMessage=@CRLF&$sTime&$sMessage
ConsoleWrite($sMessage)
If $LogToFile Then
if $LogFileMaxSize<>0 AND FileGetSize($LogFilePath) > $LogFileMaxSize*1024 then
$sMessage=FileRead($LogFilePath) & $sMessage
$sMessage=StringTrimLeft($sMessage,StringInStr($sMessage, @CRLF, 0, 5))
$hHandle=FileOpen($LogFilePath,2)
Else
$hHandle=FileOpen($LogFilePath,1)
endif
FileWrite($hHandle,$sMessage)
FileClose($hHandle)
endif
endif
Return $sMessage
EndFunc ;==> _ConsoleWrite