wpid-chinese-take-out.jpg

Two scripts I used for updating migrated Package and Application content source paths to suit an environment where some of the paths for each (Packages and Applications) had to be maintained on distinct hosts.

Props to Matt Shadbolt for inspiration and examples:  https://blogs.technet.microsoft.com/configmgrdogs/2013/05/08/package-application-source-modification-scripts/

Packages

<#
 .SYNOPSIS
Replace PkgSourcePath assignments using explicit mapping

.DESCRIPTION
I hope the synopsis is enough, otherwise I've failed miserably.

.NOTES
Lazy hack: David Stein

.LINK
https://skatterbrainz.wordpress.com 

.EXAMPLE
.\Update-CmPackageSourcePaths.ps1

#>

function Get-CmAdminConsolePath {
  $result = Get-ItemProperty "HKLM:SOFTWARE\Microsoft\SMS\Setup" | 
  Select-Object -ExpandProperty "UI Installation Directory"
  $cpath = "$result\bin\ConfigurationManager.psd1"
  if (Test-Path $cpath) { $cpath }
}

function Swap-Paths {
  param (
    [parameter(Mandatory=$False)] 
    [string] $SourcePath = ""
  )
  $newPath = ""
  if ($SourcePath -ne "") {
    $newPath = $SourcePath.ToLower().replace("\\server1.contoso.com\apps$", "\\FS1.fabrikam.com\DSL$\Apps")
    $newPath = $newPath.ToLower().replace("\\Apps1.fubar.com\software$\apps", "\\FS2.fabrikam.com\DSL$\Apps")
  }
  return $newPath
}

Import-Module $(Get-CmAdminConsolePath)

$SiteCode = Get-ItemProperty "HKLM:SOFTWARE\Microsoft\SMS\Identification" -Name "Site Code" | Select-Object -ExpandProperty "Site Code"
cd "$SiteCode`:"

$pkgs = Get-CMPackage

foreach ($pkg in $pkgs) {
  Write-Host "PackageID: $($pkg.PackageID)"
  Write-Host "Name: $($pkg.Name)"
  $z = $pkg.PkgSourcePath
  Write-Host "Old Source: $z"
  $x = Swap-Paths -SourcePath $pkg.PkgSourcePath
  Write-Host "New Source: $x"
  if ($x -ne "") {
    if ($x.ToLower() -ne $z.ToLower()) {
      Write-Host "updating pacakge source path..."
      Set-CMPackage -Id $pkg.PackageID -Path $x
    }
  }
  write-host "----"
}

Write-Host "all done. go drink!"

Applications

<#
 .SYNOPSIS
Replace Deployment Type source folder assignments using explicit mapping

.DESCRIPTION
I hope the synopsis is enough, otherwise I've failed miserably.  
If you stare at this code long enough, you might fall asleep.

.NOTES
Lazy hack: David Stein

.LINK
https://skatterbrainz.wordpress.com 

.EXAMPLE
.\Update-CmAppSourcePaths.ps1

#>

function Get-CmAdminConsolePath {
  $result = Get-ItemProperty "HKLM:SOFTWARE\Microsoft\SMS\Setup" | 
  Select-Object -ExpandProperty "UI Installation Directory"
  $cpath = "$result\bin\ConfigurationManager.psd1"
  if (Test-Path $cpath) { $cpath }
}

function Swap-Paths {
  param (
    [parameter(Mandatory=$False)] 
    [string] $SourcePath = ""
  )
  $newPath = ""
  if ($SourcePath -ne "") {
    $newPath = $SourcePath.ToLower().replace("\\server1.contoso.com\apps$", "\\FS1.fabrikam.com\DSL$\Apps")
    $newPath = $newPath.ToLower().replace("\\Apps1.fubar.com\software$\apps", "\\FS2.fabrikam.com\DSL$\Apps")
  }
  return $newPath
}

Import-Module $(Get-CmAdminConsolePath)

$SiteCode = Get-ItemProperty "HKLM:SOFTWARE\Microsoft\SMS\Identification" -Name "Site Code" | Select-Object -ExpandProperty "Site Code"
cd "$SiteCode`:"

Write-Host "Querying Applications..." -ForegroundColor Cyan

$Apps = Get-CMApplication
$AppNames = $Apps.LocalizedDisplayName
$AppCount = $AppNames.Count
$RowCount = 1

Write-Host "Returned $AppCount objects"

foreach ($AppName in $AppNames) {
  Write-Host "Application: $AppName ($RowCount of $AppCount)" -ForegroundColor White
  $DtNames = Get-CMDeploymentType -ApplicationName $AppName
  Write-Host "Querying Deployment types..." -ForegroundColor Cyan
  foreach ($dt in $DtNames) {
    $DtSDMPackageXML = $dt.SDMPackageXML
    $DtSDMPackageXML = [xml]$DtSDMPackageXML
    $DtLocalName = $dt.LocalizedDisplayName
    $DtCleanPath = ""
    $DtCleanPath = $DtSDMPackageXML.AppMgmtDigest.DeploymentType.Installer.Contents.Content.Location[0]
 
    # check if array returned only a "\" value, indicating a singular value
    if ($DtPath.Length -lt 2) {
      $DtPath = $DtSDMPackageXML.AppMgmtDigest.DeploymentType.Installer.Contents.Content.Location
    }
    Write-Host "Current source: $DtPath" -ForegroundColor Cyan
    $NewDtPath = Swap-Paths -SourcePath $DtPath
    Write-Host "New directory source: $NewDtPath" -ForegroundColor Green
    if ($NewDtPath -ne "") {
      if ($NewDtPath.ToLower() -ne $DtPath.ToLower()) {
        Set-CMDeploymentType -ApplicationName "$AppName" -DeploymentTypeName $DtLocalName -MsiOrScriptInstaller -ContentLocation "$NewDtPath"
        Write-Host "Updating: $DtLocalName" -ForegroundColor Cyan
      }
      else {
        Write-Host "No changes made" -ForegroundColor Cyan
      }
    }
  }
  Write-Host "--------------"
  $RowCount++
}

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s