Posted on 周日 10 四月 2022

SCCM PowerShell Scripts and log file reference



Reprinted from: https://drive.google.com/file/d/1F1M-T0eaWDRvB59x-xWFh4Thd9soh0lP/view

SCCM Log file reference: https://docs.microsoft.com/en-us/mem/configmgr/core/plan-design/hierarchy/log-files

Basic Powershell Commands :

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# 1.Below Command Displays Site Server Name, SiteCode And Site Version
Get-CMSite | Select ServerName, SiteCode, Version

# 2.Below Command Displays Roles installed on Primary Site Server (MECMPS.lab.com )
Get-CMSiteRole -SiteSystemServerName MECMPS.lab.com | Select RoleName

# 3. Below Command Displays the list of users present in MECM Database
Get-CMUser | Select Name

# 4. Below Command Displays the list of Devices present in MECM Database
Get-CMDevice | Select Name

# 5. Below Command Displays the list of Packages in MECM 
Get-CMPackage -fast | Select Name

# 6. Below Command Displays the list of Applications in MECM 
Get-CMApplication | Select LocalizedDisplayName 

# 7. Below Command Displays the list of Boundaries and their value in MECM 
Get-CMBoundary | Select DisplayName, Value

# 8. Below Command Displays the list of Boundary Groups in MECM 
Get-CMBoundaryGroup | Select Name

# 9. Below Command Displays the list of Compliance Baseline Name and its compliance result in MECM 
Get-CMBaseline | Select LocalizedDisplayName, ComplianceCount, NonComplianceCount

# 10. Below Command Displays the list of All deployments made in MECM 
Get-CMDeployment | Select ApplicationName, CollectionName, DeploymentTime

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Powershell Commands To Create User Collections

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

New-CMUserCollection -Name 'New York Users' -LimitingCollectionName 'All Users'
New-CMUserCollection -Name 'Mumbai Users' -LimitingCollectionName 'All Users'
New-CMUserCollection -Name 'London Users' -LimitingCollectionName 'All Users'
New-CMUserCollection -Name 'Mexico Users' -LimitingCollectionName 'All Users'
New-CMUserCollection -Name 'Brazil Users' -LimitingCollectionName 'All Users'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Powershell Commands To Create Device Collections

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

New-cmdevicecollection -Name 'Brazil Computers' -LimitingCollectionName 'All Systems'
New-cmdevicecollection -Name 'New York Computers' -LimitingCollectionName 'All Systems'
New-cmdevicecollection -Name 'Mumbai Computers' -LimitingCollectionName 'All Systems'
New-cmdevicecollection -Name 'London Computers' -LimitingCollectionName 'All Systems'
New-cmdevicecollection -Name 'Mexico Computers' -LimitingCollectionName 'All Systems' 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create Device collections in sccm using powershell and move it to a specific folder

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#Step 1 : Create Folders
New-Item -Name 'Google Chrome' -Path 'PS1:\DeviceCollection'
New-Item -Name 'Firefox' -Path 'PS1:\DeviceCollection'
New-Item -Name '7zip' -Path 'PS1:\DeviceCollection'
New-Item -Name 'Mremote' -Path 'PS1:\DeviceCollection'
New-Item -Name 'VLC Media Player' -Path 'PS1:\DeviceCollection'
New-Item -Name 'Visio' -Path 'PS1:\DeviceCollection'

#Step 2 : Create Device Collections
New-CMDeviceCollection -Name 'Google_Chrome_Device_Collection' -LimitingCollectionName 'All Systems'
New-CMDeviceCollection -Name 'Firefox_Device_Collection' -LimitingCollectionName 'All Systems'
New-CMDeviceCollection -Name '7zip_Device_Collection' -LimitingCollectionName 'All Systems'
New-CMDeviceCollection -Name 'Mremote_Device_Collection' -LimitingCollectionName 'All Systems'
New-CMDeviceCollection -Name 'VLC_Media_Player_Device_Collection' -LimitingCollectionName 'All Systems'
New-CMDeviceCollection -Name 'Visio_Device_Collection' -LimitingCollectionName 'All Systems'

