Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##########Extra's:#########
- $Version = “Version 2.0”
- $WriteMenu = “`n`tPCI – AD Tasks Menu – $($Version)`t`n”
- $runasAlias = [Environment]::UserName
- function Get-User ([string]$uid) {
- try { $aduser = get-aduser -identity $uid } catch { $aduser = $null }
- return $aduser
- }
- function Get-ADGroupAudit ($dn) {
- $ttl = ([string](get-adgroup -identity $dn -property info).info).split(";")
- [regex]$pattern = "^(?<State>\w+)\s+member(?:\s(?<DateTime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s+(?:.*\\)?\s+)"
- foreach ($row in (repadmin /showobjmeta $env:logonserver.substring(2) $dn | select-string "member" -context 0,2)) {
- $user = $(try { (get-aduser -identity $row.context.postcontext[1]).samaccountname } catch {})
- if (($row.line -match $pattern) -and ($user -ne $null)) {
- [pscustomobject]@{
- UID = $user
- Age = if($matches.datetime){[math]::round((new-timespan -start $([datetime]$matches.datetime)).totalhours,2)}else{$null}
- TTL = 12 + [int]($ttl -contains $user)*12
- IsActive = ($matches.state -eq "PRESENT")
- LastModified = if($matches.datetime){[datetime]$matches.datetime}else{$null}
- }
- }
- }
- }
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- Function GenerateForm {
- #Build Form
- $Form = New-Object System.Windows.Forms.Form
- $Form.Text = "$($WriteMenu)"
- $Form.AutoScroll = $true
- $Form.Size = New-Object System.Drawing.Size(640,750)
- $Form.SizeGripStyle = "Hide"
- $Form.StartPosition = "CenterScreen"
- #$Form.TopMost = $true
- #Build Messagebox1
- $MessageBox1 = New-Object System.Windows.Forms.TextBox
- $MessageBox1.Location = New-Object System.Drawing.Point(25,475)
- $MessageBox1.Width = 575
- $MessageBox1.Height = 200
- $MessageBox1.Multiline = $true
- $MessageBox1.ReadOnly = $true
- $MessageBox1.ScrollBars = 'Vertical'
- $MessageBox1.BackColor = "White"
- $MessageBox1.Font = New-object System.Drawing.Font("lucida console",11)
- #Build Mainbox1
- $Mainbox1 = New-Object System.Windows.Forms.Groupbox
- $Mainbox1.height = 350
- $Mainbox1.Width = 300
- $Mainbox1.Location = New-Object System.Drawing.Point(25,25)
- #Build Label
- $Label = New-Object System.Windows.Forms.Label
- $Label.Text = "Running as : $($runasAlias)"
- $Label.Location = New-object System.Drawing.Size(25,25)
- $Label.AutoSize = $true
- #Build Button1 #Give users access to PROD
- $Button1 = New-Object System.Windows.Forms.Button
- $Button1.Location = New-Object System.Drawing.Point(25,55)
- $Button1.Size = New-Object System.Drawing.Size(250,30)
- $Button1.Text = "Give users access to PROD"
- $Button1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- #Build Button3 #Remove users from PROD
- $Button3 = New-Object System.Windows.Forms.Button
- $Button3.Location = New-Object System.Drawing.Point(25,95)
- $Button3.Size = New-Object System.Drawing.Size(250,30)
- $Button3.Text = "Remove users from PROD"
- $Button3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- #Build Button4 #Check users allowed
- $Button4 = New-Object System.Windows.Forms.Button
- $Button4.Location = New-Object System.Drawing.Point(25,135)
- $Button4.Size = New-Object System.Drawing.Size(250,30)
- $Button4.Text = "Check users allowed to PROD"
- $Button4.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- #Build Button5 #Extend users access to PROD
- $Button5 = New-Object System.Windows.Forms.Button
- $Button5.Location = New-Object System.Drawing.Point(25,175)
- $Button5.Size = New-Object System.Drawing.Size(250,30)
- $Button5.Text = "Extend users access to PROD"
- $Button5.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- #Build Button6 #Reset a Prod Password
- $Button6 = New-Object System.Windows.Forms.Button
- $Button6.Location = New-Object System.Drawing.Point(25,215)
- $Button6.Size = New-Object System.Drawing.Size(250,30)
- $Button6.Text = "Reset a PROD Password"
- $Button6.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- #Build Mainbox2
- $Mainbox2 = New-Object System.Windows.Forms.Groupbox
- $Mainbox2.height = 350
- $Mainbox2.Width = 300
- $Mainbox2.Location = New-Object System.Drawing.Point(300,25)
- #Build RadioButton1
- $RadioButton1 = New-Object System.Windows.Forms.RadioButton
- $RadioButton1.AutoSize = $true
- $RadioButton1.location = New-Object System.Drawing.point(45,55)
- $RadioButton1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $RadioButton1.Visible = $false
- $RadioButton1.Checked = $false
- #Build RadioButton2
- $RadioButton2 = New-Object System.Windows.Forms.RadioButton
- $RadioButton2.AutoSize = $true
- $RadioButton2.location = New-Object System.Drawing.point(45,85)
- $RadioButton2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $RadioButton2.Visible = $false
- $RadioButton2.Checked = $false
- #Build RadioButton3
- $RadioButton3 = New-Object System.Windows.Forms.RadioButton
- $RadioButton3.AutoSize = $true
- $RadioButton3.location = New-Object System.Drawing.point(45,115)
- $RadioButton3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $RadioButton3.Visible = $false
- $RadioButton3.Checked = $false
- #Build RadioButton4
- $RadioButton4 = New-Object System.Windows.Forms.RadioButton
- $RadioButton4.AutoSize = $true
- $RadioButton4.location = New-Object System.Drawing.point(45,145)
- $RadioButton4.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $RadioButton4.Visible = $false
- $RadioButton4.Checked = $false
- #Add TextBox1
- $TextBox1 = New-Object System.Windows.Forms.Textbox
- $TextBox1.Multiline = $false
- $TextBox1.Width = 90
- $TextBox1.Height = 20
- $TextBox1.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $TextBox1.Visible = $false
- #Add TextBox2
- $TextBox2 = New-Object System.Windows.Forms.Textbox
- $TextBox2.Multiline = $false
- $TextBox2.Width = 90
- $TextBox2.Height = 20
- $TextBox2.Location = New-Object System.Drawing.Point(135,95)
- $TextBox2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $TextBox2.Visible = $false
- #Build Label2
- $Label2 = New-Object System.Windows.Forms.Label
- $Label2.Text = "UserId : "
- $Label2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $Label2.AutoSize = $true
- $Label2.Visible = $false
- #Build Label3
- $Label3 = New-Object System.Windows.Forms.Label
- $Label3.Text = "Password : "
- $Label3.Location = New-object System.Drawing.Size(45,95)
- $Label3.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $Label3.AutoSize = $true
- $Label3.Visible = $false
- #Build Button2
- $Button2 = New-Object System.Windows.Forms.Button
- $Button2.Location = New-Object System.Drawing.Size(45,280)
- $Button2.Autosize = $true
- $Button2.Font = New-object System.Drawing.Font("Microsoft Sans Serif",11)
- $Button2.Visible = $false
- #Build Mainbox3
- $Mainbox3 = New-Object System.Windows.Forms.Groupbox
- $Mainbox3.height = 100
- $Mainbox3.Width = 575
- $Mainbox3.Location = New-Object System.Drawing.Point(25,371)
- $Form.controls.AddRange(@($Mainbox1,$Mainbox2,$Mainbox3,$MessageBox1))
- $Mainbox1.Controls.AddRange(@($Label,$Button1,$Button3,$Button4,$Button5,$Button6))
- $Mainbox2.Controls.AddRange(@($RadioButton1,$RadioButton2,$RadioButton3,$RadioButton4,$TextBox1,$TextBox2,$Label2,$Label3,$Button2))
- #Add Button event
- $Button1.Add_Click({Button1_Click})
- $Button3.Add_Click({Button3_Click})
- $Button4.Add_Click({Button4_Click})
- $Button5.Add_Click({Button5_Click})
- $Button6.Add_Click({Button6_Click})
- #Show the Form
- $Form.ShowDialog() | Out-Null
- ############################################
- ##### Give users access to PROD #####
- Function Button1_Click(){
- $RadioButton1.Visible = $true
- $RadioButton1.Text = "Give access to ECS"
- $RadioButton2.Visible = $true
- $RadioButton2.Text = "Give access to TOS"
- $RadioButton3.Visible = $false
- $RadioButton4.Visible = $false
- $TextBox1.Visible = $true
- $TextBox1.Location = New-Object System.Drawing.Point(135,240)
- $Label2.Visible = $true
- $Label2.Location = New-object System.Drawing.Size(45,240)
- $Button2.Visible = $true
- $Button2.Text = "Enter"
- $Label3.Visible = $false
- $TextBox2.Visible = $false
- $Button2.Add_Click({AddUser})
- }
- ##### Remove users from PROD #####
- Function Button3_Click() {
- $RadioButton1.Visible = $true
- $RadioButton1.Text = "Remove a user from ECS"
- $RadioButton2.Visible = $true
- $RadioButton2.Text = "Remove a user from TOS"
- $RadioButton3.Visible = $true
- $RadioButton3.Text = "Remove all users from ECS"
- $RadioButton4.Visible = $true
- $RadioButton4.Text = "Remove all users from TOS"
- $TextBox1.Visible = $true
- $TextBox1.Location = New-Object System.Drawing.Point(135,240)
- $Label2.Visible = $true
- $Label2.Location = New-object System.Drawing.Size(45,240)
- $Button2.Visible = $true
- $Button2.Text = "Enter"
- $Label3.Visible = $false
- $TextBox2.Visible = $false
- $Button2.Add_Click({Remove-User})
- }
- ##### Check users allowed to PROD #####
- FUnction Button4_Click(){
- $RadioButton1.Visible = $true
- $RadioButton1.Text = "Check users allowed to ECS"
- $RadioButton2.Visible = $true
- $RadioButton2.Text = "Check users allowed to TOS"
- $RadioButton3.Visible = $false
- $RadioButton4.Visible = $false
- $TextBox1.Visible = $false
- $TextBox1.Location = New-Object System.Drawing.Point(135,240)
- $Label2.Visible = $false
- $Label2.Location = New-object System.Drawing.Size(45,240)
- $Button2.Visible = $true
- $Button2.Text = "Check"
- $Label3.Visible = $false
- $TextBox2.Visible = $false
- $Button2.Add_Click({CheckUser})
- }
- ##### Extend users to PROD #####
- Function Button5_Click(){
- $RadioButton1.Visible = $true
- $RadioButton1.Text = "Extend user for ECS"
- $RadioButton2.Visible = $true
- $RadioButton2.Text = "Extend user for TOS"
- $RadioButton3.Visible = $false
- $RadioButton4.Visible = $false
- $TextBox1.Visible = $true
- $TextBox1.Location = New-Object System.Drawing.Point(135,240)
- $Label2.Visible = $true
- $Label2.Location = New-object System.Drawing.Size(45,240)
- $Button2.Visible = $true
- $Button2.Text = "Enter"
- $Label3.Visible = $false
- $TextBox2.Visible = $false
- $Button2.Add_Click({ExtendUser})
- }
- ##### Reset a PROD Password #####
- Function Button6_Click(){
- $RadioButton1.Visible = $false
- $RadioButton2.Visible = $false
- $RadioButton3.Visible = $false
- $RadioButton4.Visible = $false
- $Label2.Visible = $true
- $Label2.Location = New-object System.Drawing.Size(45,55)
- $TextBox1.Visible = $true
- $TextBox1.Location = New-Object System.Drawing.Point(135,55)
- $Label3.Visible = $true
- $TextBox2.Visible = $true
- $Button2.Visible = $true
- $Button2.Text = "Reset Password"
- $Button2.Add_Click({ResetPassword})
- }
- Function AddUser() {
- if ($RadioButton1.Checked){
- $secgroup = "SQL_P1_ADMINS_ECS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- add-adgroupmember -identity $secgroup -members $user
- $MessageBox1.text = "User $($TextBox1.Text) added to $secgroup"
- }
- else { $MessageBox1.text = "User ID $($TextBox1.Text) not found in Active Directory"
- }
- }
- elseif ($RadioButton2.Checked){
- $secgroup = "SQL_P1_ADMINS_TOS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- add-adgroupmember -identity $secgroup -members $user
- $MessageBox1.text = "User $($TextBox1.Text) added to $secgroup"
- }
- else{ $MessageBox1.text = "User ID $($TextBox1.Text) not found in Active Directory"
- }
- }
- }
- Function Remove-User() {
- if ($RadioButton1.Checked){
- $secgroup = "SQL_P1_ADMINS_ECS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- $users = get-adgroupmember -identity $secgroup
- if ($users.samaccountname -contains $user.samaccountname) {
- remove-adgroupmember -identity $secgroup -members $user -confirm:$false
- # REMOVE USER FROM TTL LIST
- #$ttl = (([string](get-adgroup -identity $secgroup -property info).info).split(";") | ? { ($_ -ne $user.samaccountname) }) -join ";"
- #if ($ttl) { set-adgroup -identity $secgroup -replace @{info=$ttl} }
- #else { set-adgroup -identity $secgroup -clear info }
- $MessageBox1.text = "User $($TextBox1.Text) has been removed from $secgroup"
- }
- else{ $MessageBox1.Text = "User $($TextBox1.Text) is not a member of $secgroup"
- }
- }
- }
- elseif ($RadioButton2.Checked){
- $secgroup = "SQL_P1_ADMINS_TOS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- $users = get-adgroupmember -identity $secgroup
- if ($users.samaccountname -contains $user.samaccountname) {
- remove-adgroupmember -identity $secgroup -members $user -confirm:$false
- # REMOVE USER FROM TTL LIST
- #$ttl = (([string](get-adgroup -identity $secgroup -property info).info).split(";") | ? { ($_ -ne $user.samaccountname) }) -join ";"
- #if ($ttl) { set-adgroup -identity $secgroup -replace @{info=$ttl} }
- #else { set-adgroup -identity $secgroup -clear info }
- $MessageBox1.text = "User $($TextBox1.Text) has been removed from $secgroup"
- }
- else{ $MessageBox1.Text = "User $($TextBox1.Text) is not a member of $secgroup"
- }
- }
- }
- elseif ($RadioButton3.Checked){
- $secgroup = "SQL_P1_ADMINS_ECS"
- $Groups = get-adgroupmember $secgroup
- if ($Groups) {
- remove-adgroupmember -identity $secgroup -members ($Groups) -confirm:$false
- #set-adgroup -identity $secgroup -clear info
- $MessageBox1.text = "All users has been removed from $secgroup"
- }
- else{ $MessageBox1.text = "$($secgroup) is already empty"
- }
- }
- elseif ($RadioButton4.Checked){
- $secgroup = "SQL_P1_ADMINS_TOS"
- $Groups = get-adgroupmember $secgroup
- if ($Groups) {
- remove-adgroupmember -identity $secgroup -members ($Groups) -confirm:$false
- #set-adgroup -identity $secgroup -clear info
- $MessageBox1.text = "All users has been removed from $secgroup"
- }
- else{ $MessageBox1.text = "$($secgroup) is already empty"
- }
- }
- }
- Function CheckUser {
- if ($RadioButton1.Checked){
- $secgroup = "SQL_P1_ADMINS_ECS"
- $group = get-adgroup -Identity $($secgroup)
- $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $group) | ? {$_.isactive})) {
- [pscustomobject]@{
- 'UID' = $member.uid
- 'Active since' = $member.lastmodified
- TTL = $member.ttl
- 'Hours remaining' = $member.ttl - $member.age
- }
- }
- $MessageBox1.Text = $list | Format-Table -Autosize | Out-String
- }
- elseif ($RadioButton2.Checked){
- $secgroup = "SQL_P1_ADMINS_TOS"
- $group = get-adgroup -Identity $($secgroup)
- $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $group) | ? {$_.isactive})) {
- [pscustomobject]@{
- 'UID' = $member.uid
- 'Active_since' = $member.lastmodified
- TTL = $member.ttl
- 'Hours_remaining' = $member.ttl - $member.age
- }
- }
- $MessageBox1.Text = $list | Format-Table -Autosize | Out-String
- }
- }
- Function ExtendUser {
- if ($RadioButton1.Checked){
- $secgroup = "SQL_P1_ADMINS_ECS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- $ttl = ([string](get-adgroup -identity $secgroup -property info).info).split(";")
- #set-adgroup -identity $secgroup -replace @{info=($ttl+($user.samaccountname) | select -unique) -join ";"}
- $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $secgroup) | ? {$_.isactive})) {
- [pscustomobject]@{
- UID = $member.uid
- 'Active since' = $member.lastmodified
- TTL = $member.ttl
- 'Hours remaining' = $member.ttl - $member.age
- }
- }
- $MessageBox1.Text = "TTL on $($secgroup) has been extended to 24 hours for User $($TextBox1.Text): `r`n $($list | Format-Table -Autosize | Out-String)"
- }
- else {
- $MessageBox1.Text = "User ID $($TextBox1.Text) is not found in Active Directory"
- }
- }
- elseif ($RadioButton2.Checked){
- $secgroup = "SQL_P1_ADMINS_TOS"
- $user = Get-User $($TextBox1.Text).toupper()
- if ($user) {
- $ttl = ([string](get-adgroup -identity $secgroup -property info).info).split(";")
- #set-adgroup -identity $secgroup -replace @{info=($ttl+($user.samaccountname) | select -unique) -join ";"}
- $list = foreach ($member in (get-adgroupaudit (get-adgroup -identity $secgroup) | ? {$_.isactive})) {
- [pscustomobject]@{
- UID = $member.uid
- 'Active since' = $member.lastmodified
- TTL = $member.ttl
- 'Hours remaining' = $member.ttl - $member.age
- }
- }
- $MessageBox1.Text = "TTL on $($secgroup) has been extended to 24 hours for User $($TextBox1.Text): `r`n $($list | Format-Table -Autosize | Out-String)"
- }
- else {
- $MessageBox1.Text = "User ID $($TextBox1.Text) is not found in Active Directory"
- }
- }
- }
- Function ResetPassword {
- $user = Get-User $($TextBox1.Text).toupper()
- $Password = (ConvertTo-SecureString $TextBox2.Text -AsPlainText -Force)
- if ($user) {
- Set-ADAccountPassword -Identity $($TextBox1.Text) -NewPassword $Password -Reset
- $MessageBox1.Text = "Password for User $($TextBox1.Text) has been changed"
- }
- else {
- $MessageBox1.Text = "Failed to change password for user $($TextBox1.Text)"
- }
- }
- }
- #############################################
- GenerateForm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement