$Location = Split-Path $MyInvocation.MyCommand.Path -Parent

$Script:ALL_Patch_Servers = @()   
$Script:Responding = @()
$Script:NotResponding = @()
$Global:Collection_Name = ""
$Global:Select_Tool
$Global:Output_Type = ""

If(!(Get-PSDrive -Name SCCM-Drive -ErrorAction SilentlyContinue)) {
    Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
    New-PSDrive -Name SCCM-Drive -PSProvider "AdminUI.PS.Provider\CMSite" -Root "PNCRASCCM001.ccx.carecentrix.com" -Description "SCCM Site"
}
#########################################################################################################################################################
Function Start-Form {

        #$Global:Collection_Name = ""
        #$Global:Select_Tool = ""
        #$Global:Output_Type = ""

        $Temp = ""

        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
        [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

        $Form = New-Object System.Windows.Forms.Form    
        $Form.Size = New-Object System.Drawing.Size(1000,800)  
        ##############################################
        #$Form_Load={ $Script:outputBox.text = "Welcome" }
        ############################################## Start functions
            <#    function pingInfo {

                    if ($RadioButton1.Checked -eq $true) {$nrOfPings=1}
                    if ($RadioButton2.Checked -eq $true) {$nrOfPings=2}
                    if ($RadioButton3.Checked -eq $true) {$nrOfPings=3}

                    $computer=$DropDownBox.SelectedItem.ToString() #populate the var with the value you selected
                    $pingResult=ping $wks -n $nrOfPings | fl | out-string;
                    $Script:outputBox.text=$pingResult
            
                } #end pingInfo
         #>
        ############################################## end functions
        Function SCCM-Module {
            
            If(!(Get-PSDrive).Name -eq "sccm-drive") { 
                Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
                New-PSDrive -Name SCCM-Drive -PSProvider "AdminUI.PS.Provider\CMSite" -Root "PNCRASCCM001.ccx.carecentrix.com" -Description "SCCM Site"
            }#endIf
            CD sccm-drive:
         }#endFunction
         ##############################################
               Function Update-Dropdown1 {
                           # $Collections += $textBox1.text
                            $Collections = @("Collection-Servers","Ping-Collection","Check_Uptime","Test-Service","Remotely Update Policy","Tool6","Tool7")
                            $Collections | % {  $DropDownBox.Items.Add($_) }
                        }
        ##############################################
                Function temp {
                        $outputBox.Clear()
                        #$Script:outputBox.text = $textBox1.text | Out-String
                        $Global:Collection_Name = $textBox1.Text
                        #$Global:Select_Tool = $RadioButton1.Checked
                    
                        $Global:Select_Tool = $DropDownBox.text
                        #$Global:Output_Type = 
                        If ($RadioButton1.Checked -eq $true) { $Global:Output_Type = "Window"; $outputBox.Text = "Radio1 Checked:  $($RadioButton1.Checked)" | Out-String }
                        ElseIf ($RadioButton2.Checked -eq $true) { $Global:Output_Type = "Gridview"; $outputBox.Text = "Radio2 Checked:  $RadioButton2.Checked" | Out-String }
                        ElseIf ($RadioButton3.Checked -eq $true) { $Global:Output_Type = "Export"; $outputBox.Text = "Radio3 Checked:  $RadioButton2.Checked" | Out-String }
                    
                    If($Global:Collection_Name -and $Global:Select_Tool) {
                       # If($DropDownBox.text = "Tool1"){gcc}
                        Run-Tool
                        $textBox1.clear()
                        $DropDownBox.text = "----Select Tool----"
                        Clear-Variables
                    }#endIf
                    Else { #$T = { Write-Host "You must enter Collection Name ............!!" -ForegroundColor Red }
                            $Script:outputBox.SelectionColor = 'red' 
                            $Script:outputBox.Text = "You must select 'Tool' and enter Collection Name............!!"
                    }
                    
                    Run-Defaults
                }#endFunction
        ##############################################
            Function Run-Tool {

                Switch($Global:Select_Tool) {
                       Collection-Servers { Collection-Servers; Write-Host "Tool Selected: $Global:Select_Tool" }
                          Ping-Collection { Ping_Collection }
                             Check_Uptime { Check_Uptime }
                             Test-Service { Test-Service }
                 #"Remotely Update Policy" { $outputBox.Text = Invoke-Expression $Location\Tool-Remotely-Restart-SCCMSyncCycle.ps1 | ft -AutoSize | Out-String }
                 "Remotely Update Policy" { $outputBox.Text = Invoke-Expression $Location\Tool-Remotely-Restart-SCCMSyncCycle.ps1 | ft -AutoSize | Out-String }
                 "Software Update Status" { $outputBox.Text = Invoke-Expression $Location\Tool-Get-SCCMSoftwareUpdateStatus.ps1 | ft -AutoSize | Out-String}
                }#endSwitch
                #Clear-Variables
       }#endFunction
       ##############################################
              Function Run-Defaults {  
                        $Script:textBox1.Text = "Nabil - Test Collection"
                        $Script:RadioButton1.Checked = $true
                    }
       ##############################################                   
              Function gcc {
                    $Script:outputBox.text = Get-Service | Out-String
                 }
        ##############################################
                Function Clear-Variables {
                    $Global:Collection_Name = ""
                    $Global:Select_Tool = ""
                    $Global:Output_Type = ""
                 
                    $Script:ALL_Patch_Servers = @()
                }
#region Controls
        ############################################## Start group boxes
        $groupBox = New-Object System.Windows.Forms.GroupBox
        $groupBox.Location = New-Object System.Drawing.Size(270,20) 
        $groupBox.size = New-Object System.Drawing.Size(150,110) 
        $groupBox.text = "Select Output:" 
        $Form.Controls.Add($groupBox) 
        ############################################## end group boxes

        ############################################## Start radio buttons
        $Script:RadioButton1 = New-Object System.Windows.Forms.RadioButton 
        $Script:RadioButton1.Location = new-object System.Drawing.Point(15,15) 
        $Script:RadioButton1.size = New-Object System.Drawing.Size(130,20) 
        $Script:RadioButton1.Checked = $true 
        $Script:RadioButton1.Text = "Window" 
        $groupBox.Controls.Add($RadioButton1) 

        $Script:RadioButton2 = New-Object System.Windows.Forms.RadioButton
        $Script:RadioButton2.Location = new-object System.Drawing.Point(15,35)
        $Script:RadioButton2.size = New-Object System.Drawing.Size(80,20)
        $Script:RadioButton2.Text = "Gridview"
        $groupBox.Controls.Add($RadioButton2)

        $Script:RadioButton3 = New-Object System.Windows.Forms.RadioButton
        $Script:RadioButton3.Location = new-object System.Drawing.Point(15,55)
        $Script:RadioButton3.size = New-Object System.Drawing.Size(80,20)
        $Script:RadioButton3.Text = "Export csv"
        $groupBox.Controls.Add($RadioButton3)

        ############################################## end radio buttons
        ########### Text Box ###########
        ############Define text box1 for input
        $Script:textBox1 = New-Object “System.Windows.Forms.TextBox”;
        $Script:textBox1.Left = 20;
        $Script:textBox1.Top = 30;
        $Script:textBox1.width = 200;
        $Script:textBox1.Text = "Nabil - Test Collection"
        $Form.Controls.Add($textBox1)
        ############################################## Start drop down boxes

        $Script:DropDownBox = New-Object System.Windows.Forms.ComboBox
        $Script:DropDownBox.Location = New-Object System.Drawing.Size(20,80) 
        $Script:DropDownBox.Size = New-Object System.Drawing.Size(180,20) 
        $Script:DropDownBox.DropDownHeight = 200 
        $Script:DropDownBox.text = "----Select Tool----"
        $Form.Controls.Add($DropDownBox) 
        
        ############################################## end drop down boxes

        <############################################## Start text fields

        $Script:outputBox = New-Object System.Windows.Forms.TextBox 
        $Script:outputBox.Location = New-Object System.Drawing.Size(10,150) 
        $Script:outputBox.Size = New-Object System.Drawing.Size(865,300) 
        $Script:outputBox.MultiLine = $True 

        $Script:outputBox.ScrollBars = "Vertical" 
        $Form.Controls.Add($Script:outputBox) 
        ###########>
        $SCRIPT:outputBox=New-Object System.Windows.Forms.RichTextBox
        $outputBox.Location=New-Object System.Drawing.Size(10,150)
        $outputBox.Size=New-Object System.Drawing.Size(965,600)
        $outputBox.Multiline=$True
        $outputBox.ReadOnly = $True
        $outputBox.BackColor = [Drawing.Color]::White
        $outputBox.ScrollBars = "both"
        $outputBox.WordWrap = $false
        $outputBox.Font = 'Consolas, 8.25pt'
        $form.Controls.Add($Script:outputBox)




        ############################################## end text fields

        ############################################## Start buttons

        $Button = New-Object System.Windows.Forms.Button 
        $Button.Location = New-Object System.Drawing.Size(450,30) 
        $Button.Size = New-Object System.Drawing.Size(110,80) 
        $Button.Text = "Go" 
        $Button.Add_Click({temp}) 
        $Form.Controls.Add($Button) 
#endregion Controls
        ############################################## end buttons
        Update-Dropdown1

        $Form.Add_Shown({$Form.Activate()})
        [void] $Form.ShowDialog()

}
#########################################################################################################################################################
 <#   Function Input-Box {
        [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
        $Global:Collection_Name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "Enter Collection Name")
        
    }
 #>
