PowerShell Code Snippets

Any solutions you find on these pages are provided “AS-IS” with no warranties. 

Find Applications with a specific dependency (Java, .Net, c++ runtime, ….)

Let’s assume you want to update your Java Application and you have to find all the Applications in SCCM where Java is set as dependency. The script below will give you the list.

#Enter the Application that is set as dependency
$Dependency = ‘%Java%’

# Fill in your database name and database server below
$SCCMDBName = ‘CM_P01’
$SCCMDBServerName = ‘SVRCMP01’

$objConnection = New-Object -comobject ADODB.Connection
$objRecordset = New-Object -comobject ADODB.Recordset
$con = “Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=$SCCMDBName;Data Source=$SCCMDBServerName”
$strSQL = @”
select rel.FromApplicationCIID from v_CIAppDependenceRelations as rel
inner join fn_ListLatestApplicationCIs(1033) AS app on app.CI_ID = rel.ToApplicationCIID
where app.DisplayName like ‘$Dependency’
“@

$objConnection.Open($con)
$objConnection.CommandTimeout = 0
# *********** Check If connection is open *******************
If($objConnection.state -eq 0)
{
Write-Host “Error: Connection to database failed. ”
Exit 1
}
else
{
$CIID_Array = @()
$objRecordset.Open($strSQL,$objConnection)
$objRecordset.MoveFirst()
$rows=$objRecordset.RecordCount
do
{
$objRecordset.MoveNext()
$CIID = $objRecordset.Fields.Item(0).Value
$CIID_Array += $CIID
}
until ($objRecordset.EOF -eq $TRUE)
$objRecordset.Close()

foreach ($ID in $CIID_Array)
{
$strSQL2 = @”
select DisplayName from fn_ListLatestApplicationCIs(1033) where CI_ID like ‘$ID’
“@
$objRecordset.Open($strSQL2,$objConnection)
If($objConnection.state -eq 0)
{
Write-Host “Error: Connection to database failed. ”
Exit 1
}
else
{
$value = $objRecordset.Fields.Item(0).Value
if($value)
{
Write-Host $value
}
$objRecordset.Close()
}
}
}

Modify the maximum runtime of software updates in SCCM

A frequent complaint is that the maximum runtime of software updates is too short. Or too long in case that small maintenance windows are being used. In most cases, the maxruntime is 10, 60, or sometimes 120 min. The script below allows you to modify the value (the feature to adjust the value was added in 1706).

function LoadCMModule
{
Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + ‘\ConfigurationManager.psd1’)

$SiteCode = Get-PSDrive -PSProvider CMSITE
Set-location $SiteCode”:”
}

$updates = Get-CMSoftwareUpdate -Name “*2020-04*” -Fast #Better set a filter, this takes quite long
$count = 0

foreach ($update in $updates )
{
Write-Host $update.LocalizedDisplayName $update.MaxExecutionTime

switch($update.MaxExecutionTime){
600 {Set-CMSoftwareUpdate -CI_ID $update.CI_ID -MaximumExecutionMins 15; $count++; Write-Host “Changing ” $update.LocalizedDisplayName ” Current MaximumExecutionMins: ” $update.MaxExecutionTime -ForegroundColor Yellow; break;}
3600 {Set-CMSoftwareUpdate -CI_ID $update.CI_ID -MaximumExecutionMins 90; $count++; Write-Host “Changing ” $update.LocalizedDisplayName ” Current MaximumExecutionMins: ” $update.MaxExecutionTime -ForegroundColor Yellow; break;}
7600 {Set-CMSoftwareUpdate -CI_ID $update.CI_ID -MaximumExecutionMins 180; $count++; Write-Host “Changing ” $update.LocalizedDisplayName ” Current MaximumExecutionMins: ” $update.MaxExecutionTime -ForegroundColor Yellow; break;}
}
}
Write-Host $count ” changes made” -ForegroundColor Green