Wednesday, December 22, 2021

Citrix Users

Function CurrentlyLoggedOnUser {

    Param([string]$computername=$(Throw "You must specify a computername."))

    $explorerprocesses = @(Get-WmiObject -ComputerName $computername -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)

    if ($explorerprocesses.Count -eq 0)

    {

        #[System.Windows.Forms.MessageBox]::Show("No explorer process found / Nobody interactively logged on", "Loggedon User", "OK", "Information")

    } else {

        $Username = ""

        foreach ($i in $explorerprocesses)

        {

            $Username = $i.GetOwner().User

            $Domain = $i.GetOwner().Domain

            #$Domain + "\" + $Username + " logged on since: " + ($i.ConvertToDateTime($i.CreationDate))

            $CurrentlyLoggedOnUser = $Username

            $CurrentlyLoggedOnUser

            #[System.Windows.Forms.MessageBox]::Show($Domain + "\" + $Username + " logged on since: " + ($i.ConvertToDateTime($i.CreationDate)), "Loggedon User", "OK", "Information")

        }

    }

}


Function Search_DN_User ($SAMName){

    $root = [ADSI]''

    $searcher = new-object     System.DirectoryServices.DirectorySearcher($root)

    $searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName*))"

    $user = $searcher.findall()


    if ($user)

    {     

        return $user

    }

    else

    {

        return $false

    }

}


Function Check_Computer_Input {

    #$global:CompTxt = $False

    $global:computernameList = @()

    $TextBoxListCN_New = $TextBoxListCN.Text -replace '"', ''

    #Write-Host $TextBoxListCN_New

    If (Test-Path $TextBoxListCN_New){

        #$CompTxt = $True

        $DropDownCN.Enabled = $false

        #$DropDownCN.Enabled = $False

        $global:ComputerNameList = Get-Content "$TextBoxListCN_New" -ea stop #| sort 

        return $true

    }

    Else{

        $global:computernameList = $DropDownCN.Text

        $DropDownCN.Enabled = $True

        If ($global:computernameList){

            return $true

        }

        else {

            return $false

        }

    }

}


<#

Function Get_DeliveryGroup_Users($computername) {

    $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue

    $DeliveryGroupName = $Machine.DesktopGroupName

    #write-host $DeliveryGroupName

    [array]$IncludedUsers = Get-BrokerAccessPolicyRule -DesktopGroupName "$DeliveryGroupName" |select IncludedUsers -Unique #| Out-String -Width 10000 

    $IncludedUsers = $IncludedUsers.IncludedUsers.Name

    $IncludedUsers = $IncludedUsers -replace "$Domain\\", ""

    return $IncludedUsers[0]

}

#>


Function Get_DeliveryGroup_Users($computername, $CitrixUser) {

    $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue

    $DeliveryGroupName = $Machine.DesktopGroupName

    $DelGrpADListDflt = @()

    $DelGrpADList = @()

    $DelGrpADListDflt = New-Object PSObject

    $DelGrpADListDflt | Add-Member NoteProperty "Name" " Add directly to Delivery Group"


    [array]$DelGrpADList =  Get-BrokerAccessPolicyRule -DesktopGroupName $DeliveryGroupName |ForEach-Object {$_.IncludedUsers} | select Name -Unique

    $DelGrpADList += $DelGrpADListDflt

    #Write-Host "UseSameDelGrpName: " $global:UseSameDelGrpName

    If (($global:UseSameDelGrpName -ne "YES") -and ($global:MultipleCitrixUsers -ne "YES")){

    

    $global:result = $DelGrpADList | Sort-Object Name| Out-GridView -PassThru -Title "'$DeliveryGroupName' - Members"

        If (($global:UseSameDelGrpNameNoShow -ne "YES") -and ($ComputerNameList.Count -gt 1)){

        $UseSameDelGrpNameAD = [System.Windows.Forms.MessageBox]::Show("Please select 'Yes' if the machines belongs to Same Delivery Group, 'NO' if machies are belongs to different Delivery groups", "$DeliveryGroupName", "YesNO", "Information")

           # Write-Host $UseSameDelGrpNameAD

            If($UseSameDelGrpNameAD -match "Yes"){

                #Write-Host "selected yes"

                $global:UseSameDelGrpName = "YES"

                #Write-Host "UseSameDelGrpName: " $global:UseSameDelGrpName

            }

            <#elseIf(($UseSameDelGrpNameAD -match "No") -and ($global:MultipleCitrixUsers -ne "YES")){

                $global:UseSameDelGrpName = "YES"

            }#>

        }

    }

    If($global:result -match "@{Name="){

        $global:result = $global:result.Name

    }

    else{

        $global:result = $global:result

    }


    If ($global:result -eq " Add directly to Delivery Group"){

        #Write-Host "Default"

        $IncludeUser = "$Domain\$CitrixUser"

        $DelGrpNames = Get-BrokerAccessPolicyRule -DesktopGroupName $DeliveryGroupName | ForEach-Object {$_.Name}

        foreach ($DelGrpName in $DelGrpNames){

            #Write-Host $DelGrpName

            Set-BrokerAccessPolicyRule -AdminAddress "<ADMINADDRESS>" -Name $DelGrpName -AddIncludedUsers @($IncludeUser)

        }

        Write-Host $ProcessIncludeUser

        If ($?) {

            $outAddList.AddDelGrpSuccess = $CitrixUser

        }

        else {

            Write-Warning "$($error[0])" 

            $outAddList.AddDelGrpFail = $CitrixUser

            

        }

        return $null

    }

    else{

        #Write-Host "Else: " $global:result.Name

        $global:result = $global:result -replace "$Domain\\", ""

        return $global:result

    }

}


