Objective :

The purpose of the script us to gather all SCCM APPLICATIONS and then check those packages which DATA SOURCE PATH is in-accessible. This script will collect those packages and format the output in HTML page with below attribute:

This script is helpful to find such applications and clean them from SCCM.

 Process :

  1. This script will ask to provide the SITE CODE and SCCM SERVER.
  2. After successful connection it will gather all applications. 
  3. All the applications will be passed to the condition to check the accessibility of data source path. 
  4. If the application's data source path is not accessible will be printed on HTML page.

Note :

This script will take time to complete the execution as getting applications in SCCM is takes time due to collect information of all applications.

Some of the applications may have distributed to some DP. While cleaning up these , special attention need to be taken care.

Learning for Powershell script:

 

 

PowerShell
Edit|Remove
function InitializeSCCM  
{  
$ProcessMessage="`n Please wait.Initializing SCCM ........."  
  
# Site configuration 
do 
{  
    write-host "`n Enter Site Code : " -foregroundcolor $inputcolor -nonewline  
    $SiteCode = read-host  
    $siteResult=($siteCode -match '\b^[a-zA-Z0-9]{3}\b') 
    if(!$siteResult) 
    { 
    write-host " Site code can have only [3] alphanumeric characters. Please re-enter site code" -foregroundcolor RED 
    } 
}while(!$siteResult) 
 
do 
{ 
    write-host "`n Enter SMS Provider Server Name : " -foregroundcolor $inputcolor -nonewline  
    $ProviderMachineName = read-host  
    $nameResult=($ProviderMachineName -match '\b^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$\b') 
    if(!$nameResult) 
    { 
    write-host " Entered SMS provider name is not valid as per naming conventions. Please re-enter provider name" -foregroundcolor RED 
    } 
}while(!$nameResult) 
 
iex $ProcessColor  
sleep 2  
# Customizations  
$initParams = @{}  
  
# Import the ConfigurationManager.psd1 module   
if((Get-Module ConfigurationManager) -eq $null) {  
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams   
}  
  
# Connect to the site's drive if it is not already present  
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {  
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams  
}  
  
# Set the current location to be the site code.  
Set-Location "$($SiteCode):\" @initParams  
}  
  
function deinitializeSCCM  
{  
    $ProcessMessage="`n Please wait.De-Initializing SCCM ......"  
    iex $ProcessColor  
    sleep 2  
    set-location $location  
}  
function updateHTML 
{ 
 
param ($strPathIF(Test-Path $strPath) 
  {  
   Remove-Item $strPath 
    
  } 
 
 } 
  
#--CSS formatting 
$test=@' 
<style type="text/css"> 
 h1, h5,h2, th { text-align: left; font-family: Segoe UI;font-size: 13px;} 
table { margin: left; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; } 
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; font-size: 12px;} 
td { font-size: 11px; padding: 5px 20px; color: #000; } 
tr { background: #b8d1f3; } 
tr:nth-child(even) { background: #dae5f4; } 
tr:nth-child(odd) { background: #b8d1f3; } 
</style> 
'@ 
 
#--Variable declaration 
 clear 
 $location=get-location  
 $InputColor="yellow"  
 $ProcessColor="write-host `$ProcessMessage -ForegroundColor gray -BackgroundColor darkgreen"  
 $ReportTitle="SCCM APPLICATIONS HAVING IN-ACCESSIBLE DATA SOURCE PATH" 
 $strPath = "$location\$ReportTitle.html"  
  
 updateHTML $strPath 
 
#Boot Image Information 
 
ConvertTo-Html -Head $test -Title $ReportTitle -Body "<h1> APPLICATIONS HAVING IN-ACCESSIBLE DATA SOURCE PATH</h1>" >  "$strPath" 
InitializeSCCM  
write-host "Getting applications Information" -foregroundcolor $inputcolor 
$AppaData=@{}  
$info = @()  
$Object = New-Object PSObject  
$appList=Get-CMApplication | select LocalizedDisplayName,PackageId,SDMPackageXML,LocalizedDescription,IsSuperseded,IsEnabled,IsExpired 
foreach ($Application in $appList)   
    {  
        $AppMgmt = ([xml]$Application.SDMPackageXML).AppMgmtDigest  
        $AppName = $AppMgmt.Application.DisplayInfo.FirstChild.Title  
        foreach ($DeploymentType in $AppMgmt.DeploymentType)   
            {  
                $appPath=$DeploymentType.Installer.Contents.Content.Location  
                $AppData = @{              
                                AppName  = $AppName  
                                DisplayName=$application.LocalizedDisplayName  
                                Location =  $appPath          
                                PackageID = $Application.packageid  
                                Enabled = $Application.IsEnabled 
                                Superseded = $Application.IsSuperseded 
                                Expired = $application.IsExpired 
                                Description=$application.LocalizedDescription 
                            }   
                $appPath=$null  
                $Object = New-Object PSObject -Property $AppData  
                $info+=$Object                          
            }  
    }  
#De-initializing SCCM  
deinitializeSCCM  
write-host "Processing data to get applications with no data source." -foregroundcolor $inputcolor 
$result = @() 
$i=0 
 
foreach($c in $Info) 
    { 
        if (($(Try { Test-Path -LiteralPath $c.Location.trim() -ErrorAction Stop} Catch { if($Error[0].Exception -is [System.UnauthorizedAccessException]){$true}else{$false}}))) {} 
        else 
            { 
                if(([System.Uri]$c.Location).isUnc -eq $true) 
                { 
                        $property=$null 
                        $property=$c | select * 
                        $newProperty = [ordered]@{} 
                        $newProperty."Sr. No" = $i+1 
                        $newProperty."Name" = $property.AppName 
                        $newProperty."Package ID" = $property.PackageID 
                        $newProperty."Description" = $property.Description 
                        $newProperty."Source Path" = $property.Location 
                        $newProperty."Enabled" = $property.enabled 
                        $newProperty."Expired" = $property.expired 
                        $newProperty."Enabled" = $property.enabled 
                        $newProperty."Superseded" = $property.superseded         
                        $Objectname = New-Object PSobject -Property $newProperty 
                        $result +$Objectname 
                        $i++ 
                } 
            } 
         
    } 
 
$i-- 
write-host "Application information gathering completed." -foregroundcolor $inputcolor 
$count=$Info.count 
ConvertTo-Html -Head $test -Title $ReportTitle -Body "<h2> Total applications in entire site : $count </h2>" >  "$strPath" 
$result | ConvertTo-html  -Head $test -Body "<h2>Total Applications having in-accessible data source path : $i </h2>" >> "$strPath" 
#Launching HTML generated report  
write-host "`n Opening $strpath report. `n" -foregroundcolor $inputcolor -nonewline  
Invoke-Item $strPath