Advertisement
Guest User

MyGui

a guest
Aug 15th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.83 KB | None | 0 0
  1. ##########Extra's:#########
  2. $Version = “Version 2.0”
  3. $WriteMenu = “`n`tPCI – AD Tasks Menu – $($Version)`t`n”
  4. $runasAlias = [Environment]::UserName
  5.  
  6.  
  7. function Get-User ([string]$uid) {
  8. try { $aduser = get-aduser -identity $uid } catch { $aduser = $null }
  9. return $aduser
  10. }
  11.  
  12. function Get-ADGroupAudit ($dn) {
  13. $ttl = ([string](get-adgroup -identity $dn -property info).info).split(";")
  14. [regex]$pattern = "^(?<State>\w+)\s+member(?:\s(?<DateTime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s+(?:.*\\)?\s+)"
  15. foreach ($row in (repadmin /showobjmeta $env:logonserver.substring(2) $dn | select-string "member" -context 0,2)) {
  16. $user = $(try { (get-aduser -identity $row.context.postcontext[1]).samaccountname } catch {})
  17. if (($row.line -match $pattern) -and ($user -ne $null)) {
  18. [pscustomobject]@{
  19. UID = $user
  20. Age = if($matches.datetime){[math]::round((new-timespan -start $([datetime]$matches.datetime)).totalhours,2)}else{$null}
  21. TTL = 12 + [int]($ttl -contains $user)*12
  22. IsActive = ($matches.state -eq "PRESENT")
  23. LastModified = if($matches.datetime){[datetime]$matches.datetime}else{$null}
  24. }
  25. }
  26. }
  27. }
  28.  
  29. Add-Type -AssemblyName System.Windows.Forms
  30. Add-Type -AssemblyName System.Drawing
  31.  
  32. Function GenerateForm {
  33.  
  34. #Build Form
  35. $Form = New-Object System.Windows.Forms.Form
  36. $Form.Text = "$($WriteMenu)"
  37. $Form.AutoScroll = $true
  38. $Form.Size = New-Object System.Drawing.Size(640,750)
  39. $Form.SizeGripStyle = "Hide"
  40. $Form.StartPosition = "CenterScreen"
  41. #$Form.TopMost = $true
  42.  
  43. #Build Messagebox1
  44. $MessageBox1 = New-Object System.Windows.Forms.TextBox
  45. $MessageBox1.Location = New-Object System.Drawing.Point(25,475)
  46. $MessageBox1.Width = 575
  47. $MessageBox1.Height = 200
  48. $MessageBox1.Multiline = $true
  49. $MessageBox1.ReadOnly = $true
  50. $MessageBox1.ScrollBars = 'Vertical'
  51. $MessageBox1.BackColor = "White"
  52. $MessageBox1.Font = New-object System.Drawing.Font("lucida console",11)
  53.  
  54.  
  55. #Build Mainbox1
  56. $Mainbox1 = New-Object System.Windows.Forms.Groupbox
  57. $Mainbox1.height = 350
  58. $Mainbox1.Width = 300
  59. $Mainbox1.Location = New-Object System.Drawing.Point(25,25)
  60.  
  61. #Build Label
  62. $Label = New-Object System.Windows.Forms.Label
  63. $Label.Text = "Running as : $($runasAlias)"
  64. $Label.Location = New-object System.Drawing.Size(25,25)
  65. $Label.AutoSize = $true
  66.  
  67. #Build Button1 #Give users access to PROD
  68. $Button1 = New-Object System.Windows.Forms.Button
  69. $Button1.Location = New-Object System.Drawing.Point(25,55)
  70. $Button1.Size = New-Object System.Drawing.Size(250,30)
  71. $Button1.Text = "Give users access to PROD"
  72. $Button1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  73.  
  74. #Build Button3 #Remove users from PROD
  75. $Button3 = New-Object System.Windows.Forms.Button
  76. $Button3.Location = New-Object System.Drawing.Point(25,95)
  77. $Button3.Size = New-Object System.Drawing.Size(250,30)
  78. $Button3.Text = "Remove users from PROD"
  79. $Button3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  80.  
  81. #Build Button4 #Check users allowed
  82. $Button4 = New-Object System.Windows.Forms.Button
  83. $Button4.Location = New-Object System.Drawing.Point(25,135)
  84. $Button4.Size = New-Object System.Drawing.Size(250,30)
  85. $Button4.Text = "Check users allowed to PROD"
  86. $Button4.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  87.  
  88. #Build Button5 #Extend users access to PROD
  89. $Button5 = New-Object System.Windows.Forms.Button
  90. $Button5.Location = New-Object System.Drawing.Point(25,175)
  91. $Button5.Size = New-Object System.Drawing.Size(250,30)
  92. $Button5.Text = "Extend users access to PROD"
  93. $Button5.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  94.  
  95. #Build Button6 #Reset a Prod Password
  96. $Button6 = New-Object System.Windows.Forms.Button
  97. $Button6.Location = New-Object System.Drawing.Point(25,215)
  98. $Button6.Size = New-Object System.Drawing.Size(250,30)
  99. $Button6.Text = "Reset a PROD Password"
  100. $Button6.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  101.  
  102. #Build Mainbox2
  103. $Mainbox2 = New-Object System.Windows.Forms.Groupbox
  104. $Mainbox2.height = 350
  105. $Mainbox2.Width = 300
  106. $Mainbox2.Location = New-Object System.Drawing.Point(300,25)
  107.  
  108. #Build RadioButton1
  109. $RadioButton1 = New-Object System.Windows.Forms.RadioButton
  110. $RadioButton1.AutoSize = $true
  111. $RadioButton1.location = New-Object System.Drawing.point(45,55)
  112. $RadioButton1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  113. $RadioButton1.Visible = $false
  114. $RadioButton1.Checked = $false
  115.  
  116.  
  117. #Build RadioButton2
  118. $RadioButton2 = New-Object System.Windows.Forms.RadioButton
  119. $RadioButton2.AutoSize = $true
  120. $RadioButton2.location = New-Object System.Drawing.point(45,85)
  121. $RadioButton2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  122. $RadioButton2.Visible = $false
  123. $RadioButton2.Checked = $false
  124.  
  125. #Build RadioButton3
  126. $RadioButton3 = New-Object System.Windows.Forms.RadioButton
  127. $RadioButton3.AutoSize = $true
  128. $RadioButton3.location = New-Object System.Drawing.point(45,115)
  129. $RadioButton3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  130. $RadioButton3.Visible = $false
  131. $RadioButton3.Checked = $false
  132.  
  133. #Build RadioButton4
  134. $RadioButton4 = New-Object System.Windows.Forms.RadioButton
  135. $RadioButton4.AutoSize = $true
  136. $RadioButton4.location = New-Object System.Drawing.point(45,145)
  137. $RadioButton4.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  138. $RadioButton4.Visible = $false
  139. $RadioButton4.Checked = $false
  140.  
  141. #Add TextBox1
  142. $TextBox1 = New-Object System.Windows.Forms.Textbox
  143. $TextBox1.Multiline = $false
  144. $TextBox1.Width = 90
  145. $TextBox1.Height = 20
  146. $TextBox1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  147. $TextBox1.Visible = $false
  148.  
  149. #Add TextBox2
  150. $TextBox2 = New-Object System.Windows.Forms.Textbox
  151. $TextBox2.Multiline = $false
  152. $TextBox2.Width = 90
  153. $TextBox2.Height = 20
  154. $TextBox2.Location = New-Object System.Drawing.Point(135,95)
  155. $TextBox2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  156. $TextBox2.Visible = $false
  157.  
  158. #Build Label2
  159. $Label2 = New-Object System.Windows.Forms.Label
  160. $Label2.Text = "UserId : "
  161. $Label2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  162. $Label2.AutoSize = $true
  163. $Label2.Visible = $false
  164.  
  165. #Build Label3
  166. $Label3 = New-Object System.Windows.Forms.Label
  167. $Label3.Text = "Password : "
  168. $Label3.Location = New-object System.Drawing.Size(45,95)
  169. $Label3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  170. $Label3.AutoSize = $true
  171. $Label3.Visible = $false
  172.  
  173. #Build Button2
  174. $Button2 = New-Object System.Windows.Forms.Button
  175. $Button2.Location = New-Object System.Drawing.Size(45,280)
  176. $Button2.Autosize = $true
  177. $Button2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
  178. $Button2.Visible = $false
  179.  
  180. #Build Mainbox3
  181. $Mainbox3 = New-Object System.Windows.Forms.Groupbox
  182. $Mainbox3.height = 100
  183. $Mainbox3.Width = 575
  184. $Mainbox3.Location = New-Object System.Drawing.Point(25,371)
  185.  
  186.  
  187. $Form.controls.AddRange(@($Mainbox1,$Mainbox2,$Mainbox3,$MessageBox1))
  188. $Mainbox1.Controls.AddRange(@($Label,$Button1,$Button3,$Button4,$Button5,$Button6))
  189. $Mainbox2.Controls.AddRange(@($RadioButton1,$RadioButton2,$RadioButton3,$RadioButton4,$TextBox1,$TextBox2,$Label2,$Label3,$Button2))
  190.  
  191.  
  192. #Add Button event
  193. $Button1.Add_Click({Button1_Click})
  194. $Button3.Add_Click({Button3_Click})
  195. $Button4.Add_Click({Button4_Click})
  196. $Button5.Add_Click({Button5_Click})
  197. $Button6.Add_Click({Button6_Click})
  198.  
  199. #Show the Form
  200. $Form.ShowDialog() | Out-Null
  201.  
  202.  
  203. ############################################
  204.  
  205. ##### Give users access to PROD #####
  206.  
  207. Function Button1_Click(){
  208.  
  209. $RadioButton1.Visible = $true
  210. $RadioButton1.Text = "Give access to ECS"
  211. $RadioButton2.Visible = $true
  212. $RadioButton2.Text = "Give access to TOS"
  213. $RadioButton3.Visible = $false
  214. $RadioButton4.Visible = $false
  215. $TextBox1.Visible = $true
  216. $TextBox1.Location = New-Object System.Drawing.Point(135,240)
  217. $Label2.Visible = $true
  218. $Label2.Location = New-object System.Drawing.Size(45,240)
  219. $Button2.Visible = $true
  220. $Button2.Text = "Enter"
  221. $Label3.Visible = $false
  222. $TextBox2.Visible = $false
  223. $Button2.Add_Click({AddUser})
  224. }
  225.  
  226. ##### Remove users from PROD #####
  227.  
  228. Function Button3_Click() {
  229. $RadioButton1.Visible = $true
  230. $RadioButton1.Text = "Remove a user from ECS"
  231. $RadioButton2.Visible = $true
  232. $RadioButton2.Text = "Remove a user from TOS"
  233. $RadioButton3.Visible = $true
  234. $RadioButton3.Text = "Remove all users from ECS"
  235. $RadioButton4.Visible = $true
  236. $RadioButton4.Text = "Remove all users from TOS"
  237. $TextBox1.Visible = $true
  238. $TextBox1.Location = New-Object System.Drawing.Point(135,240)
  239. $Label2.Visible = $true
  240. $Label2.Location = New-object System.Drawing.Size(45,240)
  241. $Button2.Visible = $true
  242. $Button2.Text = "Enter"
  243. $Label3.Visible = $false
  244. $TextBox2.Visible = $false
  245. $Button2.Add_Click({Remove-User})
  246. }
  247.  
  248. ##### Check users allowed to PROD #####
  249.  
  250. FUnction Button4_Click(){
  251. $RadioButton1.Visible = $true
  252. $RadioButton1.Text = "Check users allowed to ECS"
  253. $RadioButton2.Visible = $true
  254. $RadioButton2.Text = "Check users allowed to TOS"
  255. $RadioButton3.Visible = $false
  256. $RadioButton4.Visible = $false
  257. $TextBox1.Visible = $false
  258. $TextBox1.Location = New-Object System.Drawing.Point(135,240)
  259. $Label2.Visible = $false
  260. $Label2.Location = New-object System.Drawing.Size(45,240)
  261. $Button2.Visible = $true
  262. $Button2.Text = "Check"
  263. $Label3.Visible = $false
  264. $TextBox2.Visible = $false
  265. $Button2.Add_Click({CheckUser})
  266. }
  267.  
  268. ##### Extend users to PROD #####
  269.  
  270. Function Button5_Click(){
  271. $RadioButton1.Visible = $true
  272. $RadioButton1.Text = "Extend user for ECS"
  273. $RadioButton2.Visible = $true
  274. $RadioButton2.Text = "Extend user for TOS"
  275. $RadioButton3.Visible = $false
  276. $RadioButton4.Visible = $false
  277. $TextBox1.Visible = $true
  278. $TextBox1.Location = New-Object System.Drawing.Point(135,240)
  279. $Label2.Visible = $true
  280. $Label2.Location = New-object System.Drawing.Size(45,240)
  281. $Button2.Visible = $true
  282. $Button2.Text = "Enter"
  283. $Label3.Visible = $false
  284. $TextBox2.Visible = $false
  285. $Button2.Add_Click({ExtendUser})
  286. }
  287.  
  288. ##### Reset a PROD Password #####
  289.  
  290. Function Button6_Click(){
  291. $RadioButton1.Visible = $false
  292. $RadioButton2.Visible = $false
  293. $RadioButton3.Visible = $false
  294. $RadioButton4.Visible = $false
  295. $Label2.Visible = $true
  296. $Label2.Location = New-object System.Drawing.Size(45,55)
  297. $TextBox1.Visible = $true
  298. $TextBox1.Location = New-Object System.Drawing.Point(135,55)
  299. $Label3.Visible = $true
  300. $TextBox2.Visible = $true
  301. $Button2.Visible = $true
  302. $Button2.Text = "Reset Password"
  303. $Button2.Add_Click({ResetPassword})
  304. }
  305.  
  306.  
  307.  
  308. Function AddUser() {
  309.  
  310. if ($RadioButton1.Checked){
  311. $secgroup = "SQL_P1_ADMINS_ECS"
  312. $user = Get-User $($TextBox1.Text).toupper()
  313. if ($user) {
  314. add-adgroupmember -identity $secgroup -members $user
  315. $MessageBox1.text = "User $($TextBox1.Text) added to $secgroup"
  316. }
  317. else { $MessageBox1.text = "User ID $($TextBox1.Text) not found in Active Directory"
  318. }
  319. }
  320. elseif ($RadioButton2.Checked){
  321. $secgroup = "SQL_P1_ADMINS_TOS"
  322. $user = Get-User $($TextBox1.Text).toupper()
  323. if ($user) {
  324. add-adgroupmember -identity $secgroup -members $user
  325. $MessageBox1.text = "User $($TextBox1.Text) added to $secgroup"
  326. }
  327. else{ $MessageBox1.text = "User ID $($TextBox1.Text) not found in Active Directory"
  328. }
  329. }
  330. }
  331.  
  332.  
  333. Function Remove-User() {
  334. if ($RadioButton1.Checked){
  335. $secgroup = "SQL_P1_ADMINS_ECS"
  336. $user = Get-User $($TextBox1.Text).toupper()
  337. if ($user) {
  338. $users = get-adgroupmember -identity $secgroup
  339. if ($users.samaccountname -contains $user.samaccountname) {
  340. remove-adgroupmember -identity $secgroup -members $user -confirm:$false
  341. # REMOVE USER FROM TTL LIST
  342. #$ttl = (([string](get-adgroup -identity $secgroup -property info).info).split(";") | ? { ($_ -ne $user.samaccountname) }) -join ";"
  343. #if ($ttl) { set-adgroup -identity $secgroup -replace @{info=$ttl} }
  344. #else { set-adgroup -identity $secgroup -clear info }
  345. $MessageBox1.text = "User $($TextBox1.Text) has been removed from $secgroup"
  346. }
  347. else{ $MessageBox1.Text = "User $($TextBox1.Text) is not a member of $secgroup"
  348. }
  349. }
  350. }
  351. elseif ($RadioButton2.Checked){
  352. $secgroup = "SQL_P1_ADMINS_TOS"
  353. $user = Get-User $($TextBox1.Text).toupper()
  354. if ($user) {
  355. $users = get-adgroupmember -identity $secgroup
  356. if ($users.samaccountname -contains $user.samaccountname) {
  357. remove-adgroupmember -identity $secgroup -members $user -confirm:$false
  358. # REMOVE USER FROM TTL LIST
  359. #$ttl = (([string](get-adgroup -identity $secgroup -property info).info).split(";") | ? { ($_ -ne $user.samaccountname) }) -join ";"
  360. #if ($ttl) { set-adgroup -identity $secgroup -replace @{info=$ttl} }
  361. #else { set-adgroup -identity $secgroup -clear info }
  362. $MessageBox1.text = "User $($TextBox1.Text) has been removed from $secgroup"
  363. }
  364. else{ $MessageBox1.Text = "User $($TextBox1.Text) is not a member of $secgroup"
  365. }
  366. }
  367. }
  368.  
  369. elseif ($RadioButton3.Checked){
  370. $secgroup = "SQL_P1_ADMINS_ECS"
  371. $Groups = get-adgroupmember $secgroup
  372. if ($Groups) {
  373. remove-adgroupmember -identity $secgroup -members ($Groups) -confirm:$false
  374. #set-adgroup -identity $secgroup -clear info
  375. $MessageBox1.text = "All users has been removed from $secgroup"
  376. }
  377. else{ $MessageBox1.text = "$($secgroup) is already empty"
  378. }
  379. }
  380.  
  381.  
  382. elseif ($RadioButton4.Checked){
  383. $secgroup = "SQL_P1_ADMINS_TOS"
  384. $Groups = get-adgroupmember $secgroup
  385. if ($Groups) {
  386. remove-adgroupmember -identity $secgroup -members ($Groups) -confirm:$false
  387. #set-adgroup -identity $secgroup -clear info
  388. $MessageBox1.text = "All users has been removed from $secgroup"
  389. }
  390. else{ $MessageBox1.text = "$($secgroup) is already empty"
  391. }
  392. }
  393. }
  394.  
  395. Function CheckUser {
  396. if ($RadioButton1.Checked){
  397. $secgroup = "SQL_P1_ADMINS_ECS"
  398. $group = get-adgroup -Identity $($secgroup)
  399. $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $group) | ? {$_.isactive})) {
  400. [pscustomobject]@{
  401. 'UID' = $member.uid
  402. 'Active since' = $member.lastmodified
  403. TTL = $member.ttl
  404. 'Hours remaining' = $member.ttl - $member.age
  405. }
  406. }
  407. $MessageBox1.Text = $list | Format-Table -Autosize | Out-String
  408. }
  409. elseif ($RadioButton2.Checked){
  410. $secgroup = "SQL_P1_ADMINS_TOS"
  411. $group = get-adgroup -Identity $($secgroup)
  412. $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $group) | ? {$_.isactive})) {
  413. [pscustomobject]@{
  414. 'UID' = $member.uid
  415. 'Active_since' = $member.lastmodified
  416. TTL = $member.ttl
  417. 'Hours_remaining' = $member.ttl - $member.age
  418. }
  419. }
  420. $MessageBox1.Text = $list | Format-Table -Autosize | Out-String
  421.  
  422. }
  423. }
  424.  
  425. Function ExtendUser {
  426. if ($RadioButton1.Checked){
  427. $secgroup = "SQL_P1_ADMINS_ECS"
  428. $user = Get-User $($TextBox1.Text).toupper()
  429. if ($user) {
  430. $ttl = ([string](get-adgroup -identity $secgroup -property info).info).split(";")
  431. #set-adgroup -identity $secgroup -replace @{info=($ttl+($user.samaccountname) | select -unique) -join ";"}
  432.  
  433. $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $secgroup) | ? {$_.isactive})) {
  434. [pscustomobject]@{
  435. UID = $member.uid
  436. 'Active since' = $member.lastmodified
  437. TTL = $member.ttl
  438. 'Hours remaining' = $member.ttl - $member.age
  439. }
  440. }
  441.  
  442. $MessageBox1.Text = "TTL on $($secgroup) has been extended to 24 hours for User $($TextBox1.Text): `r`n $($list | Format-Table -Autosize | Out-String)"
  443. }
  444. else {
  445.  
  446. $MessageBox1.Text = "User ID $($TextBox1.Text) is not found in Active Directory"
  447. }
  448. }
  449.  
  450. elseif ($RadioButton2.Checked){
  451. $secgroup = "SQL_P1_ADMINS_TOS"
  452. $user = Get-User $($TextBox1.Text).toupper()
  453. if ($user) {
  454. $ttl = ([string](get-adgroup -identity $secgroup -property info).info).split(";")
  455. #set-adgroup -identity $secgroup -replace @{info=($ttl+($user.samaccountname) | select -unique) -join ";"}
  456.  
  457. $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $secgroup) | ? {$_.isactive})) {
  458. [pscustomobject]@{
  459. UID = $member.uid
  460. 'Active since' = $member.lastmodified
  461. TTL = $member.ttl
  462. 'Hours remaining' = $member.ttl - $member.age
  463. }
  464. }
  465. $MessageBox1.Text = "TTL on $($secgroup) has been extended to 24 hours for User $($TextBox1.Text): `r`n $($list | Format-Table -Autosize | Out-String)"
  466.  
  467. }
  468. else {
  469.  
  470. $MessageBox1.Text = "User ID $($TextBox1.Text) is not found in Active Directory"
  471. }
  472. }
  473. }
  474.  
  475. Function ResetPassword {
  476.  
  477. $user = Get-User $($TextBox1.Text).toupper()
  478. $Password = (ConvertTo-SecureString $TextBox2.Text -AsPlainText -Force)
  479. if ($user) {
  480. Set-ADAccountPassword -Identity $($TextBox1.Text) -NewPassword $Password -Reset
  481. $MessageBox1.Text = "Password for User $($TextBox1.Text) has been changed"
  482. }
  483. else {
  484. $MessageBox1.Text = "Failed to change password for user $($TextBox1.Text)"
  485. }
  486.  
  487. }
  488.  
  489. }
  490.  
  491. #############################################
  492.  
  493.  
  494. GenerateForm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement