# Version 1.0 - 2017/03/06
###########################################
# Disclaimer
#
# The sample script is not supported under any standard support program or service. The sample script is provided
# AS IS without warranty of any kind.
#
# Examples:
# CMDL Args:
# .\Distribute-Content.ps1 -DPGName "Germany DPs" -CMObjectType "Application"
# .\Distribute-Content.ps1 -DPGName "Germany DPs" -CMObjectType "Package"
# v1.0 - Distribute new Packages or Applications to a Distribution Point Group
Param (
[string]
$SiteServer = ".",
[string]
[Parameter(Mandatory=$true,HelpMessage='Distribution Point Group Name as a string')]
$DPGName,
[Parameter(Mandatory=$true,HelpMessage='CM Object Type as Application or Package')]
[string]
$CMObjectType
)
#=================================================
# Declaring Functions
#=================================================
function GetSiteCode($server)
{
$wqlQuery = "SELECT * FROM SMS_ProviderLocation"
Get-WmiObject -Query $wqlQuery -Namespace "root\sms" -ComputerName $server | ForEach-Object {
If ($_.ProviderForLocalSite)
{$siteCode = $_.SiteCode}
}
return $siteCode.ToLower()
}
#=================================================
# Validating Parameters and Values
#=================================================
$SiteCode = GetSiteCode($SiteServer)
# Set CMObjectType to lower letters
$CMObjectType = $CMObjectType.ToLower()
#Check SourceRoot
if (($CMObjectType -ne "application") -and ($CMObjectType -ne "package"))
{
Write-Host "@ERROR: 1" -ForegroundColor Red
Write-Host "The parameter CMObjectType : $CMObjectType has to be either Package or Application" -ForegroundColor Red
Exit 1
}
# Validate specified Distribution Point Group
$DPG = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_DistributionPointGroup -Filter "Name = '$DPGName'"
if (($DPG | Measure-Object).Count -gt 1) {
Write-Host "@ERROR: 2" -ForegroundColor Red
Write-Host "Found more then 1 Distribution Point Group: $DPGName)" -ForegroundColor Red
Exit 2
}
if (!$DPG) {
Write-Host "@ERROR: 3" -ForegroundColor Red
Write-Host "Unable to determine Distribution Point Group name from specified string for parameter '$DPGName'" -ForegroundColor Red
Exit 3
}
#=================================================
# Main Script
#=================================================
#Get Site Code
$SiteCode = GetSiteCode($SiteServer)
#Import the CM12 Powershell cmdlets
if (-not (Test-Path -Path $SiteCode)) {
Write-Verbose "$(Get-Date): CM12 module has not been imported yet, will import it now."
Import-Module ($env:SMS_ADMIN_UI_PATH.Substring(0,$env:SMS_ADMIN_UI_PATH.Length - 5) + '\ConfigurationManager.psd1') | Out-Null
}
#CM cmdlets need to be run from the CM drive
Set-Location "$($SiteCode):" | Out-Null
if (-not (Get-PSDrive -Name $SiteCode)) {
Write-Error "There was a problem loading the Configuration Manager powershell module and accessing the sites PSDrive."
exit 1
}
# Distribute Packages/Applications to the Distribution Point Group
# Get Packages' PackageIDs
if ($CMObjectType -eq "package") {
$PackageIDs = Get-CMPackage | Select-Object -ExpandProperty PackageID
}
# Get Applications' PackageIDs
if ($CMObjectType -eq "application") {
$PackageIDs = Get-CMApplication | Select-Object -ExpandProperty PackageID
}
ForEach($PackageID in $PackageIDs) {
if (-not(Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_DPGroupDistributionStatusDetails -ComputerName $SiteServer -Filter "PackageID = '$PackageID'" -ErrorAction SilentlyContinue)) {
Write-Host "Adding $CMObjectType PackageID: $PackageID - " -NoNewline
if (($DPG.AddPackages($PackageID)).ReturnValue -ne 0)
{
Write-Host -ForegroundColor Red "Possible error processing $PackageID"
}
else{
Write-Host -ForegroundColor Green "Success!"
}
}
}
Set-Location C:\Temp