#############################################################################################################################################################
    Function Display-Results {
        #Param($Global:Output_Type,$Final_Result)
        Write-Host "DisplayType: $($Global:Output_Type)"
            Switch ($Global:Output_Type){
                
                  Window { $Script:outputBox.Text = $Script:ALL_Patch_Servers | Out-String}
                Gridview { $Script:ALL_Patch_Servers | Out-GridView }
                  Export { $Script:ALL_Patch_Servers | Export-Csv -Path $Location\Results.csv -NoTypeInformation; ii $Location\Results.csv }
                
            }#endSwitch
    
    }
#############################################################################################################################################################
    Function Collection-Servers {
        #Param($Coll_Name)
        
                Write-Host "Collection:$Global:Collection_Name"

                SCCM-Module
                #CD sccm-drive:
                $Script:DeviceCollection_MemberCount = Get-CMDeviceCollection -Name $Global:Collection_Name | select Name,MemberCount
                $Script:DeviceCollection_ServerNames = Get-CMCollectionMember -CollectionName $Global:Collection_Name | select Name,IsClient #| Export-Csv "E:\SCCM-Files\SCCM-Scripts\Files\$Global:Collection_Name.csv" -NoTypeInformation
                #$ServerName = $DeviceCollection_ServerNames.Name       
                Set-Location c:
            
                #$Script:Temp = Get-CMCollectionMember -CollectionName $Global:Collection_Name #| select Name,IsClient       
                       
            $Script:DeviceCollection_ServerNames | % {
                $Obj = New-Object -TypeName PSObject
                $Obj | Add-Member -MemberType NoteProperty -Name Server -Value $_.Name
                $Obj | Add-Member -MemberType NoteProperty -Name ClientInstalled -Value $_.IsClient
                $Obj | Add-Member -MemberType NoteProperty -Name CollectionName -Value $Global:Collection_Name
                #$Obj | Add-Member -MemberType NoteProperty -Name CollectionMemberCount -Value {(Get-CMCollectionMember -Name $Global:Collection_Name).MemberCount}
    
                $Obj
                $Script:ALL_Patch_Servers += $Obj
               
            }#end%

                # $Script:outputBox.Text = $Script:ALL_Patch_Servers | Out-String
                Display-Results #-Final_Result $Script:ALL_Patch_Servers
                Write-Host "$Script:ALL_Patch_Servers"
                $Script:ALL_Patch_Servers | Export-Csv $Location\Current-Collection-Servers.csv -NoTypeInformation
            
    }#endFunction
