Skip to content

Instantly share code, notes, and snippets.

@techdecline
Last active December 19, 2018 09:01
Show Gist options
  • Select an option

  • Save techdecline/74b4a55d14626f60bde4ec7bd04ccd78 to your computer and use it in GitHub Desktop.

Select an option

Save techdecline/74b4a55d14626f60bde4ec7bd04ccd78 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Exports application objects from ConfigMgr.
.DESCRIPTION
Exports application objects from ConfigMgr from a local or remote Site Server.
Additionally, a filter can be used to export only a subset of applications.
.EXAMPLE
PS> Export-MyCMApplication -ExportLocation '\\siteserver1\Sources$\Applications\Import' -CMSiteCode SP1: -CMSiteServerFQDN siteserver1.contoso.com -Filter "*Visio*" -Verbose
Exports all Application from 'siteserver1' that match the filter '*visio*'
#>
function Export-MyCMApplication {
[CmdletBinding(DefaultParameterSetName="Default")]
param(
# Specify Export File Path
[Parameter(Mandatory)]
[ValidateScript({Test-Path $_})]
[String]$ExportLocation,
# Specify ConfigMgr Site Code
[Parameter(Mandatory)]
[ValidatePattern("^\w{3}:$")]
[string]
$CMSiteCode,
# Specify CM Site Server FQDN
[Parameter(Mandatory)]
[String]
$CMSiteServerFQDN,
# Optionally set filter for application quering
[Parameter(Mandatory=$false)]
[String]$Filter
)
begin {
# Connect ConfigMgr
$modulePath = Join-Path -Path (split-path "$env:SMS_ADMIN_UI_PATH" -Parent) -ChildPath "ConfigurationManager.psd1"
Write-Verbose "Loading ConfigMgr Module from: $modulePath"
try {
Import-Module $modulePath -ErrorAction Stop -Verbose:$false
New-PSDrive -Name $CMSiteCode.Substring(0,3) -PSProvider CMSite -Root $CMSiteServerFQDN | Out-Null
Push-Location -Path ($CMSiteCode + "\")
}
catch [System.Management.Automation.ActionPreferenceStopException] {
Write-Error "Could not load ConfigMgr Module"
return $false
}
}
process {
if ($Filter) {
Write-Verbose "Quering Application with filter: $Filter"
$appArr = Get-CMApplication -Name $Filter
}
else {
Write-Verbose "Quering Application without filter"
$appArr = Get-CMApplication
}
foreach ($appObj in $appArr) {
Write-Verbose "Exporting application: $($appObj.LocalizedDisplayName)"
$exportFile = Join-Path $ExportLocation -ChildPath ($appObj.LocalizedDisplayName + ".zip")
Export-CMApplication -InputObject $appObj -Path $exportFile
}
}
end {
Pop-Location
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment