Hi, please could i get some help on the below,<\/p>\n
The goal:<\/p>\n
To have a form with tick boxs that then puts the options ticked into a script to obtain a list of AD users from OU’s.<\/p>\n
The issue:<\/p>\n
I am able to have one of the ticked options to go into the AD users script…BUT it only selects the last option selected in the tick box. I need all selected options (which would be AD attribute names for job title,surname,display name etc) to be put into the script in the ares after -properties & Select. I have searched online but cant seem to find a solution for my skill level I.E im learning PS but im not at this level of knowledge to understand whats required. The form itself has been cobbled together from online research.(so may be a bit of a mess in places).<\/p>\n
Is there any help that anyone could provide please?<\/p>\n
The full code:<\/p>\n
Add-Type -AssemblyName System.Windows.Forms\nAdd-Type -AssemblyName System.Drawing\n\n$form = New-Object System.Windows.Forms.Form\n$form.Text = 'Select an option'\n$form.Size = New-Object System.Drawing.Size(300,200)\n$form.StartPosition = 'CenterScreen'\n\n$okButton = New-Object System.Windows.Forms.Button\n$okButton.Location = New-Object System.Drawing.Point(75,120)\n$okButton.Size = New-Object System.Drawing.Size(75,23)\n$okButton.Text = 'OK'\n$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK\n$form.AcceptButton = $okButton\n$form.Controls.Add($okButton)\n\n$cancelButton = New-Object System.Windows.Forms.Button\n$cancelButton.Location = New-Object System.Drawing.Point(150,120)\n$cancelButton.Size = New-Object System.Drawing.Size(75,23)\n$cancelButton.Text = 'Cancel'\n$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel\n$form.CancelButton = $cancelButton\n$form.Controls.Add($cancelButton)\n\n$label = New-Object System.Windows.Forms.Label\n$label.Location = New-Object System.Drawing.Point(10,20)\n$label.Size = New-Object System.Drawing.Size(280,20)\n$label.Text = 'Please select options:'\n$form.Controls.Add($label)\n\n$listBox = New-Object System.Windows.Forms.checkedlistbox\n$checkBox.CheckOnClick = $true\n$listBox.Location = New-Object System.Drawing.Point(10,40)\n$listBox.Size = New-Object System.Drawing.Size(260,20)\n$CheckedListBox.CheckOnClick = $true\n$CheckedListBox.Items.Add(\"Select All\")\n$CheckedListBox.Items.AddRange(1..10)\n$CheckedListBox.ClearSelected()\n$objForm.Controls.Add($CheckedListBox)\n$listBox.Height = 80\n\n###The below has the names the same as the AD attributes are called.....ive done it like this as i was trying to pass that into the script as a variable.\n[void] $listBox.Items.Add(\"EmailAddress\")\n[void] $listBox.Items.Add('Surname')\n[void] $listBox.Items.Add('title')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n\n$form.Controls.Add($listBox)\n\n$form.Topmost = $true\n\n$result = $form.ShowDialog()\n\nif ($result -eq [System.Windows.Forms.DialogResult]::OK)\n{\n######the idea below was to place the text from the list boxs into a variable that i then put the variable into the script below.\n $Email = $listBox.SelectedItem.ToString();\n $surname = $listBox.SelectedItem.ToString();\n $Job = $listBox.SelectedItem.ToString();\n\n $ous = \"OU=Some place A,OU=Some place A,DC=B,DC=Some place c\",\"OU=Some place D,OU=Some place E,DC=F,DC=Some place G,DC=H\"\n\n ####and here is where i would like the selected items to appear after -properties & Select with the , in between.\n$ous | ForEach { Get-ADUser -Filter {Enabled -eq $True} -SearchBase $_ -properties displayname,$Email,$surname,$job} | Select displayname,$surname,$job | Export-Csv -path c:\\test.csv -NoTypeInformation\n}\n<\/code><\/pre>","upvoteCount":5,"answerCount":9,"datePublished":"2021-01-20T13:37:24.000Z","author":{"@type":"Person","name":"the-guvner","url":"https://community.spiceworks.com/u/the-guvner"},"suggestedAnswer":[{"@type":"Answer","text":"Hi, please could i get some help on the below,<\/p>\n
The goal:<\/p>\n
To have a form with tick boxs that then puts the options ticked into a script to obtain a list of AD users from OU’s.<\/p>\n
The issue:<\/p>\n
I am able to have one of the ticked options to go into the AD users script…BUT it only selects the last option selected in the tick box. I need all selected options (which would be AD attribute names for job title,surname,display name etc) to be put into the script in the ares after -properties & Select. I have searched online but cant seem to find a solution for my skill level I.E im learning PS but im not at this level of knowledge to understand whats required. The form itself has been cobbled together from online research.(so may be a bit of a mess in places).<\/p>\n
Is there any help that anyone could provide please?<\/p>\n
The full code:<\/p>\n
Add-Type -AssemblyName System.Windows.Forms\nAdd-Type -AssemblyName System.Drawing\n\n$form = New-Object System.Windows.Forms.Form\n$form.Text = 'Select an option'\n$form.Size = New-Object System.Drawing.Size(300,200)\n$form.StartPosition = 'CenterScreen'\n\n$okButton = New-Object System.Windows.Forms.Button\n$okButton.Location = New-Object System.Drawing.Point(75,120)\n$okButton.Size = New-Object System.Drawing.Size(75,23)\n$okButton.Text = 'OK'\n$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK\n$form.AcceptButton = $okButton\n$form.Controls.Add($okButton)\n\n$cancelButton = New-Object System.Windows.Forms.Button\n$cancelButton.Location = New-Object System.Drawing.Point(150,120)\n$cancelButton.Size = New-Object System.Drawing.Size(75,23)\n$cancelButton.Text = 'Cancel'\n$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel\n$form.CancelButton = $cancelButton\n$form.Controls.Add($cancelButton)\n\n$label = New-Object System.Windows.Forms.Label\n$label.Location = New-Object System.Drawing.Point(10,20)\n$label.Size = New-Object System.Drawing.Size(280,20)\n$label.Text = 'Please select options:'\n$form.Controls.Add($label)\n\n$listBox = New-Object System.Windows.Forms.checkedlistbox\n$checkBox.CheckOnClick = $true\n$listBox.Location = New-Object System.Drawing.Point(10,40)\n$listBox.Size = New-Object System.Drawing.Size(260,20)\n$CheckedListBox.CheckOnClick = $true\n$CheckedListBox.Items.Add(\"Select All\")\n$CheckedListBox.Items.AddRange(1..10)\n$CheckedListBox.ClearSelected()\n$objForm.Controls.Add($CheckedListBox)\n$listBox.Height = 80\n\n###The below has the names the same as the AD attributes are called.....ive done it like this as i was trying to pass that into the script as a variable.\n[void] $listBox.Items.Add(\"EmailAddress\")\n[void] $listBox.Items.Add('Surname')\n[void] $listBox.Items.Add('title')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n[void] $listBox.Items.Add('Empty at mo')\n\n$form.Controls.Add($listBox)\n\n$form.Topmost = $true\n\n$result = $form.ShowDialog()\n\nif ($result -eq [System.Windows.Forms.DialogResult]::OK)\n{\n######the idea below was to place the text from the list boxs into a variable that i then put the variable into the script below.\n $Email = $listBox.SelectedItem.ToString();\n $surname = $listBox.SelectedItem.ToString();\n $Job = $listBox.SelectedItem.ToString();\n\n $ous = \"OU=Some place A,OU=Some place A,DC=B,DC=Some place c\",\"OU=Some place D,OU=Some place E,DC=F,DC=Some place G,DC=H\"\n\n ####and here is where i would like the selected items to appear after -properties & Select with the , in between.\n$ous | ForEach { Get-ADUser -Filter {Enabled -eq $True} -SearchBase $_ -properties displayname,$Email,$surname,$job} | Select displayname,$surname,$job | Export-Csv -path c:\\test.csv -NoTypeInformation\n}\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2021-01-20T13:37:24.000Z","url":"https://community.spiceworks.com/t/add-selections-from-ticked-boxs-into-powershell-script/788000/1","author":{"@type":"Person","name":"the-guvner","url":"https://community.spiceworks.com/u/the-guvner"}},{"@type":"Answer","text":"Instead of making a list of checkboxes from scratch, I would recommend running your query and sending the results to Out-GridView, and you can use the -PassThru parameter to send the selection output back to your script.<\/p>\n