Functions/Public/Start-COVDistroForm.ps1



function Start-COVDistroForm {

    
    #WPF form code

    $inputXML = @"
 
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DL_Form_1"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="1300" FontFamily="Roboto Medium" FontWeight="Medium" WindowStyle="None" Background="#181735" Foreground="White" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
 
        <Grid>
        <ListBox x:Name="AddUserLsT" FontSize="18" HorizontalAlignment="Left" Height="292" Background="Black" Foreground="White" Margin="44,80,0,0" VerticalAlignment="Top" Width="273"/>
        <ListBox Name="CurrentUserLsT" FontSize="18" HorizontalAlignment="Center" Height="292" Background="Black" Foreground="White" Margin="0,80,0,0" VerticalAlignment="Top" Width="272"/>
        <ListBox x:Name="RemoveUserLsT" FontSize="18" HorizontalAlignment="Left" Height="292" Background="Black" Foreground="White" Margin="952,80,0,0" VerticalAlignment="Top" Width="273" />
        <TextBlock HorizontalAlignment="Left" Margin="108,46,0,0" TextWrapping="Wrap" Text="City AD Users" VerticalAlignment="Top" FontSize="20"/>
        <TextBlock HorizontalAlignment="Left" Height="31" Margin="556,44,0,0" TextWrapping="Wrap" Text="DL Current Users" VerticalAlignment="Top" Width="304" FontSize="20" FontWeight="Medium" FontFamily="Leelawadee UI"/>
        <TextBlock HorizontalAlignment="Left" FontSize="20" Margin="1026,46,0,0" TextWrapping="Wrap" Text="Remove Users" VerticalAlignment="Top"/>
        <TextBox x:Name="DLTxT" HorizontalAlignment="Left" Margin="170,530,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125" Height="26"/>
        <Label Content="DL Group Search" HorizontalAlignment="Left" Margin="27,526,0,0" VerticalAlignment="Top" Foreground="#FFDA29" FontSize="16" />
 
        <Button x:Name="CancelBtN" FontFamily="Roboto" Content="Cancel" HorizontalAlignment="Left" Background="#FE6584" Foreground="White" Margin="650,525,0,0" VerticalAlignment="Top" Height="30" Width="92" IsCancel="True">
        <Button.Resources>
              <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="5"/>
              </Style>
          </Button.Resources>
      </Button>
       
       
       
      <Button x:Name="AddUserBtN" Content="---&gt;" HorizontalAlignment="Left" Background="#FE6584" Foreground="White" Margin="370,196,0,0" VerticalAlignment="Top" Height="30" Width="91" RenderTransformOrigin="0.545,0.447">
        <Button.Resources>
              <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="5"/>
              </Style>
          </Button.Resources>
      </Button>
       
       
      <Button x:Name="RemoveUserBtN" Content="---&gt;" HorizontalAlignment="Left" Background="#FE6584" Foreground="White" Margin="815,196,0,0" VerticalAlignment="Top" Height="30" Width="91" RenderTransformOrigin="0.545,0.447">
        <Button.Resources>
              <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="5"/>
              </Style>
          </Button.Resources>
      </Button>
       
       
      <Button x:Name="SearchBtN" Content="Search" HorizontalAlignment="Left" Background="#FE6584" Foreground="White" Margin="324,525,0,0" VerticalAlignment="Top" Height="30" Width="92" IsDefault="True">
        <Button.Resources>
              <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="5"/>
              </Style>
          </Button.Resources>
      </Button>
       
      <Button x:Name="SubmitBtN" FontFamily="Roboto" Content="Submit" HorizontalAlignment="Left" Background="#FE6584" Foreground="White" Margin="526,526,0,0" VerticalAlignment="Top" Height="30" Width="92" IsCancel="True">
        <Button.Resources>
              <Style TargetType="Border">
                  <Setter Property="CornerRadius" Value="5"/>
              </Style>
          </Button.Resources>
      </Button>
        </Grid>
 
</Window>
 
"@


    #End Region Xaml Form


    #Region "Xaml to Powershell code"

    $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    [xml]$XAML = $inputXML
    #Read XAML

    $reader = (New-Object System.Xml.XmlNodeReader $xaml) 
    try {
        $Form2 = [Windows.Markup.XamlReader]::Load( $reader ) 
    }
    catch [System.Management.Automation.MethodInvocationException] {
        Write-Warning "We ran into a problem with the XAML code. Check the syntax for this control..."
        write-host $error[0].Exception.Message -ForegroundColor Red
        if ($error[0].Exception.Message -like "*button*") {
            write-warning "Ensure your &lt;button in the `$inputXML does NOT have a Click=ButtonClick property. PS can't handle this`n`n`n`n"
        }
    }
    catch {
        #if it broke some other way <img draggable="false" role="img" class="emoji" alt="😀" src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/svg/1f600.svg">
        Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed."
    }

    $xaml.SelectNodes("//*[@Name]") | % { Set-Variable -Name "WPF$($_.Name)" -Value $Form2.FindName($_.Name) }

    Function Get-FormVariables {
        if ($global:ReadmeDisplay -ne $true) { Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow; $global:ReadmeDisplay = $true }
        write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
        get-variable WPF*
    }

    $CityUsers = Get-Aduser -filter * | Sort-Object name | select name -ExpandProperty name
    #Adds the output to the city user listbox on the second form
    $CityUsers | ForEach-Object { $WPFAddUserLsT.Items.Add($_) }


    #### Buttons start region #####

    #Searches for users in the DL group and add's them to the current user list.
    $WPFSearchBtN.add_click({

    


            if ($WPFCurrentUserLsT.items.Count -gt 0 -eq $True) {

                $WPFCurrentUserLsT.Items.Clear()

            }

            $SelectedDL = $WPFDLTxT.Text
            $Users = Get-ADGroupMember $SelectedDL | Sort-Object samaccountname | select name -ExpandProperty name
            $Users | ForEach-Object { $WPFCurrentUserLsT.Items.Add($_) }

        })

    #Moves users from the City AD list to the current users list
    $WPFAddUserBtN.Add_Click({

        
    
    
            if ($WPFAddUserLsT.selecteditem -ne $null) {
    
                while ($WPFAddUserLsT.SelectedItems.Count -gt 0) {
                    $WPFCurrentUserLsT.Items.Add($WPFAddUserLsT.SelectedItem)
                    $WPFAddUserLsT.items.remove($WPFAddUserLsT.SelectedItem)
                }
    
            }
        })  
    
    
    #Remove user button, moves user from the current user list to the remove user list
    $WPFRemoveUserBtN.Add_Click({

       
    
    
            if ($WPFCurrentUserLsT.selecteditems -ne $null) {
    
                while ($WPFCurrentUserLsT.SelectedItems.Count -gt 0) {
                    $WPFRemoveUserLsT.Items.Add($WPFCurrentUserLsT.SelectedItem)
                    $WPFCurrentUserLsT.items.remove($WPFCurrentUserLsT.SelectedItem)
                }
    
            }
        })  

    $WPFSubmitBtN.Add_Click({


            $Users = get-adgroupmember -Identity $WPFDLTxT.text | select name -ExpandProperty name
            #Defining what users to add to the DL
            $Addgroup = foreach ($item in $WPFCurrentUserLsT.Items) {

                get-aduser -filter * | Where name -eq $Item | select samaccountname -ExpandProperty samaccountname
            }

            #Adds Users to DL group
            Add-ADGroupMember -Identity $WPFDLTxT.text -Members $Addgroup 

            #Creating new list to show what users have been added
            $Users2 = get-adgroupmember -Identity $WPFDLTxT.text | select name -ExpandProperty name

            #Remove Users to DL group

        
            if ($WPFRemoveUserLsT.Items -gt 0) {

                $RemoveGroup = foreach ($item in $WPFRemoveUserLsT.Items) {

                    Get-ADuser -filter * | Where name -eq $Item | select samaccountname -ExpandProperty samaccountname
                }
    
                #Removes user from DL group
                Remove-ADGroupMember -Identity $WPFDLTxT.text -members $Removegroup -confirm:$False
    
            }
        


            #LogVariables
            $Date = Get-Date -Format MM-dd-yyyy-hh-mm 
            $Logname = $WPFDLTxT.text + "_" + $Date + ".txt"
            $Logfile = New-Item "\\cvfile01\software\Apps\_NoAppScripts\Powershell\Scriptlogs\DLGroupChanges\$Logname"

            #Adding users that have been removed to logfile
            foreach ($Item in $WPFRemoveUserLsT.items) {

            

                Add-Content $Logfile -Value "###REMOVED### $item" 


            }

            #Comparing the DL before and after the users have been added
            $Difference = Compare-Object -ReferenceObject $Users2 -DifferenceObject $Users | Sort-Object inputobject |  select inputobject -ExpandProperty inputobject

            #New DL user logs
            foreach ($Item in $Difference) {
                Add-Content $Logfile -Value "###ADDED### $item "


            }

            Invoke-Item $Logfile 



        })




    #### Buttons end region #####



    #Displays form
    $Form2.ShowDialog()





}