Asked by:
Have an error in the script

General discussion
-
I originally posted this in the incorrect forum and do not know how to move it, so I am doing a repost. Sorry mods.
I freely admit that I got this script from the Microsoft site. I have 1 or maybe 2 problems with it. The script does what it is supposed to. However, if you only select a security group and click add, it will give an error at line 171. If you select only a distribution group, it gives an error at line 170. If you select something from both columns, then there is no error. I can select an existing group the user in already in, in either column and one that they are not part of, and it works properly. I cannot seem to find how to get it to accept a null entry in one of the columns to just pass through. The script is as follows:
If Life Is A Dream, What Will We See When We Wake Up?
- Changed type Bill_Stewart Friday, July 7, 2017 6:42 PM
- Moved by Bill_Stewart Friday, July 7, 2017 6:43 PM This is not "fix/debug/rewrite my script for me" forum
Wednesday, May 24, 2017 11:37 AM
All replies
-
<#Powershell script to manage security groups and distribution groups for a single user with windows forms GUI.
You should not have to customize any code in this script for it to work. Just extract the AD folder to C:\ .
I've included a shortcut that will launch the script. I may take a second for the form to load.You must search for users by their logon id. #>
Add-Type -AssemblyName System.Windows.Forms
Import-Module ActiveDirectory##function to populate the current group list box with the users current security group membership
function cgroups { process {
$group10 = Get-ADPrincipalGroupMembership -Identity $id2
foreach($groups in $group10){
$Currentgroups.Items.Add($groups.Name);
}
}
}# function to populate the allgroups listbox with all current security groups located in the domain AD
function sgroups { process{$group1 = Get-ADGroup -Filter {GroupCategory -eq 'security'}
foreach($group in $group1){
$allgroups.Items.Add($group.Name);
}
}
}#function to pre populate the dgroups with all current distribution groups
function mgroups { process {$group8 = Get-ADGroup -Filter {GroupCategory -eq 'distribution'}
foreach($group in $group8){
$dgroups.Items.Add($group.Name);
}
}
}function clear { process{
$Currentgroups.Items.Clear();
$userid.Text = "";
}}
#convert system drawing to a displayable image and displays it in the image box.
function success { process {
#$file1 = (Get-Item '\\syscon\share\AD_Powershell\check3.jpg')
#$img1 = [System.Drawing.Image]::Fromfile($file1);
#$pbox2.Image = $img1;}
}#convert system drawing to a displayable image and displays it in the image box.
function fail { process {
#$file2 = (Get-Item '\\syscon\share\AD_Powershell\redx.jpg')
#$img2 = [System.Drawing.Image]::Fromfile($file2);
#$pbox2.Image = $img2;}
}
#clears specified fields and selected items
function cclear { process{Start-Sleep -Seconds 3
$pbox2.Image = $null;
$complete1.text = "";
$dgroups.ClearSelected()
$allgroups.ClearSelected()
}
}
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "AD Groups"
$Form.TopMost = $true
$Form.Width = 900
$Form.BackColor = "white"
$Form.Height = 433$Currentgroups = New-Object system.windows.Forms.ListBox
$Currentgroups.Text = "listBox"
$Currentgroups.Width = 249
$Currentgroups.Height = 221
$Currentgroups.location = new-object system.drawing.point(17,94)
$Form.controls.Add($Currentgroups)$allgroups = New-Object system.windows.Forms.ListBox
$allgroups.Text = "listBox"
$allgroups.Width = 249
$allgroups.Height = 221
$allgroups.location = new-object system.drawing.point(320,94)
$Form.controls.Add($allgroups)$dgroups = New-Object system.windows.Forms.ListBox
$dgroups.Text = "listBox"
$dgroups.Width = 249
$dgroups.Height = 221
$dgroups.location = new-object system.drawing.point(620,94)
$Form.controls.Add($dgroups)
$userID = New-Object system.windows.Forms.TextBox
$userID.Width = 100
$userID.Height = 20
$userID.location = new-object system.drawing.point(86,25)
$userID.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($userID)$label8 = New-Object system.windows.Forms.Label
$label8.Text = "UserID:"
$label8.AutoSize = $true
$label8.Width = 25
$label8.Height = 10
$label8.location = new-object system.drawing.point(16,26)
$label8.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($label8)#finds user specified in text box and returns their group membership to the Currentgroups listbox.
$button10 = New-Object system.windows.Forms.Button
$button10.Text = "Find"
$button10.BackColor = "Green"
$button10.Width = 101
$button10.Height = 20
$button10.Add_MouseClick({
$script:id2 = $userID.Text;
$getu = Get-ADUser -Identity $id2
$Currentgroups.Items.Clear();
$pbox2.Image = $null;
$complete1.Text = "";
if($getu -eq $null){
fail;
$complete1.Text = "User Not Found";
}else {
cgroups;
success;
$complete1.Text = "User Found";
cclear;
}})
$button10.location = new-object system.drawing.point(213,25)
$button10.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($button10)
#adds user to group selected in either the distribution groups listbox or the security groups listbox
$button12 = New-Object system.windows.Forms.Button
$button12.Text = "Add"
$button12.BackColor = "Green"
$button12.Width = 250
$button12.Height = 34
$button12.Add_MouseClick({$sgroup = $allgroups.SelectedItems;
$mgroup = $dgroups.SelectedItems;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $sgroup;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $mgroup;
success;
$complete1.Text = "Added to group";
Start-Sleep -Seconds 1;
$Currentgroups.Items.Clear();
cgroups;
cclear;
})
$button12.location = new-object system.drawing.point(320,334)
$button12.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($button12)
#removes user from selected group in "Current Groups" list box and refreshes the results of that box.
$button15 = New-Object system.windows.Forms.Button
$button15.Text = "Remove"
$button15.BackColor = "Green"
$button15.Width = 249
$button15.Height = 30
$button15.Add_MouseClick({
$group = $Currentgroups.SelectedItem;Remove-ADGroupMember -Identity $group -Member $id2 -Confirm: $false;
$complete1.Text = "Completed"
success;
$Currentgroups.Items.Clear();
cgroups;
cclear;
})
$button15.location = new-object system.drawing.point(17,335)
$button15.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($button15)$pbox2 = New-Object system.windows.Forms.PictureBox
$pbox2.Width = 98
$pbox2.Height = 78
$pbox2.location = new-object system.drawing.point(452,4)
$Form.controls.Add($pbox2)$complete1 = New-Object system.windows.Forms.Label
$complete1.AutoSize = $true
$complete1.Width = 25
$complete1.Height = 10
$complete1.location = new-object system.drawing.point(350,7)
$complete1.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($complete1)$label23 = New-Object system.windows.Forms.Label
$label23.Text = "Current Groups"
$label23.AutoSize = $true
$label23.Width = 25
$label23.Height = 10
$label23.location = new-object system.drawing.point(18,74)
$label23.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($label23)$label25 = New-Object system.windows.Forms.Label
$label25.Text = "Security groups"
$label25.AutoSize = $true
$label25.Width = 25
$label25.Height = 10
$label25.location = new-object system.drawing.point(340,74)
$label25.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($label25)$label27 = New-Object system.windows.Forms.Label
$label27.Text = "Distribution Groups"
$label27.AutoSize = $true
$label27.Width = 25
$label27.Height = 10
$label27.location = new-object system.drawing.point(620,74)
$label27.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($label27)sgroups
mgroups[void]$Form.ShowDialog()
$Form.Dispose()If Life Is A Dream, What Will We See When We Wake Up?
Wednesday, May 24, 2017 11:37 AM -
I recommend that you ask the author of the script for help.
This forum in not for fixing scripts you have found on the Internet.
Here is a hint: Yu have to test each ListBox for a selection and code the appropriate steps.
if($sgroup = $allgroups.SelectedItems){
# preform action
}It is just basic PowerShell.
\_(ツ)_/
- Edited by jrv Wednesday, May 24, 2017 2:07 PM
Wednesday, May 24, 2017 1:57 PM -
The thing is this is a script downloaded from Microsoft. Hence, I was under the impression I thought I was asking a forum that dealt with the development of the script.
If Life Is A Dream, What Will We See When We Wake Up?
Wednesday, May 24, 2017 2:11 PM -
The thing is this is a script downloaded from Microsoft. Hence, I was under the impression I thought I was asking a forum that dealt with the development of the script.
Just so you know: This is a peer-to-peer volunteer support forum without any SLA. We don't have the resources to redesign/rewrite scripts on demand. See the first post from the top of this forum:
This forum is for scripting questions rather than script requests
-- Bill Stewart [Bill_Stewart]
Wednesday, May 24, 2017 2:18 PM -
The script is old and badly designed. It is from a community forum. We do not fix scripts on request. It is assumed that you are a PowerShell scripter and understand how to write and understand basic scripts.
If you want to learn scripting I recommend avoiding Forms until you understand basic PowerShell.
\_(ツ)_/
Wednesday, May 24, 2017 2:19 PM -
I was not asking for a redesign/rewrite. I was asking about how to get the script to accept a null entry on the one column or the other.
If Life Is A Dream, What Will We See When We Wake Up?
Wednesday, May 24, 2017 2:22 PM -
I was not asking for a redesign/rewrite. I was asking about how to get the script to accept a null entry on the one column or the other.
I would recommend posting a short script that contains only the absolute minimum amount of code needed to reproduce the problem. This is known as the SSCCE principle.
-- Bill Stewart [Bill_Stewart]
Wednesday, May 24, 2017 2:29 PM -
I included the entire script because other forums throughout the course of discussion end up wanting to see the entire script. So, I did a shortcut and just posted the entire script up front. So, the portion of the script I am asking for assistance with is :
$sgroup = $allgroups.SelectedItems;
$mgroup = $dgroups.SelectedItems;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $sgroup;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $mgroup;when the $sgroup and $mgroup have a null value entered, the script gives an error. I understand null values are not good for programming in any language. How do I get it to skip the Add-ADPrincipaGroupMembership if either variable contains a null value entry?
If Life Is A Dream, What Will We See When We Wake Up?
Wednesday, May 24, 2017 2:59 PM -
I included the entire script because other forums throughout the course of discussion end up wanting to see the entire script. So, I did a shortcut and just posted the entire script up front. So, the portion of the script I am asking for assistance with is :
$sgroup = $allgroups.SelectedItems;
$mgroup = $dgroups.SelectedItems;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $sgroup;
Add-ADPrincipalGroupMembership -Identity $id2 -MemberOf $mgroup;when the $sgroup and $mgroup have a null value entered, the script gives an error. I understand null values are not good for programming in any language. How do I get it to skip the Add-ADPrincipaGroupMembership if either variable contains a null value entry?
If Life Is A Dream, What Will We See When We Wake Up?
I posted the answer above. You need t test the results and take appropriate action.
\_(ツ)_/
Wednesday, May 24, 2017 3:05 PM -
So, I did a shortcut and just posted the entire script up front. So, the portion of the script I am asking for assistance with is :
...That assumes that the adjustment to your code to produce the behavior you want is only in those two lines of code. That's not a realistic assumption for a number of reasons. This is why it's best to write a short, self-contained example that contains only the minimum amount of code needed to reproduce the problem.
-- Bill Stewart [Bill_Stewart]
- Edited by Bill_Stewart Wednesday, May 24, 2017 3:31 PM
Wednesday, May 24, 2017 3:25 PM