#Step 3 : Define Variables
$Collection1 = Get-CMDeviceCollection -Name 'Google_Chrome_Device_Collection'
$Collection2 = Get-CMDeviceCollection -Name 'Firefox_Device_Collection'
$Collection3 = Get-CMDeviceCollection -Name '7zip_Device_Collection'
$Collection4 = Get-CMDeviceCollection -Name 'Mremote_Device_Collection'
$Collection5 = Get-CMDeviceCollection -Name 'VLC_Media_Player_Device_Collection'
$Collection6 = Get-CMDeviceCollection -Name 'Visio_Device_Collection'

#Step 4 : Move collections into a specified folders
Move-CMObject -InputObject $Collection1 -FolderPath 'PS1:\DeviceCollection\Google Chrome'
Move-CMObject -InputObject $Collection2 -FolderPath 'PS1:\DeviceCollection\Firefox'
Move-CMObject -InputObject $Collection3 -FolderPath 'PS1:\DeviceCollection\7zip'
Move-CMObject -InputObject $Collection4 -FolderPath 'PS1:\DeviceCollection\Mremote'
Move-CMObject -InputObject $Collection5 -FolderPath 'PS1:\DeviceCollection\VLC Media Player'
Move-CMObject -InputObject $Collection6 -FolderPath 'PS1:\DeviceCollection\Visio'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create Direct Rule Device collections in sccm using powershell

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   $collectiondir = "C:\Users\sccmadmin\Desktop\Kashif"
   $collectionname = "New York Laptops"
   $computers = Get-Content "C:\Users\sccmadmin\Desktop\Kashif\PC_List.txt"


   New-CMDeviceCollection -Name $collectionname -LimitingCollectionName "All Systems"
    foreach($computer in $computers) {
        try {
            Add-CMDeviceCollectionDirectMembershipRule  -CollectionName $collectionname -ResourceId $(get-cmdevice -Name $computer).ResourceID
            }
        catch {
            "Invalid client or direct membership rule may already exist: $computer" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
            }
    }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create Query Rule Device collections in sccm using powershell

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#  1. Define Variables

$collectiondir = "C:\Users\sccmadmin\Desktop\Kashif"
$collectionname = "All Windows 10 Computes"
$Query = "select *  from  SMS_R_System where SMS_R_System.OperatingSystemNameandVersion like '%workstation%'"

# 2. Create New Collection#

New-CMDeviceCollection -Name $collectionname -LimitingCollectionName "All Systems"

# 3. Add members in the collection using query rule method#

Add-CMDeviceCollectionQueryMembershipRule -CollectionName $collectionname -QueryExpression  $Query -RuleName 'Query1'

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Packaging Cisco Anyconnect with Java as a pre-requisite using Powershell App Deployment Toolkit (PSADT)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#Install Java : 
$setup = Start-Process "\\sccmps\source\Applications\JRE\jre-8u192-windows-x64.exe" -ArgumentList "/s" -Wait -NoNewWindow


#Uninstall Java : 
$app = Get-WmiObject -Class win32_Product | Where-Object {$_.Name -eq "Java 8 Update 221 (64-bit)"}
$app.uninstall()

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create And Deploy Application In Using Powershell

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#1. Define Variables
$appname = "Application Name"
$publisher = "Vendor"
$version = "Version"
$location = "Application Location"
$collname = "Collection Name"
$description = "Description"
$icon = "Icon Location"
$dp = "DP Name"
$install = "Install Command Line"
$uninstall = "Uninstall Command Line"

#2. Create New Application
New-CMApplication -Name $appname -Description $description -SoftwareVersion $version -Publisher $publisher -AutoInstall $true -IconLocationFile $icon -Verbose

#3. Add Deployment Type
Add-CMMsiDeploymentType -ApplicationName $appname -ContentLocation $location -InstallationBehaviorType InstallForSystem -InstallCommand $install -UninstallCommand $uninstall

#4. Distribute The App To DP
Start-CMContentDistribution -ApplicationName $appname -DistributionPointName $dp -Verbose

#5. Deployment 
New-CMApplicationDeployment -CollectionName $collname -Name $appname -DeployAction Install -DeployPurpose Available -UserNotification DisplayAll -AvailableDateTime (Get-Date) -TimeBaseOn LocalTime -Verbose

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++