In my previous blog post i wrote about how to Enable Programs and Applications to be installed from a Task Sequence without being Deployed.  While this was a good start i received some initial feedback regarding the concern around policy traffic for non referenced applications.  My Previous blog post can be read here and you can also see the feedback from Aaron from our PG which is what led to this updated version of the script that now utilizes a Windows Form so you can select which Task Sequence you want to target and all referenced applications and packages will be affected just for that Task Sequence limiting the scope to only what you need instead of the previous method of everything.

While this limits the targeting to what you really need you should still be cautious and test this in your lab and only run when needed.

Below is a quick example of the Windows Form that will dynamically display all Task Sequences when ran on your site server.  Additionally the Enable checkbox is selected by default and  you can select to disable as well by unchecking the checkbox.

image

Sample output from the script letting you know if you are Enabling / Disabling and what Task Sequence you selected.

image

import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
$PSD = Get-PSDrive -PSProvider CMSite
CD "$($PSD):"
 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
 
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Task Sequence"
$objForm.AutoSize = $true
$objForm.AutoScroll = $true 
$objForm.StartPosition = "CenterScreen"
 
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})
 
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)
 
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
 
$CheckboxButton = New-Object System.Windows.Forms.Checkbox 
$CheckboxButton.Location = New-Object System.Drawing.Size(120,150) 
$CheckboxButton.Size = New-Object System.Drawing.Size(75,23)
$CheckboxButton.Checked = $true
$CheckboxButton.Text = "Enable"
$CheckboxButton.TabIndex = 4
$objForm.Controls.Add($CheckboxButton)
 
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a Task Sequence:"
$objForm.Controls.Add($objLabel) 
 
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.AutoSize = $true 
$objListBox.Height = 80
 
$TSList = (Get-CMTaskSequence -Name *).Name | Foreach-Object {[void] $objListBox.Items.Add($_)}
 
$objForm.Controls.Add($objListBox) 
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
 
$TSSelection = $objListBox.SelectedItem
 
if (!$TSSelection) { Write-Host "User Exited Script... Please Run again and select a Task Sequence."
Break}
 
If($CheckboxButton.CheckState -eq "Checked"){
Write-Host "Enabling Referenced Items for Task Sequence:"$TSSelection -foregroundcolor Yellow
$CheckBoxState = $True
}
Else{
Write-Host "Disabling Referenced Items for Task Sequence:"$TSSelection -foregroundcolor Yellow
$CheckBoxState = $False
}
 
$References = (Get-CMTaskSequence -Name ($TSSelection)).References
foreach ($Reference in $References){
    if ($Reference.Type -eq "0"){
    $Programs = Get-CMProgram -PackageID $Reference.Package -ProgramName $Reference.Program | Where-Object {$_.NumOfPrograms -ne 0}
    foreach ($Program in $Programs){
    Try{
    Write-Host "Setting EnableTaskSequence to $CheckBoxState for Package Name:"$Program.PackageName "and Program Name:" $Program.ProgramName
    Set-CMProgram -Id ($Program.PackageID) -StandardProgramName ($Program.ProgramName) -EnableTaskSequence $CheckBoxState
    }
    Catch
         {
          "Error: $_.Exception"
         }
        }
    }
    Elseif ($Reference.Type -eq "1"){
    $Applications = Get-CMApplication -id * | Where-Object {$_.ModelName -eq ($Reference.Package)}
    foreach ($Application in $Applications){
    Try{
    Write-Host "Setting AutoInstall to $CheckBoxState for Application Name:"$Application.LocalizedDisplayName
    Set-CMApplication -Id $Application.CI_ID -AutoInstall $CheckBoxState
    }
    Catch
         {
          "Error: $_.Exception"
         }
        }
    }
    Else{
    Write-Host "Excluding as it is not a Package or Application..."
        }
} 

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified
in the
Terms of Use.