Powershell Scripts to distribute packages to a set of DP
At times we get request on that we need to refresh the package or we need to distribute the package for a specific set of DP.
This scripts helps in distributing any kind of content (Application, legacy package, SUM package, driver package etc ) on the .
It uses two input files ($path1 = "c:\temp\server.csv" ,$path2 = "c:\temp\Package.csv")
The first one you can enter the server names and in the second the package names .
Script
=======
Function Refresh-SpecificDP($ar) {
$dpFound = $false
$packageID=$ar[0]
$siteCode=$ar[1]
$dpName= $ar[2]
If ($packageID.Length -ne 8)
{
Throw "Invalid package"
}
$distPoints = Get-WmiObject -Namespace "root\SMS\Site_$($siteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$packageID'"
ForEach ($dp In $distPoints)
{
If ((($dp.ServerNALPath).ToUpper()).Contains($dpName.ToUpper()))
{
$dpFound = $true
Try {
$dp.RefreshNow = $true
$dp.Put() | Out-Null
#$dpName + " - " + $packageID
}
Catch [Exception]
{
return $_.Exception.Message
}
}
}
If ($dpFound -eq $false)
{
Throw "No results returned."
}
}
#main pgm
$path1 = "c:\temp\server.csv"
$path2 = "c:\temp\Package.csv"
$SiteCode='CAS'
Import-csv -path $path1 -Header servername | foreach `
{
$ServerName=$_.servername
Import-csv -path $path2 -Header Packageid | foreach `
{
$pkgID=$_.Packageid
$PackageType= ""
$a= Get-CMPackage | Select-Object PackageID | Where-Object {$_.PackageID -eq $pkgID }
if ($a.PackageID -eq $pkgID)
{
$PackageType= "Package"
}
$a= Get-CMApplication | Select-Object PackageID | Where-Object {$_.PackageID -eq $pkgID }
if ($a.PackageID -eq $pkgID)
{
$PackageType= "Application"
}
$a= Get-CMDriverPackage | Select-Object PackageID | Where-Object {$_.PackageID -eq $pkgID }
if ($a.PackageID -eq $pkgID)
{
$PackageType= "Driver"
}
$a= Get-CMBootImage | Select-Object PackageID | Where-Object {$_.PackageID -eq $pkgID }
if ($a.PackageID -eq $pkgID)
{
$PackageType= "BootImage"
}
$a= Get-CMOperatingSystemImage | Select-Object PackageID | Where-Object {$_.PackageID -eq $pkgID }
if ($a.PackageID -eq $pkgID)
{
$PackageType= "OSImage"
}
If($PackageType -eq "Package")
{
$distpoints = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$PkgID' and serverNALPath like '%$Servername%'" | Select-Object ServerNALPath
if ($distpoints.ServerNALPath.length -eq 0) {
#echo "This is a Package"
start-CMContentDistribution -PackageId "$pkgID" -DistributionPointName "$ServerName"
write-host "Distributing Package :" $pkgID "of type " $PackageType "to DP:" $serverName
}
else {
write-host "Re-Distribting Package :" $pkgID "of type " $PackageType "to DP:" $serverName
$ar=$PkgID,$SiteCode,$Servername
Refresh-SpecificDP ($ar)
}
}
#For applications
If($PackageType -eq "Application")
{
$distpoints = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$PkgID' and serverNALPath like '%$Servername%'" | Select-Object ServerNALPath
if ($distpoints.ServerNALPath.length -eq 0) {
#echo "This is an Application"
start-CMContentDistribution -ApplicationID "$pkgID" -DistributionPointName "$ServerName"
write-host "Distributing Package :" $pkgID "of type " $PackageType "to DP:" $serverName
}
else {
write-host "Re-Distribting Package :" $pkgID "of type " $PackageType "to DP:" $serverName
$ar=$PkgID,$SiteCode,$Servername
Refresh-SpecificDP ($ar)}
}
#For Driverpackages
If($PackageType -eq "Driver")
{ $distpoints = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$PkgID' and serverNALPath like '%$Servername%'" | Select-Object ServerNALPath
if ($distpoints.ServerNALPath.length -eq 0) {
#echo "This is a Driver"
Start-CMContentDistribution -DriverPackageID "$pkgID" -DistributionPointName "$ServerName"
write-host "Distributing Package :" $pkgID "of type " $PackageType "to DP:" $serverName
}
else {
write-host "Re-Distribting Package :" $pkgID "of type " $PackageType "to DP:" $serverName
$ar=$PkgID,$SiteCode,$Servername
Refresh-SpecificDP ($ar)}
}
#For BootImages
If($PackageType -eq "BootImage")
{ $distpoints = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$PkgID' and serverNALPath like '%$Servername%'" | Select-Object ServerNALPath
if ($distpoints.ServerNALPath.length -eq 0) {
#echo "This is a BootImage"
Start-CMContentDistribution -BootImageID "$pkgID" -DistributionPointName "$serverName"
write-host "Distributing Package :" $pkgID "of type " $PackageType "to DP:" $serverName
}
else {
write-host "Re-Distribting Package :" $pkgID "of type " $PackageType "to DP:" $serverName
$ar=$PkgID,$SiteCode,$Servername
Refresh-SpecificDP ($ar)}
}
#For OSImage
If($PackageType -eq "OSImage")
{ $distpoints = Get-WmiObject -Namespace "root\SMS\Site_$($SiteCode)" -Query "Select * From SMS_DistributionPoint WHERE PackageID='$PkgID' and serverNALPath like '%$Servername%'" | Select-Object ServerNALPath
if ($distpoints.ServerNALPath.length -eq 0) {
#echo "This is a OSimage"
Start-CMContentDistribution –OperatingSystemImageID "$pkgID" -DistributionPointName "$serverName"
write-host "Distributing Package :" $pkgID "of type " $PackageType "to DP:" $serverName
}
else {
write-host "Re-Distribting Package :" $pkgID "of type " $PackageType "to DP:" $serverName
$ar=$PkgID,$SiteCode,$Servername
Refresh-SpecificDP ($ar)}
}
}
}
Hope this helps
Sudheesh Narayanaswamy
This posting /Script is provided "AS IS" with no warranties and confers no rights