I am setting up a large powershell script with dialog boxes to do the following:<\/p>\n
I have most of it but I am not a great programmer so my thought would be to display a message box then have a button for all the special depts (about 26 of them unfortunately) and then have a for loop or something that says >> if this PM button is clicked >> then add them to PM, and any other groups.<\/p>\n
Anyone have anything they have already done that is similar or perhaps better than this idea? I am an infrastructure and solutions architect so the code portion is not my string suit but I’m always willing to learn <\/p>\n
Thanks<\/p>\n
@powershelln00b<\/a><\/p>","upvoteCount":3,"answerCount":24,"datePublished":"2021-11-04T19:39:16.000Z","author":{"@type":"Person","name":"eric-schroeder","url":"https://community.spiceworks.com/u/eric-schroeder"},"suggestedAnswer":[{"@type":"Answer","text":" I am setting up a large powershell script with dialog boxes to do the following:<\/p>\n I have most of it but I am not a great programmer so my thought would be to display a message box then have a button for all the special depts (about 26 of them unfortunately) and then have a for loop or something that says >> if this PM button is clicked >> then add them to PM, and any other groups.<\/p>\n Anyone have anything they have already done that is similar or perhaps better than this idea? I am an infrastructure and solutions architect so the code portion is not my string suit but I’m always willing to learn Thanks<\/p>\n @powershelln00b<\/a><\/p>","upvoteCount":3,"datePublished":"2021-11-04T19:39:16.000Z","url":"https://community.spiceworks.com/t/powershell-to-add-new-user-to-specific-groups/816034/1","author":{"@type":"Person","name":"eric-schroeder","url":"https://community.spiceworks.com/u/eric-schroeder"}},{"@type":"Answer","text":" I will make the most common first response: What have you tried and what were your results?<\/p>\n And the most common second response: Please post the relevant pieces of your code so we can help you with it. Please use the ‘Insert Code’ button. Please and thank you!<\/p>\n PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a><\/p>\n Housekeeping aside, let’s see if we can get the conversation rolling. I would approach the problem in steps:<\/p>\n Build a list of the groups<\/p>\n Figure out what kind of interface you want to use<\/p>\n Collect the selections as a variable<\/p>\n<\/li>\n Use a ForEach to run through the contents of the variable and add-adgroupmember ( Assuming you are on-premises AD and not AzureAD )<\/p>\n Do you have any of this already done?<\/p>\n @eric-schroeder<\/a><\/p>","upvoteCount":0,"datePublished":"2021-11-04T20:16:38.000Z","url":"https://community.spiceworks.com/t/powershell-to-add-new-user-to-specific-groups/816034/2","author":{"@type":"Person","name":"AdmiralKirk","url":"https://community.spiceworks.com/u/AdmiralKirk"}},{"@type":"Answer","text":" Below is the code I am currently working on. The If statements will have the AzureAD will include the Add ad group. I was also considering trying to see if it would be possible to have a dialog/message box that includes a drop down with the entire list to select from and then run the if loops with the variables that are collected?<\/p>\n NOTE: This is only the “Add to specialized groups” section that I split out from the rest of the portions I created.<\/p>\n I ran that code and see the pop-up, but it asks for free text input. I think a Listbox ( dropdown list ) would be your answer.<\/p>\n Assuming you have on-premises Active Directory, or hybrid on-premises with Azure, you can do this with AD cmdlets. If you’re AzureAD only then you will need to tweak for AzureAD cmdlets. Then I have a function that triggers when the cursor leaves the listbox, whose purpose is to store the result where I can access it later. In my case it is a global variable called $UserInfo. Later on, when I create the user account, I just set the Department value to $Userinfo.Department<\/p>\n My approach is to pull all the values populated in the Department field for all users, then allow HR to select from the existing departments. I have additional filters on the Get-ADUser that I did not show, but at the minimum I’m only pulling user accounts that have<\/em> a Department listed. This eliminates most of the system-generated service accounts -not that they have Departments anyway, but it could improve query performance. The thing I like about this approach is if we add, remove, or rename a Department, then the script will automatically provide the current list. I built it based on this StackOverflow entry: powershell - How to make output from array to listbox? - Stack Overflow<\/a> Let me know if this makes sense, or if you have trouble adapting it, or whatever.<\/p>\n\n
<\/p>\n
<\/p>\n
\n
\n
\n
\n
\n```\nAdd-Type -AssemblyName System.Windows.Forms\nAdd-Type -AssemblyName System.Drawing\n\n$form = New-Object System.Windows.Forms.Form\n$form.Text = 'New User name'\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 = 'What Department will the user be assigned too?:'\n$form.Controls.Add($label)\n\n$textBox = New-Object System.Windows.Forms.TextBox\n$textBox.Location = New-Object System.Drawing.Point(10,40)\n$textBox.Size = New-Object System.Drawing.Size(260,20)\n$form.Controls.Add($textBox)\n\n$form.Topmost = $true\n\n$form.Add_Shown({$textBox.Select()})\n$result = $form.ShowDialog()\n\nif ($result -eq [System.Windows.Forms.DialogResult]::OK)\n{\n $x = $textBox.Text\n $x\n}\n\n#### If Statements for special group additions\n\nif ( 'Project Engineers' -eq $x )\n{\n # do something\n Write-Output \"You chose Project Engineers\"\n Add-ADGroupMember -Identity Whatevergroup -Members $UPN\n}\n\nif ( 'Sr. Project Engineers' -eq $x )\n{\n # do something\n Write-Output \"You chose Sr. Project Engineers\"\n}\n\nif ( 'Project Managers' -eq $x )\n{\n # do something\n Write-Output \"You chose Project Managers\"\n}\n\nif ( 'Sr. Project Managers' -eq $x )\n{\n # do something\n Write-Output \"You chose Sr. Project Managers\"\n}\n\nif ( 'Project Executive' -eq $x )\n{\n # do something\n Write-Output \"You chose Project Executive\"\n}\n\nif ( 'Leadership' -eq $x )\n{\n # do something\n Write-Output \"You chose Leadership\"\n}\n\nif ( 'Foreman' -eq $x )\n{\n # do something\n Write-Output \"You chose Foreman\"\n}\n\nif ( 'Superintendents' -eq $x )\n{\n # do something\n Write-Output \"You chose Superintendents\"\n}\n\nif ( 'Sr. Superintendents' -eq $x )\n{\n # do something\n Write-Output \"You chose Sr. Superintendents\"\n}\n\nif ( 'PreCon' -eq $x )\n{\n # do something\n Write-Output \"You chose PreCon\"\n}\n\nif ( 'PreCon Executive' -eq $x )\n{\n # do something\n Write-Output \"You chose PreCon Executive\"\n}\n\nif ( 'Cost Engineer' -eq $x )\n{\n # do something\n Write-Output \"You chose Cost Engineer\"\n}\n\nif ( 'Project Estimators' -eq $x )\n{\n # do something\n Write-Output \"You chose Project Estimators\"\n}\n\nif ( 'Procraft' -eq $x )\n{\n # do something\n Write-Output \"You chose Procraft\"\n #Ask if its a Driver or Operations Personnel\n}\n\nif ( 'CTI' -eq $x )\n{\n # do something\n Write-Output \"You chose CTI\"\n}\n\n}\n```\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-11-04T20:39:46.000Z","url":"https://community.spiceworks.com/t/powershell-to-add-new-user-to-specific-groups/816034/3","author":{"@type":"Person","name":"eric-schroeder","url":"https://community.spiceworks.com/u/eric-schroeder"}},{"@type":"Answer","text":"
\nWe are hybrid, and here is what I use to prepopulate a Department listbox for our HR team to use during hiring:<\/p>\n# Add text box to enter the Department\n $DepartmentTextBox = New-Object System.Windows.Forms.ComboBox\n $DepartmentTextBox.Location = 'x,y'\n $DepartmentTextBox.Size = 'x,y'\n [void] $DepartmentTextBox.Items.Add(\"\") <# put a blank at the top of the list #>\n\n <# Pull the department list and populate into the listbox #>\n foreach ($Dept in Get-ADUser -filter \" Department -Like '*'\" -Properties Department | Select-Object Department | Sort-Object Department -Unique) {\n [void] $DepartmentTextBox.Items.Add($Dept)\n }\n $DepartmentTextBox.DisplayMember = \"Department\" \n<\/code><\/pre>\n
$DepartmentTextBox.Add_Leave({\n $UserInfo.Department = $DepartmentTextBox.Text\n})\n<\/code><\/pre>\n
\nOne advantage of a Listbox is that since it is only one line you can put it on your main form instead of a separate pop-up, which ought to simplify the code a bit.<\/p>\n