diff --git a/contrast.ps1 b/contrast.ps1 new file mode 100644 index 0000000..4831fac --- /dev/null +++ b/contrast.ps1 @@ -0,0 +1,202 @@ +Add-Type -AssemblyName System.Windows.Forms + +#Set Domain Controller Variable + +$domaincontroller = "WIN7108" + +# Create a form +$form = New-Object Windows.Forms.Form +$form.Text = "Compare AD Group Memberships" +$form.Width = 600 +$form.Height = 900 +$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle + +# Signature for UPNs +$labelSignature = New-Object Windows.Forms.Label +$labelSignature.Text = "Made by Zak Bearman - Intel" +$labelSignature.Location = New-Object Drawing.Point(10, 832) +$form.Controls.Add($labelSignature) + +# Labels and Textboxes for UPNs +$labelPrimaryUser = New-Object Windows.Forms.Label +$labelPrimaryUser.Text = "Primary User UPN:" +$labelPrimaryUser.Location = New-Object Drawing.Point(10, 23) +$form.Controls.Add($labelPrimaryUser) + +$textBoxPrimaryUser = New-Object Windows.Forms.TextBox +$textBoxPrimaryUser.Location = New-Object Drawing.Point(120, 20) +$textBoxPrimaryUser.Width = 200 +$form.Controls.Add($textBoxPrimaryUser) + +# ComboBox for selecting the number of users to compare against +$labelNumUsers = New-Object Windows.Forms.Label +$labelNumUsers.Text = "How many other users?" +$labelNumUsers.Location = New-Object Drawing.Point(10, 50) +$form.Controls.Add($labelNumUsers) + +$comboBoxNumUsers = New-Object Windows.Forms.ComboBox +$comboBoxNumUsers.Location = New-Object Drawing.Point(250, 50) +$comboBoxNumUsers.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList +$comboBoxNumUsers.Items.Add("1") +$comboBoxNumUsers.Items.Add("2") +$comboBoxNumUsers.Items.Add("3") +$comboBoxNumUsers.SelectedIndex = 0 +$form.Controls.Add($comboBoxNumUsers) + +# GroupBox for UPNs of users to compare against +$groupBoxUsers = New-Object Windows.Forms.GroupBox +$groupBoxUsers.Text = "Users to Compare Against:" +$groupBoxUsers.Location = New-Object Drawing.Point(10, 80) +$groupBoxUsers.Width = 340 +$groupBoxUsers.Height = 100 +$form.Controls.Add($groupBoxUsers) + +# Textboxes for entering UPNs based on the selected number of users +$textBoxUser1 = New-Object Windows.Forms.TextBox +$textBoxUser1.Location = New-Object Drawing.Point(10, 20) +$textBoxUser1.Width = 200 +$groupBoxUsers.Controls.Add($textBoxUser1) + +$textBoxUser2 = New-Object Windows.Forms.TextBox +$textBoxUser2.Location = New-Object Drawing.Point(10, 50) +$textBoxUser2.Width = 200 +$textBoxUser2.Visible = $false +$groupBoxUsers.Controls.Add($textBoxUser2) + +$textBoxUser3 = New-Object Windows.Forms.TextBox +$textBoxUser3.Location = New-Object Drawing.Point(10, 80) +$textBoxUser3.Width = 200 +$textBoxUser3.Visible = $false +$groupBoxUsers.Controls.Add($textBoxUser3) + +# Button to initiate comparison +$buttonCompare = New-Object Windows.Forms.Button +$buttonCompare.Text = "Compare" +$buttonCompare.Location = New-Object Drawing.Point(120, 195) +$form.Controls.Add($buttonCompare) + +# Help button to display usage instructions +$buttonHelp = New-Object Windows.Forms.Button +$buttonHelp.Text = "Click Me" +$buttonHelp.Location = New-Object Drawing.Point(215, 195) +$form.Controls.Add($buttonHelp) + +# Scrollable and selectable textbox for displaying output +$textBoxOutput = New-Object Windows.Forms.TextBox +$textBoxOutput.Location = New-Object Drawing.Point(10, 230) +$textBoxOutput.Width = 560 +$textBoxOutput.Height = 600 +$textBoxOutput.Multiline = $true +$textBoxOutput.ScrollBars = "Vertical" +$textBoxOutput.ReadOnly = $true +$form.Controls.Add($textBoxOutput) + +# Event handler for the ComboBox selection change +$comboBoxNumUsers.Add_SelectedIndexChanged({ + $numUsers = [int]$comboBoxNumUsers.SelectedItem + + # Show/hide textboxes based on the selected number of users + $textBoxUser1.Visible = $numUsers -ge 1 + $textBoxUser2.Visible = $numUsers -ge 2 + $textBoxUser3.Visible = $numUsers -ge 3 +}) + +# Event handler for the Compare button +$buttonCompare.Add_Click({ + # Get the entered UPNs and the selected number of users + $primaryUserUPN = $textBoxPrimaryUser.Text + $numUsers = [int]$comboBoxNumUsers.SelectedItem + + # Get the UPNs of the users to compare against based on the selected number + $usersToCompareUPNs = @() + if ($textBoxUser1.Visible) { $usersToCompareUPNs += $textBoxUser1.Text } + if ($textBoxUser2.Visible) { $usersToCompareUPNs += $textBoxUser2.Text } + if ($textBoxUser3.Visible) { $usersToCompareUPNs += $textBoxUser3.Text } + + # Function to get group memberships for a user using UPN + function Get-ADUserGroupsByUPN { + param ( + [string]$userUPN + ) + + $user = Get-ADUser -Filter { UserPrincipalName -eq $userUPN } -Server $domaincontroller + if ($user) { + $groups = Get-ADPrincipalGroupMembership -Identity $user -Server $domaincontroller + return $groups | Select-Object -ExpandProperty Name + } else { + Write-Host "User with UPN $userUPN not found." + return $null + } + } + + # Get group memberships for the primary user + $primaryUserGroups = Get-ADUserGroupsByUPN -userUPN $primaryUserUPN + + if ($primaryUserGroups -ne $null) { + # Compare group memberships for the primary user with other users + $missingGroups = @() + + foreach ($userUPN in $usersToCompareUPNs) { + $userGroups = Get-ADUserGroupsByUPN -userUPN $userUPN + if ($userGroups -ne $null) { + foreach ($group in $userGroups) { + if ($primaryUserGroups -notcontains $group -and $missingGroups -notcontains $group) { + $missingGroups += $group + } + } + } + } + + # Display the missing groups in the output textbox + if ($missingGroups.Count -gt 0) { + $missingGroupsText = $missingGroups -join "`r`n" + $textBoxOutput.Text = "Groups missing for $primaryUserUPN compared to the other users:`r`n`r`n$missingGroupsText" + } else { + $textBoxOutput.Text = "No missing groups found for $primaryUserUPN." + } + } +}) + +# Event handler for the Help button +$buttonHelp.Add_Click({ + # Display usage instructions in a new window + $helpForm = New-Object Windows.Forms.Form + $helpForm.Text = "Usage Instructions" + $helpForm.Width = 400 + $helpForm.Height = 350 + + $helpLabel = New-Object Windows.Forms.Label + $helpLabel.Text = "Instructions:" + $helpLabel.Location = New-Object Drawing.Point(10, 10) + $helpForm.Controls.Add($helpLabel) + + $helpText = @" +1. Enter the UPN of the primary user in the 'Primary User UPN' field *@wd.govt.nz or *@mbie.govt.nz. + +2. Select the number of users to compare against from the dropdown 1, 2 or 3. + +3. Enter the UPNs of the users to compare against in the 'Users to Compare Against' section *@wd.govt.nz or *@mbie.govt.nz. + +4. Click the 'Compare' button to compare group memberships that the primary user is missing. + +5. The results will be displayed in the textbox below. Find the membership that is most likely what they need and apply this in AD. + +6. If this is an admin account, Please supply this information to Wintel via the ticket to be actioned accordingly. +"@ + $helpTextBox = New-Object Windows.Forms.TextBox + $helpTextBox.Location = New-Object Drawing.Point(10, 40) + $helpTextBox.Width = 360 + $helpTextBox.Height = 250 + $helpTextBox.Multiline = $true + $helpTextBox.ScrollBars = "Vertical" + $helpTextBox.Text = $helpText + $helpTextBox.ReadOnly = $true + $helpForm.Controls.Add($helpTextBox) + + $helpForm.ShowDialog() +}) + +# Show the form +$form.ShowDialog() + +# Disclaimer: TWFkZSBieSBaYWsgQmVhcm1hbiAtIERhdGFjb20gV2ludGVsIC0gUHJvdmlkZWQgYXMgaXMgd2hlcmUgaXMuIElmIGJyb2tlbiB3aWxsIGJlIGxvb2tlZCBhdCBpbiBhIGJlc3QgZWZmb3J0IGF0dGVtcHQ= \ No newline at end of file