#############################################################################################################################################################
    Function Ping_Collection {
            
            $Ping_Result = @()                      
            Write-Host "Total Server Count: $($Script:ALL_Patch_Servers.count)" -ForegroundColor Green
            Write-Host ""
            
            SCCM-Module
            #CD sccm-drive:
            $Script:DeviceCollection_ServerNames1 = Get-CMCollectionMember -CollectionName $Global:Collection_Name | select Name
            Set-Location c:
            $Script:DeviceCollection_ServerNames1.Name | % { 
                        
                        $Obj = New-Object -TypeName PSObject
                        If (Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue ) { 
                            #Write-Host "Responding:---- $_ " -ForegroundColor Green 
                            #$Script:outputBox.AppendText("Responding: $_") 
                                         
                            $Obj | Add-Member -MemberType NoteProperty -Name Server -Value $_
                            $Obj | Add-Member -MemberType NoteProperty -Name Responding -Value "No!"
                            $Ping_Result += $Obj

                        }#endIf
            
                        Else { 
                            #$Script:outputBox.AppendText("NOT Responding: $_")
                                $NotResponding += $_ 
                                $Obj | Add-Member -MemberType NoteProperty -Name Server -Value $_
                                $Obj | Add-Member -MemberType NoteProperty -Name Responding -Value "No!"
                                $Ping_Result += $Obj
                             }            
             }#end%
                        $Script:outputBox.Text = $Ping_Result | ft -AutoSize | Out-String 

                        write-host "-------------------------------------------------------"
                        Write-host "$($Responding.count) / $($DeviceCollection_ServerNames1.count) -- Responding" -foregroundcolor Green
                        Write-host "$($NotResponding.count) / $($DeviceCollection_ServerNames1.count) -- NOT Responding" -foregroundcolor Red

            Write-Host "Servers NOT Responding: $NotResponding " -ForegroundColor Red

      }#endFunction
#############################################################################################################################################################
      Function Check_Uptime {
        SCCM-Module
        $Script:DeviceCollection_ServerNames1 = Get-CMCollectionMember -CollectionName $Global:Collection_Name | select Name
        $Script:outputBox.Text = $Script:DeviceCollection_ServerNames1.name | % { gwmi win32_operatingsystem -ComputerName $_ -ErrorAction SilentlyContinue | select  @{n="Server";e={$_.PSComputername}},@{n="LastBootup";e={$_.Converttodatetime($_.LastBootUpTime)}}} | sort LastBootup -descending | Out-String
              
      }

#############################################################################################################################################################
      Function Test-Service {
        $T = Get-Service
        $outputBox.Text = $T | ft -AutoSize | Out-String
      }
#############################################################################################################################################################
  
#############################################################################################################################################################
Start-Form    