Function Add_Citrix_Users ($Computername, $CitrixUser) {

    $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue


    If($Machine ){

        Try {

            Add-BrokerUser -AdminAddress "<ADMINADDRESS>" -Name "$Domain\$CitrixUser" -Machine "$Domain\$computername"

            $outAddList.AddCitrixSuccess = $CitrixUser

        }

        catch {

            Write-Warning "$($error[0])"

            $outAddList.AddCitrixFail = $CitrixUser

        }

        #$DeliveryGroupUsers = Get_DeliveryGroup_Users $Computername

        $DeliveryGroupUsers = Get_DeliveryGroup_Users $Computername $CitrixUser

        

        If($DeliveryGroupUsers){

            $ADGrpMembers = Get-ADGroupMember $DeliveryGroupUsers | select samaccountname | Sort-Object samaccountname | where {$_ -eq $CitrixUser}

           

            If ($ADGrpMembers){

            }

            else {

                Try {

                   

                    Add-ADGroupMember -Identity $DeliveryGroupUsers -Members $CitrixUser

                    $outAddList.AddAdSuccess = $CitrixUser

                }

                catch {

                    If ($($error[0]) -match "already a member") {

                        Write-Warning "$($error[0])" 

                        $outAddList.AlreadyADMember = $CitrixUser

                    }

                    else {

                        Write-Warning "$($error[0])"

                        $outAddList.AddAdFail = $CitrixUser

                    }

                }

            }

        }

        

    }

    Else{

        [System.Windows.Forms.MessageBox]::Show("Computer Name does not exist", "Citrix Users", "OK", "Warning")

    }

}


Function Add_Admin_Users ($Computername, $AdminUser) {

    $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue

    $Trustee = 'WinNT://',$Domain,'/',$AdminUser -join ''

    If($Machine ){

        Try {

            #Add-BrokerUser -AdminAddress "<ADMINADDRESS>" -Name "$Domain\$CitrixUser" -Machine "$Domain\$computername"

            ([ADSI]"WinNT://$Computername/Administrators,group").add($Trustee)

            $outAddAdminList.AddAdminSuccess = $AdminUser

        }

        catch {

            Write-Warning "$($error[0])"

            $outAddAdminList.AddAdminFail = $AdminUser

        }

        

    }

    Else{

        [System.Windows.Forms.MessageBox]::Show("Computer Name does not exist", "Citrix Users", "OK", "Warning")

    }

}


Function Delete_Citrix_Users ($Computername, $CitrixUser) {

    $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue

    If($Machine ){

        Try {

            Remove-BrokerUser -AdminAddress "<ADMINADDRESS>" -Name "$Domain\$CitrixUser" -Machine "$Domain\$computername"

            $outDelList.DelCitrixSuccess = $CitrixUser

        }

        Catch {

            Write-Warning "$($error[0])"

            $outDelList.DelCitrixFail = $CitrixUser

        }


        If ($DelAdDelGrpCheckBox.Checked){ 

            $DeliveryGroupUsers = Get_DeliveryGroup_Users $Computername

            If($DeliveryGroupUsers){

                $ADGrpMembers = Get-ADGroupMember $DeliveryGroupUsers | select samaccountname | Sort-Object samaccountname | where {$_ -match $CitrixUser}

                If ($ADGrpMembers){

                    Try{

                        remove-ADGroupMember -Identity $DeliveryGroupUsers -Members $CitrixUser -Confirm:$false

                        $outDelList.DelAdSuccess = $CitrixUser

                    }

                    Catch{

                        Write-Warning "$($error[0])"

                        $outDelList.DelAdFail = $CitrixUser

                    }

                }

                else {

                }

            }

        }

        

    }

    Else{

        [System.Windows.Forms.MessageBox]::Show("Computer Name does not exist", "Citrix Users", "OK", "Warning")

    }

}




