Created
December 2, 2015 14:38
-
-
Save crshnbrn66/157dacf93ed88718c99a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This gets the group information in a functioncall used by two other functions | |
function getComputerGroup | |
{ param($strComputer, $strGroup) | |
$computer = [ADSI]('WinNT://' + $strComputer + ',computer') | |
$Group = $computer.psbase.children.find($strGroup) | |
return $Group | |
} | |
# This will list what’s currently in a Group so you can verify the result | |
function listUsers | |
{ param($strComputer, $strGroup) | |
$returnGroup = getComputerGroup -strComputer $strComputer -strGroup $strGroup | |
$members= $returnGroup.psbase.invoke('Members') | %{$_.GetType().InvokeMember('Adspath', 'GetProperty', $null, $_, $null)} | |
$members |Out-GridView -Title ($strComputer +' '+ $strGroup) | |
} | |
#function to add a user to the affected machine group | |
function Add-User | |
{ param($strComputer, $strGroup, $strUser) | |
$returnGroup = getComputerGroup -strComputer $strComputer -strGroup $strGroup | |
$tempvar ='WinNT://'+$strUser+',group' | |
$returnGroup.Add($tempvar) | |
} | |
#function to delete a user to the affected/machine/group | |
function Del-User | |
{ | |
param($strComputer, $strGroup, $strUser) | |
$returnGroup = getComputerGroup -strComputer $strComputer -strGroup $strGroup | |
$tempvar ='WinNT://'+$strUser+',group' | |
$returnGroup.Remove($tempvar) | |
} | |
# Load the Winforms assembly | |
[reflection.assembly]::LoadWithPartialName( 'System.Windows.Forms') | |
# Create the form | |
$form = New-Object Windows.Forms.Form | |
#Set the dialog title | |
$form.text = 'Group Query Tool' | |
# Create the label control and set text, size and location | |
$svrLabel = New-Object Windows.Forms.Label | |
$svrLabel.Location = New-Object Drawing.Point 10,20 | |
$svrLabel.Size = New-Object Drawing.Point 200,15 | |
$svrLabel.text = 'Enter the server name' | |
$grplabel = New-Object Windows.Forms.Label | |
$grplabel.Location = New-Object Drawing.Point 10,60 | |
$grplabel.Size = New-Object Drawing.Point 200,15 | |
$grplabel.text = 'Enter the group name' | |
$userlabel = new-object windows.forms.label | |
$userlabel.Location = New-Object Drawing.Point 10,100 | |
$userlabel.Size = New-Object Drawing.Point 375,15 | |
$userlabel.text = 'Enter the username' | |
$dbglabel = New-Object Windows.Forms.Label | |
$dbglabel.Location = New-Object Drawing.Point 10,150 | |
$dbglabel.Size = New-Object Drawing.Point 200,15 | |
$dbglabel.text = 'Junk' | |
$dbglabel.visible = $false | |
$svrtext = New-Object Windows.Forms.TextBox | |
$svrtext.Location = New-Object Drawing.Point 10,40 | |
$svrtext.Size = New-Object Drawing.Point 200,15 | |
$svrtext.text = 'Server' | |
$grptext = New-Object Windows.Forms.TextBox | |
$grptext.Location = New-Object Drawing.Point 10,80 | |
$grptext.Size = New-Object Drawing.Point 200,15 | |
$grptext.text = 'administrators' | |
$usertext = New-Object Windows.Forms.TextBox | |
$usertext.Location = New-Object Drawing.Point 10,120 | |
$usertext.Size = New-Object Drawing.Point 200,15 | |
# Create Button and set text and location | |
$button = New-Object Windows.Forms.Button | |
$button.text = 'GO' | |
$button.Location = New-Object Drawing.Point 50,200 | |
$rdoAdd = New-Object Windows.forms.RadioButton | |
$rdoAdd.text = 'Add' | |
$rdoAdd.height = 20 | |
$rdoAdd.width = 50 | |
$rdoAdd.top=170 | |
$rdoAdd.left =10 | |
$rdoDel = New-Object Windows.forms.RadioButton | |
$rdoDel.text = 'Del' | |
$rdoDel.height = 20 | |
$rdoDel.width = 50 | |
$rdoDel.top=170 | |
$rdoDel.left =60 | |
$rdoLst = New-Object Windows.forms.RadioButton | |
$rdoLst.text = 'List' | |
$rdoLst.height = 20 | |
$rdoLst.width = 50 | |
$rdoLst.top=170 | |
$rdoLst.left =110 | |
# Set up event handler to extarct text from TextBox and display it on the Label. | |
$button.add_click({ | |
$strComputer = $svrtext.text | |
$strgroup = $grptext.text | |
$username = $usertext.text | |
if($rdoAdd.checked) | |
{$status = 'Add' | |
Add-User -strComputer $strComputer -strGroup $strgroup -strUser $username | |
} | |
elseif($rdoLst.checked) | |
{$status = 'List' | |
listUsers -strComputer $strComputer -strGroup $strgroup | |
} | |
elseif($rdoDel.checked) | |
{$status = 'Del' | |
Del-User -strComputer $strComputer -strGroup $strgroup -strUser $username | |
} | |
$dbglabel.visible = $true | |
$dbglabel.Text = 'Action: '+ $status + ' ' + $svrtext.text +' ' + $grptext.text + ' ' + $usertext.text | |
#$debuglabel.visible = true | |
}) | |
# Add the controls to the Form | |
$form.controls.add($button) | |
$form.controls.add($svrlabel) | |
$form.controls.add($svrtext) | |
$form.controls.add($grplabel) | |
$form.controls.add($grptext) | |
$form.controls.add($rdoAdd) | |
$form.controls.add($rdoDel) | |
$form.controls.add($rdoLst) | |
$form.controls.add($dbglabel) | |
$form.controls.add($userlabel) | |
$form.controls.add($usertext) | |
# Display the dialog | |
$form.ShowDialog() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment