· 9 years ago · Oct 05, 2016, 12:52 PM
1$path = "\\adminscript\Scripts\Logs\psLog\NewUsers_" + (Get-Date).ToString("yyyyMMddhhmmss") + ".txt"
2Set-Location C:\Windows\System32
3Start-Transcript -Path $path -NoClobber
4
5Add-PSSnapin SqlServerCmdletSnapin100
6Add-PSSnapin SqlServerProviderSnapin100
7Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
8import-module ActiveDirectory
9
10#$notify = "amador.garcia@uthct.edu"
11$notify = "scripting@uthct.edu"
12Copy-Item "\\adminscript\scripts\Newusers\users.csv" ("\\adminscript\scripts\Logs\csvLog\NewUsers.csv").Replace(".csv", "_" + (Get-Date).ToString("yyyyMMddhhmmss") + ".csv")
13$import = "\\adminscript\scripts\Newusers\users.csv"
14
15Import-CSV $import | ForEach {
16
17 echo "Import CSV"
18 $userid = $_.first_name.substring(0,1).ToLower()+$_.last_name.substring(0,1).ToLower()+$_.emplid
19 $empid = $_.emplid
20 $emptype = $_.employeetype
21 $empname = $_.last_name + ", " + $_.first_name
22 $first = $_.first_name
23 $last = $_.last_name
24 $middle = $_.middle_name
25 $badge = $_.badge_nbr
26 $jobcode = $_.jobcode
27 $jobtitle = $_.jobtitle
28 $deptid= $_.deptid
29 $deptname = $_.departmentname
30 $managerid = $_.manager_id
31 $domain = "uthct"
32 $logFile = "\\adminscript\Scripts\Logs\userLog\$userid.txt"
33 $homedir = "\\uth-md-fil-03\main"
34 $chars = "123456789".ToCharArray()
35 $Password="Uthct!"
36 1..4 | ForEach { $Password += $chars | Get-Random }
37 echo $Password
38
39 $null = repadmin /syncall infp-dc-01.uthct.edu
40 $null = repadmin /syncall dc02.uthct.edu
41 $null = repadmin /syncall dc03.uthct.edu
42 #Start-Sleep -s 15
43
44
45 if (dsquery user -samid $userid)
46 {
47 echo "User already exists. Not proceeding with AD account creation."
48 $logResult = (Get-Date).ToString() + " - AD account for $userid already existed" | Out-File $logFile -Append -Force
49 }
50 else
51 {
52 echo "verifying email address"
53 $emailHR = $_.email_addr.Substring(0, $_.email_addr.IndexOf("@"))
54 $email = "$emailHR@uthct.edu"
55 [int16] $inc = 1
56 while ((Get-Recipient ($email) -ErrorAction SilentlyContinue) -and $inc -lt 10)
57 {
58 $inc = $inc + 1
59 $email = "$emailHR$inc@uthct.edu"
60 echo $email
61 }
62 echo "using $email for new email address"
63
64 echo "verifying CN name"
65 $nameCN = $empname
66 $userCN= Get-Aduser -Filter {Name -eq $nameCN}
67 $nameCN = if($userCN.name){("$nameCN $middle").Trim()}else{$nameCN}
68 $userCN= Get-Aduser -Filter {Name -eq $nameCN}
69 $isAvailable = if($userCN.name){"False"}else{"True"}
70 $inc = 1
71 While ($isAvailable -ne "True" -and $inc -lt 10)
72 {
73 $inc = $inc + 1
74 $nameCN = "$nameCN $inc"
75 $userCN= Get-Aduser -Filter {Name -eq $nameCN}
76 $isAvailable= if($userCN.name){"False"}else{"True"}
77 }
78 echo "using $nameCN for CN name"
79
80
81 echo "User does not exists. Creating AD account."
82 $user = New-ADuser `
83 -SamAccountName ($userid) `
84 -Description ("ADSync 2.0 auto created on " + (Get-Date -Format g).ToString()) `
85 -Name ($nameCN) `
86 -Displayname ($nameCN) `
87 -UserPrincipalName ("$userid@$domain.edu") `
88 -Surname ($last) `
89 -GivenName ($first) `
90 -EmailAddress ($email) `
91 -title ($jobtitle) `
92 -department ($deptname) `
93 -Homedirectory ("$homedir\$userid") `
94 -Homedrive ("Z:") `
95 -employeeid ($empid) `
96 -otherattributes @{employeetype=$emptype} `
97 -AccountPassword (ConvertTo-SecureString -AsPlainText $password -force ) `
98 -Enabled $true `
99 -PasswordNeverExpires $false `
100 -ChangePasswordAtLogon $true `
101 -PassThru
102
103 $null = repadmin /syncall infp-dc-01.uthct.edu
104 $null = repadmin /syncall dc02.uthct.edu
105 $null = repadmin /syncall dc03.uthct.edu
106 Start-Sleep -s 30
107
108 if (dsquery user -samid $userid)
109 {
110 echo "log successful AD accoiunt creation for $userid"
111 $logResult= (Get-Date).ToString() + ' - Successfully created AD account for ' + $userid | Out-File $logFile -Append -Force
112
113 echo "setting manager for $userid"
114 if (dsquery user -samid $managerid)
115 {
116 $adUser = Get-ADUser -identity $managerid -Properties DistinguishedName
117 $adValue = $adUser.DistinguishedName
118 if ($adValue -ne "")
119 {
120 Get-ADUser -identity $userid | Set-Aduser -Replace @{manager=$adValue}
121
122 $subject = "Setting $userid Manager to $adValue"
123 echo $subject
124 $logResult = (Get-Date).ToString() + " - "+ $subject | Out-File $logFile -Append -Force
125 }
126 }
127
128 echo "adding $userid to security groups"
129 Add-ADGroupMember -identity "Dl-eu" -Members $user.SamAccountName
130 if($emptype -eq 'E')
131 {
132 Add-ADGroupMember -identity "efmDomainusers" -Members $user.SamAccountName
133 }
134
135 echo "Sending Email for $userid"
136 $subject = "The University of Texas Health Science Center at Tyler"
137 $body = "
138 Welcome, <B>$first $last</B> to the University of Texas Health Science Center at Tyler. Your employee number, user ID, network password, and email address have been established and are as follows:<br>
139 <br>
140 <li> Employee Number: $empid<br>
141 <li> UserID: $userid<br>
142 <li> Password: $password<br>
143 <li> Email Address: $email<br>
144 <li> Badge Number: $badge<br>
145 <p> You will be required to change your password the first time you log onto the computer systems. Your password must be a minimum of
146 8 total characters long and contain at least 1 upper case character, 1 lower case character, 1 special character, and 1 number. Your password is case sensitive.<br>
147 <p> If you have any questions concerning the use of our network, email, or other computer-related resources, please do not hesitate to contact the IT Help Desk at ext 7700. Your badge number and password should remain confidential and not shared with anyone.<br>
148 </P>
149 "
150 Send-MailMessage -smtpserver utmail-smtp-01 -from Feedback@uthct.edu -to 'newhires@uthct.edu' -Subject $subject -body $body -bodyashtml
151
152 echo "PS Stat table update for $empid"
153 $sqlPS = "UPDATE PS_UT_AD_STAT_TBL set PROCESSED = 'Y' where EMPLID = $empid"
154 invoke-sqlcmd -ServerInstance "uth-md-pssql-01\peoplesoft" -database "PYPROD" -query $sqlPS
155 Set-Location C:\Windows\System32
156
157 $sqlV1 = "SELECT PROCESSED FROM [UTH-MD-PSSQL-01\PEOPLESOFT].PYPROD.dbo.PS_UT_AD_STAT_TBL WHERE EMPLID = $empid"
158 $sqlR1 = Invoke-sqlcmd -serverinstance "infpsql05" -database "EEDataWarehouse" -query $sqlV1
159 Set-Location C:\Windows\System32
160 if ($sqlR1.processed -eq "Y")
161 {
162 $subject = "Successfully updated PS stat table for $empid"
163 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
164 }
165 else
166 {
167 $subject = "Failed to update PS stat table for $empid"
168 Send-MailMessage -SmtpServer utmail-smtp-01 -from Feedback@uthct.edu -to $notify -Subject $subject
169 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
170 }
171
172 echo "Account info table insert for $userid"
173 $sqlActInfo = "INSERT INTO dbo.AD_ACCOUNT_INFORMATION
174 (UNIQUE_ID, ID_TYPE, USERNAME, EMAIL_ADDRESS, FIRST_NAME, LAST_NAME, NAME, DEPTID, DEPTNAME, JOBCODE, JOBTITLE, PREF_FIRST_NAME)
175 VALUES ('$empid', '$emptype', '$userid', '$email', '$first', '$last','$last, $first', '$deptID', '$deptname', '$jobcode', '$jobtitle', '$first')"
176 Invoke-sqlcmd -serverinstance "infpsql05" -database "EEDataWarehouse" -query $sqlActInfo
177 Set-Location C:\Windows\System32
178
179 $sqlV2 = "SELECT RTRIM(LTRIM(UNIQUE_ID)) AS empid FROM dbo.AD_ACCOUNT_INFORMATION WHERE UNIQUE_ID = $empid"
180 $sqlR2 = Invoke-sqlcmd -serverinstance "infpsql05" -database "EEDataWarehouse" -query $sqlV2
181 Set-Location C:\Windows\System32
182 if ($sqlR2.empid -eq $empid)
183 {
184 $subject = "Successfully inserted into account info table for $empid"
185 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
186 }
187 else
188 {
189 $subject = "Failed to insert into account info for $empid"
190 Send-MailMessage -SmtpServer utmail-smtp-01 -from Feedback@uthct.edu -to $notify -Subject $subject
191 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
192 }
193
194 echo "email creation for $userid as $email"
195 Enable-Mailbox -identity ("$domain\$userid") -alias $userid -database "utmail-mbx1\First Mailbox Database" -PrimarySmtpAddress $email | out-file \\adminscript\Scripts\Logs\MailboxLog.txt -Append -Force
196 $null = repadmin /syncall infp-dc-01.uthct.edu
197 $null = repadmin /syncall dc02.uthct.edu
198 $null = repadmin /syncall dc03.uthct.edu
199 Start-Sleep -s 30
200 if((Get-Recipient ($email) -ErrorAction SilentlyContinue))
201 {
202 $subject = "Successfully enable email for $email"
203 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
204 }
205 else
206 {
207 $subject = "Failed to enable email for $email"
208 Send-MailMessage -SmtpServer utmail-smtp-01 -from Feedback@uthct.edu -to $notify -Subject $subject
209 $logResult= (Get-Date).ToString() + ' - ' + $subject | Out-File $logFile -Append -Force
210 }
211 }
212 else
213 {
214 $subject = "Failed to create AD UserID: $userid"
215 Send-MailMessage -SmtpServer utmail-smtp-01 -from Feedback@uthct.edu -to 'scripting@uthct.edu' -Subject $subject
216 $logResult= (Get-Date).ToString() + ' - '+ $subject | Out-File $logFile -Append -Force
217 }
218
219 }
220
221}
222
223
224echo "update people & places directory"
225 $directoryUrl = "http://infp-lamp-01/directory/builder2/build_directory.php"
226 $directoryLog = "\\adminscript\scripts\Logs\directoryLog\update_" + (Get-Date).ToString("yyyyMMddhhmmss") + ".html"
227 $wc=new-object system.net.webclient
228 $wc.UseDefaultCredentials = $true
229 $wc.downloadfile($directoryUrl, $directoryLog)