Function GenerateProfilesForm {

    #Param([string]$computername=$(Throw "You must specify a computername."))

    

    Add-Type -AssemblyName System.Windows.Forms

    Add-PSSnapin Citrix*

    Import-Module ActiveDirectory


    [array]$ComputerNameArray = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MaxRecordCount 2147483647 -Filter { RegistrationState -eq 'Registered' -and HostedMachineName -ne $null } | Select HostedMachineName 


    #Create Form Objects

    $Form = New-Object system.Windows.Forms.Form

    $LabelCN = New-Object System.Windows.Forms.Label

    $LabelUD = New-Object System.Windows.Forms.Label

    $LabelUA = New-Object System.Windows.Forms.Label

    $LabelUAA = New-Object System.Windows.Forms.Label

    $ProgressLabel= New-Object System.Windows.Forms.Label

    $DelBtn = New-Object System.Windows.Forms.Button

    $AddBtn = New-Object System.Windows.Forms.Button

    $AddAdminBtn = New-Object System.Windows.Forms.Button

    $AddOkBtn = New-Object System.Windows.Forms.Button

    $AddAdminOkBtn = New-Object System.Windows.Forms.Button

    $SelectAllDelBtn = New-Object System.Windows.Forms.Button

    $ClearAllDelBtn = New-Object System.Windows.Forms.Button

    $RefreshBtn = New-Object System.Windows.Forms.Button

    $TextBoxCN = New-Object System.Windows.Forms.TextBox

    $TextBoxListCN = New-Object System.Windows.Forms.TextBox

    $DropDownCN = new-object System.Windows.Forms.ComboBox

    $TextBoxUN = New-Object System.Windows.Forms.TextBox

    $TextBoxUNA = New-Object System.Windows.Forms.TextBox

    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

    $TabControl = New-object System.Windows.Forms.TabControl

    $DeleteUsersPage = New-Object System.Windows.Forms.TabPage

    $AddUsersPage = New-Object System.Windows.Forms.TabPage

    $AddAdminUsersPage = New-Object System.Windows.Forms.TabPage

    $AddUsersPanel = New-Object System.Windows.Forms.Panel

    $AddAdminUsersPanel = New-Object System.Windows.Forms.Panel

    $DeleteUsersPanel = New-Object System.Windows.Forms.Panel



    $handler_RefreshBtn_Click = {

        If ($TabControl.SelectedTab.Name -eq "DeleteUsers") {

            If (Check_Computer_Input){

                $i = 0

                $form.Refresh()

                $ProgressLabel.Text = "Progress Status"

                $handler_ClearAllDelBtn_Click.Invoke()

                $handler_DelAllDelBtn_Click.Invoke()

                

                $DelAdDelGrpCheckBox.Enabled = $True


                $global:Delusers = @()

                $global:DelCheckBoxes = @()


                Foreach ($computername in $ComputerNameList){

                    Try {   

                        

                            $Machine = Get-BrokerMachine -AdminAddress "<ADMINADDRESS>" -MachineName "$Domain\$computername" -ErrorAction SilentlyContinue

                            If($Machine ){

                                $global:Delusers += (Get-BrokerUser -AdminAddress "<ADMINADDRESS>" -MachineUid $Machine.Uid).Name

                                

                            }

                            Else{

                                [System.Windows.Forms.MessageBox]::Show("Computer Name: $Computername does not exist ", "Citrix Users", "OK", "Warning")

                            }

                        }  

                    Catch {  

                            Write-Warning "$($error[0]) "  

                            #Break  

                        }

                }

                

                $Delusers = $global:Delusers | select -Unique

                    If($Delusers){

                         $Delusers| %{

                             $global:DelCheckBoxes += New-Object System.Windows.Forms.CheckBox

                             $global:DelCheckBoxes[-1].UseVisualStyleBackColor = $True

                             $global:DelCheckBoxes[-1].Text = ($_).replace('$Domain\','')

                             $global:DelCheckBoxes[-1].AutoSize = $True

                             $global:DelCheckBoxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0

                             $global:DelCheckBoxes[-1].Location = New-Object System.Drawing.Point((10),(10 + 20*$i))

                             $DeleteUsersPanel.Controls.Add($global:DelCheckBoxes[-1])

                             $i++

                             }

                        }

            }

            else{

                [System.Windows.Forms.MessageBox]::Show("Computer Name is empty, please type computer name and click on Refresh button", "Citrix Users", "OK", "Warning")

            }

        }


        If ($TabControl.SelectedTab.Name -eq "AddUsers") {

            $TextBoxUN.Text = ""

            $handler_ClearAddokBtn_Click.Invoke()

        }


        If ($TabControl.SelectedTab.Name -eq "AddAdminUsers") {

            $TextBoxUNA.Text = ""

            $handler_ClearAddAdminokBtn_Click.Invoke()

        }


     }


    $handler_SelectAllDelBtn_Click = {

        foreach ($DelCheckBox in $DelCheckBoxes) {

            $DelCheckBox.Checked = $True

        }

    }


    $handler_ClearAllDelBtn_Click = {

        foreach ($DelCheckBox in $DelCheckBoxes) {

            $DelCheckBox.Checked = $False

        }

    }


    $handler_ClearAddokBtn_Click = {


        foreach ($AddCheckBox in $AddCheckBoxes) {

            $AddCheckBox.Checked = $False

            $AddUsersPanel.Controls.Remove($AddCheckBox)

        }

    }


    $handler_ClearAddAdminokBtn_Click = {


        foreach ($AddAdminCheckBox in $AddAdminCheckBoxes) {

            $AddAdminCheckBox.Checked = $False

            $AddAdminUsersPanel.Controls.Remove($AddAdminCheckBox)

        }

    }


    $handler_DelAllDelBtn_Click = {


        foreach ($DelCheckBox in $DelCheckBoxes) {

            $DeleteUsersPanel.Controls.Remove($DelCheckBox)

        }

    }


    $handler_DelBtn_Click = {


        $DelCount = 0

        $DelSuccUsersList = @()

        $DelFailUsersList = @()


        $outDelListTemplate = New-Object PSObject

        $outDelListTemplate| Add-Member Noteproperty "MachineName" ""

        $outDelListTemplate| Add-Member Noteproperty "DelCitrixSuccess" ""

        $outDelListTemplate| Add-Member Noteproperty "DelCitrixFail" ""

        $outDelListTemplate| Add-Member Noteproperty "DelADSuccess" ""

        $outDelListTemplate| Add-Member Noteproperty "DelADFail" ""

        $outDelListTemplate| Add-Member Noteproperty "NotAMember" ""


        foreach ($DelCheckBox in $DelCheckBoxes) {

            If ($DelCheckBox.Checked){ $DelCount++ }

        }


        If ($DelCount -eq 0){

            [System.Windows.Forms.MessageBox]::Show("You have not checked any user, please check one", "Citrix Users", "OK", "Warning")

        }

        Else{

            #$CurrentlyLoggedOnUser = CurrentlyLoggedOnUser $computername

            $DelBtn.Enabled = $False

            $SelectAllDelBtn.Enabled = $False

            $ClearAllDelBtn.Enabled = $False

            $outDelListGrid = @()

            Foreach ($computername in $ComputerNameList){

                $DelSuccUsersList = @()

                $DelFailUsersList = @()

                foreach ($DelCheckBox in $DelCheckBoxes) {

                    If ($DelCheckBox.Checked){


                        $outDelList = $outDelListTemplate | Select-Object *

                        $outDelList.Machinename = $computername



                        $DeluserName = $Delcheckbox.Text

                        $ProgressLabel.Text = "Deleting $DeluserName"


                        $ResultDel = Delete_Citrix_Users $ComputerName $DeluserName

                    


                       $outDelListGrid += $outDelList

                    }

                }



            }

            $handler_RefreshBtn_Click.Invoke()

            $DelBtn.Enabled = $True

            $SelectAllDelBtn.Enabled = $True

            $ClearAllDelBtn.Enabled = $True

            $outDelListGrid | Out-GridView -Title "Citrix Delete Users"  

            $DelAdDelGrpCheckBox.Checked = $False

            $DelAdDelGrpCheckBox.Enabled = $False     

        }

        

        $ProgressLabel.Text = "Progress Status"

    }


    $handler_AddOkBtn_Click = {


        $AddOkCount = 0

        $global:UseSameDelGrpName = ""

        $global:result = ""

        $global:UseSameDelGrpNameNoShow = ""


        #Create a templae object


        $outAddListTemplate = New-Object PSObject

        $outAddListTemplate| Add-Member Noteproperty "MachineName" ""

        $outAddListTemplate| Add-Member Noteproperty "AddCitrixSuccess" ""

        $outAddListTemplate| Add-Member Noteproperty "AddCitrixFail" ""

        $outAddListTemplate| Add-Member Noteproperty "AddADSuccess" ""

        $outAddListTemplate| Add-Member Noteproperty "AddADFail" ""

        $outAddListTemplate| Add-Member Noteproperty "AddDelGrpSuccess" ""

        $outAddListTemplate| Add-Member Noteproperty "AddDelGrpFail" ""

        $outAddListTemplate| Add-Member Noteproperty "AlreadyADMember" ""



        foreach ($AddCheckBox in $AddCheckBoxes) {

            If ($AddCheckBox.Checked){ $AddOkCount++ }

        }


        If ($AddOkCount -eq 0){

            [System.Windows.Forms.MessageBox]::Show("You have not checked any user, please check one", "Citrix Users", "OK", "Warning")

        }

        Else{

            $outAddListGrid = @()

            $AddOkBtn.Enabled = $False

            $AddBtn.Enabled = $False

            $ComputerNameCount = 1

            Foreach ($computername in $ComputerNameList){

                $global:MultipleCitrixUsers = ""

                foreach ($AddCheckBox in $AddCheckBoxes) {

                    If ($AddCheckBox.Checked){

                        

                        $outAddList = $outAddListTemplate | Select-Object *

                        $outAddList.Machinename = $computername

                        


                        $AdduserName = $AddCheckBox.Text

                        $ProgressLabel.Text = "Adding $AdduserName"

                        $ResultAdd = Add_Citrix_Users $computername $AdduserName

                        $global:MultipleCitrixUsers = "YES"

                        $outAddListGrid += $outAddList

                    }

                }

                $ComputerNameCount++

                #Write-Host "Count++: " $ComputerNameCount

                If($ComputerNameCount -ge $ComputerNameList.Count){

                    #Write-Host "count: $ComputerNameCount noshow yes"

                    $global:UseSameDelGrpNameNoShow = "YES"

                }

                

            }

            $handler_RefreshBtn_Click.Invoke()

            $AddBtn.Enabled = $True

            $outAddListGrid | Out-GridView -Title "Citrix Add Users"

        }

        


        $ProgressLabel.Text = "Progress Status"

    }


    $handler_AddBtn_Click = {


        $handler_ClearAddokBtn_Click.Invoke()

        If (Check_Computer_Input){

            $global:Addusers = $TextBoxUN.Text

            [array]$Addusers = $global:Addusers.Split([Environment]::NewLine)

            $AddCount = 0

            $j = 0

            $AddUsersList = @()

            $DoesNotExistUserList = @()

            $global:AddCheckBoxes = @()

            foreach ($Adduser in $Addusers) {

                If ($Adduser){

                    $AddCount++

                }

            }


            If ($AddCount -eq 0){

                [System.Windows.Forms.MessageBox]::Show("You have not input any user, please check one", "Citrix Users", "OK", "Warning")

            }

            Else{


                #$CurrentlyLoggedOnUser = CurrentlyLoggedOnUser $computername


                foreach ($Adduser in $Addusers) {

                    If ($Adduser){

                        $Adduser = $Adduser.trim()

                        $ProgressLabel.Text = "Adding $Adduser"


                        $AdduserCheck = Search_DN_User $Adduser


                        If ($AdduserCheck){

                        

                            $AddUsersList += $AdduserCheck.Properties.samaccountname

                        }

                        Else

                        {


                            $DoesNotExistUserList += $Adduser

                        }



                    

                    }

                }


                $AddUsersList = $AddUsersList | select -Unique

                    $AddUsersList| %{

                            $global:AddCheckBoxes += New-Object System.Windows.Forms.CheckBox

                            $global:AddCheckBoxes[-1].UseVisualStyleBackColor = $True

                            $global:AddCheckBoxes[-1].Text = ($_)#.replace('$Domain\','')

                            $global:AddCheckBoxes[-1].AutoSize = $True

                            $global:AddCheckBoxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0

                            $global:AddCheckBoxes[-1].Location = New-Object System.Drawing.Point((10),(10 + 20*$j))

                            $AddUsersPanel.Controls.Add($global:AddCheckBoxes[-1])

                            $j++

                            }

                $AddOkBtn.Enabled = $True


            }

            $ProgressLabel.Text = "Progress Status"

        }

        else{

            [System.Windows.Forms.MessageBox]::Show("Computer Name is empty, please type computer name and click on Refresh button", "Citrix Users", "OK", "Warning")

        }

    }


    $handler_AddAdminOkBtn_Click = {


        $AddAdminOkCount = 0


        #Create a templae object


        $outAddAdminListTemplate = New-Object PSObject

        $outAddAdminListTemplate| Add-Member Noteproperty "MachineName" ""

        $outAddAdminListTemplate| Add-Member Noteproperty "AddAdminSuccess" ""

        $outAddAdminListTemplate| Add-Member Noteproperty "AddAdminFail" ""

        $outAddAdminListTemplate| Add-Member Noteproperty "AlreadyAdminMember" ""



        foreach ($AddAdminCheckBox in $AddAdminCheckBoxes) {

            If ($AddAdminCheckBox.Checked){ $AddAdminOkCount++ }

        }


        If ($AddAdminOkCount -eq 0){

            [System.Windows.Forms.MessageBox]::Show("You have not checked any user, please check one", "Admin Users", "OK", "Warning")

        }

        Else{

            $outAddAdminListGrid = @()

            $AddAdminOkBtn.Enabled = $False

            $AddAdminBtn.Enabled = $False

            

            Foreach ($computername in $ComputerNameList){


                foreach ($AddAdminCheckBox in $AddAdminCheckBoxes) {

                    If ($AddAdminCheckBox.Checked){

                        

                        $outAddAdminList = $outAddAdminListTemplate | Select-Object *

                        $outAddAdminList.Machinename = $computername

                        


                        $AddAdminuserName = $AddAdminCheckBox.Text

                        $ProgressLabel.Text = "Adding $AddAdminuserName"

                        $ResultAddAdmin = Add_Admin_Users $computername $AddAdminuserName


                        $outAddAdminListGrid += $outAddAdminList

                    }

                }


                

            }

            $handler_RefreshBtn_Click.Invoke()

            $AddAdminBtn.Enabled = $True

            $outAddAdminListGrid | Out-GridView -Title "Admin Add Users"

        }

        


        $ProgressLabel.Text = "Progress Status"

    }


    $handler_AddAdminBtn_Click = {


        $handler_ClearAddAdminokBtn_Click.Invoke()

        If (Check_Computer_Input){

            $global:AddAdminusers = $TextBoxUNA.Text

            [array]$AddAdminusers = $global:AddAdminusers.Split([Environment]::NewLine)

            $AddAdminCount = 0

            $k = 0

            $AddAdminUsersList = @()

            $DoesNotExistAdminUserList = @()

            $global:AddAdminCheckBoxes = @()

            foreach ($AddAdminuser in $AddAdminusers) {

                If ($AddAdminuser){

                    $AddAdminCount++

                }

            }


            If ($AddAdminCount -eq 0){

                [System.Windows.Forms.MessageBox]::Show("You have not input any user, please check one", "Admin Users", "OK", "Warning")

            }

            Else{


                #$CurrentlyLoggedOnUser = CurrentlyLoggedOnUser $computername


                foreach ($AddAdminuser in $AddAdminusers) {

                    If ($AddAdminuser){

                        $AddAdminuser = $AddAdminuser.trim()

                        $ProgressLabel.Text = "Adding $AddAdminuser"


                        $AddAdminuserCheck = Search_DN_User $AddAdminuser


                        If ($AddAdminuserCheck){

                        

                            $AddAdminUsersList += $AddAdminuserCheck.Properties.samaccountname

                        }

                        Else

                        {


                            $DoesNotExistAdminUserList += $AddAdminuser

                        }



                    

                    }

                }


                $AddAdminUsersList = $AddAdminUsersList -match "-" | select -Unique 

                    $AddAdminUsersList| %{

                            $global:AddAdminCheckBoxes += New-Object System.Windows.Forms.CheckBox

                            $global:AddAdminCheckBoxes[-1].UseVisualStyleBackColor = $True

                            $global:AddAdminCheckBoxes[-1].Text = ($_)#.replace('$Domain\','')

                            $global:AddAdminCheckBoxes[-1].AutoSize = $True

                            $global:AddAdminCheckBoxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0

                            $global:AddAdminCheckBoxes[-1].Location = New-Object System.Drawing.Point((10),(10 + 20*$k))

                            $AddAdminUsersPanel.Controls.Add($global:AddAdminCheckBoxes[-1])

                            $k++

                            }

                $AddAdminOkBtn.Enabled = $True


            }

            $ProgressLabel.Text = "Progress Status"

        }

        else{

            [System.Windows.Forms.MessageBox]::Show("Computer Name is empty, please type computer name and click on Refresh button", "Admin Users", "OK", "Warning")

        }

    }


     $OnLoadForm_StateCorrection=

     {#Correct the initial state of the form to prevent the .Net maximized form issue

     $form.WindowState = $InitialFormWindowState

     }


    #Form Vaiable

    $Form.Text = "User Profiles"

    $Form.AutoSize = $True


    #Label1 variables

    $LabelCN.Text = "Computer Name"

    $LabelCN.Location = New-Object System.Drawing.Point(10,10)

    $LabelCN.AutoSize = $True

    $Form.Controls.Add($LabelCN)



    #TextBox1 variables

    #$TextBoxCN.Text = ""

    #$TextBoxCN.AutoSize = $True

    #$TextBoxCN.Location = New-Object System.Drawing.Point(10,30)

    #$Form.Controls.Add($TextBoxCN)


    #Dropdown variables

    #$DropDownCN.Text = 'Select Computer Name'

    $DropDownCN.Size = New-Object System.Drawing.Size(150,20)

    #$DropDownCN.AutoSize = $True

    $DropDownCN.Location = New-Object System.Drawing.Point(10,30)

    ForEach ($Item in $ComputerNameArray) {

     [void] $DropDownCN.Items.Add($Item.HostedMachineName)

    }

    $Form.Controls.Add($DropDownCN)



    #TextBox1 variables

    $TextBoxListCN.Text = 'Please provide the .txt file'

    $TextBoxListCN.Location = New-Object System.Drawing.Point(10,60)

    $TextBoxListCN.Size = New-Object System.Drawing.Size(220,20)

    $Form.Controls.Add($TextBoxListCN)


    #ProgressLabel variables

    #$ProgressLabel.Text = "Progress Status"

    #$ProgressLabel.Location = New-Object System.Drawing.Point(10,60)

    #$ProgressLabel.AutoSize = $True

    #$Form.Controls.Add($ProgressLabel)


    #RefreshBtn variables

    $RefreshBtn.Text = "Refresh"

    $RefreshBtn.AutoSize = $True

    $RefreshBtn.Location = New-Object System.Drawing.Point(180,20)

    $RefreshBtn.add_Click($handler_RefreshBtn_Click)

    $Form.Controls.Add($RefreshBtn)


    $TabControl.DataBindings.DefaultDataSourceUpdateMode = 0

    $TabControl.Location = New-Object System.Drawing.Point(10,90)

    $TabControl.Name = "TabControl"

    $TabControl.Size = New-Object System.Drawing.Size(360,260)

    $Form.Controls.Add($TabControl)


    $DeleteUsersPage.DataBindings.DefaultDataSourceUpdateMode = 0

    $DeleteUsersPage.UseVisualStyleBackColor = $True

    $DeleteUsersPage.Name = "DeleteUsers"

    $DeleteUsersPage.Text = "Delete Users”

    $TabControl.Controls.Add($DeleteUsersPage)


    $DeleteUsersPanel.Size = new-object System.Drawing.Size(200,200)

    $DeleteUsersPanel.AutoScroll = $True

    $DeleteUsersPanel.Location = New-Object System.Drawing.Point(10,30)

    $DeleteUsersPanel.BackColor = "LightBlue"

    $DeleteUsersPage.Controls.Add($DeleteUsersPanel)


    $AddUsersPage.DataBindings.DefaultDataSourceUpdateMode = 0

    $AddUsersPage.UseVisualStyleBackColor = $True

    $AddUsersPage.Name = "AddUsers"

    $AddUsersPage.Text = "Add Users”

    $TabControl.Controls.Add($AddUsersPage)


    $AddAdminUsersPage.DataBindings.DefaultDataSourceUpdateMode = 0

    $AddAdminUsersPage.UseVisualStyleBackColor = $True

    $AddAdminUsersPage.Name = "AddAdminUsers"

    $AddAdminUsersPage.Text = "Add Admin Users”

    $TabControl.Controls.Add($AddAdminUsersPage)



    #Label1 variables

    $LabelUD.Text = "Select the Users to Delete"

    $LabelUD.Location = New-Object System.Drawing.Point(10,10)

    $LabelUD.AutoSize = $True

    $DeleteUsersPage.Controls.Add($LabelUD)



    #DelBtn variables

    $DelBtn.Text = "Delete"

    $DelBtn.AutoSize = $True

    $DelBtn.Location = New-Object System.Drawing.Point(220,20)

    $DelBtn.add_Click($handler_DelBtn_Click)

    $DeleteUsersPage.Controls.Add($DelBtn)


    #SelectAllBtn variables

    $SelectAllDelBtn.Text = "Select All"

    $SelectAllDelBtn.AutoSize = $True

    $SelectAllDelBtn.Location = New-Object System.Drawing.Point(220,60)

    $SelectAllDelBtn.add_Click($handler_SelectAllDelBtn_Click)

    $DeleteUsersPage.Controls.Add($SelectAllDelBtn)


    #ClearAllBtn variables

    $ClearAllDelBtn.Text = "Clear All"

    $ClearAllDelBtn.AutoSize = $True

    $ClearAllDelBtn.Location = New-Object System.Drawing.Point(220,100)

    $ClearAllDelBtn.add_Click($handler_ClearAllDelBtn_Click)

    $DeleteUsersPage.Controls.Add($ClearAllDelBtn)


    $DelAdDelGrpCheckBox = New-Object System.Windows.Forms.CheckBox

    $DelAdDelGrpCheckBox.UseVisualStyleBackColor = $True

    $DelAdDelGrpCheckBox.Text = "Delete Users from`r`nAD Group assigned to`r`nDelivery Group"

    $DelAdDelGrpCheckBox.AutoSize = $True

    #$DelAdDelGrpCheckBox.DataBindings.DefaultDataSourceUpdateMode = 0

    $DelAdDelGrpCheckBox.Location = New-Object System.Drawing.Point(220,140)

    $DeleteUsersPage.Controls.Add($DelAdDelGrpCheckBox)

    $DelAdDelGrpCheckBox.Enabled = $False


    $LabelUA.Text = "Type the user name and click add"

    $LabelUA.Location = New-Object System.Drawing.Point(10,10)

    $LabelUA.AutoSize = $True

    $AddUsersPage.Controls.Add($LabelUA)


    $TextBoxUN.Text = ""

    $TextBoxUN.AutoSize = $True

    $TextBoxUN.Multiline = $True

    $TextBoxUN.Location = New-Object System.Drawing.Point(10,30)

    $TextBoxUN.Size = New-Object System.Drawing.Size(120,190)

    $AddUsersPage.Controls.Add($TextBoxUN)


    #AddBtn variables

    $AddBtn.Text = "Add"

    $AddBtn.AutoSize = $True

    $AddBtn.Location = New-Object System.Drawing.Point(220,20)

    $AddBtn.add_Click($handler_AddBtn_Click)

    $AddUsersPage.Controls.Add($AddBtn)


    $AddUsersPanel.Size = new-object System.Drawing.Size(200,140)

    $AddUsersPanel.AutoScroll = $True

    $AddUsersPanel.Location = New-Object System.Drawing.Point(140,50)

    $AddUsersPanel.BackColor = "LightBlue"

    $AddUsersPage.Controls.Add($AddUsersPanel)


    #AddOKBtn variables

    $AddOkBtn.Text = "Ok"

    $AddOkBtn.AutoSize = $True

    $AddOkBtn.Location = New-Object System.Drawing.Point(200,200)

    $AddOkBtn.add_Click($handler_AddOkBtn_Click)

    $AddUsersPage.Controls.Add($AddOkBtn)

    $AddOkBtn.Enabled = $false


    $LabelUAA.Text = "Type the user name and click add"

    $LabelUAA.Location = New-Object System.Drawing.Point(10,10)

    $LabelUAA.AutoSize = $True

    $AddAdminUsersPage.Controls.Add($LabelUAA)


    $TextBoxUNA.Text = ""

    $TextBoxUNA.AutoSize = $True

    $TextBoxUNA.Multiline = $True

    $TextBoxUNA.Location = New-Object System.Drawing.Point(10,30)

    $TextBoxUNA.Size = New-Object System.Drawing.Size(120,190)

    $AddAdminUsersPage.Controls.Add($TextBoxUNA)


    #AddBtn variables

    $AddAdminBtn.Text = "Add"

    $AddAdminBtn.AutoSize = $True

    $AddAdminBtn.Location = New-Object System.Drawing.Point(220,20)

    $AddAdminBtn.add_Click($handler_AddAdminBtn_Click)

    $AddAdminUsersPage.Controls.Add($AddAdminBtn)


    $AddAdminUsersPanel.Size = new-object System.Drawing.Size(200,140)

    $AddAdminUsersPanel.AutoScroll = $True

    $AddAdminUsersPanel.Location = New-Object System.Drawing.Point(140,50)

    $AddAdminUsersPanel.BackColor = "LightBlue"

    $AddAdminUsersPage.Controls.Add($AddAdminUsersPanel)


    #AddOKBtn variables

    $AddAdminOkBtn.Text = "Ok"

    $AddAdminOkBtn.AutoSize = $True

    $AddAdminOkBtn.Location = New-Object System.Drawing.Point(200,200)

    $AddAdminOkBtn.add_Click($handler_AddAdminOkBtn_Click)

    $AddAdminUsersPage.Controls.Add($AddAdminOkBtn)

    $AddAdminOkBtn.Enabled = $false

    


     #Save the initial state of the form

     $InitialFormWindowState = $form.WindowState


     #Init the OnLoad event to correct the initial state of the form

     $form.add_Load($OnLoadForm_StateCorrection)


     #Show the Form

     $Form.ShowDialog()

 

 }


 GenerateProfilesForm 

No comments:

Post a Comment