
Inspired by the single export defined in Export SAM Application Template - Example Orion SDK PowerShell Script
<#
Name: Export-SamTemplates.ps1
Description: Export all SAM Application Templates
Inspired by: https://thwack.solarwinds.com/product-forums/the-orion-platform/f/orion-sdk/91158/export-sam-application-template---example-orion-sdk-powershell-script
#>
# Set the Export Path we want to use for the Templates
$ExportPath = "D:\Scratch\SAM Templates"
# Set the hostname for the Orion systems
$SwisHost = "kmsorion01v.kmsigma.local"
# Check to see if the Export Path exists, and if not, create it
if ( -not ( Test-Path -Path $ExportPath -ErrorAction SilentlyContinue ) ) {
Write-Warning -Message "Creating folder at '$ExportPath'"
New-Item -ItemType Directory -Path $ExportPath | Out-Null
}
# Prompt for the credentials
if ( -not $SwisCreds ) {
$SwisCreds = Get-Credential -Message "Enter the credentials to connect to `"$SwisHost`""
}
# Load SwisPowerShell if not already loaded
if ( -not ( Get-Module -Name "SwisPowerShell" ) ) {
Import-Module -Name SwisPowerShell
}
# Build the connection to the SolarWinds Information Service (SWIS)
$SwisConnection = Connect-Swis -Hostname $SwisHost -Credential $SwisCreds
# Get all the Template IDs and Names
$AllTemplates = Get-SwisData -SwisConnection $SwisConnection -Query "SELECT ApplicationTemplateID, Name FROM Orion.APM.ApplicationTemplate ORDER BY Name"
# Cycle through each one
ForEach ( $Template in $AllTemplates ) {
Write-Host "Exporting $( $Template.Name ) from $( $SwisHost )"
# Specify applicationTemplateId to export
$applicationTemplateId = $Template.ApplicationTemplateId
# Export template
# The ExportTemplate template verb is expecting an array of ID's.
# if we only have one, wrap it in @( ) to convert to an array.
$Response = Invoke-SwisVerb -SwisConnection $SwisConnection -EntityName Orion.APM.ApplicationTemplate -Verb ExportTemplate -Arguments @($applicationTemplateId)
# The template text itself is only the 'inner' details.
# if we convert this to an "XML" type, then we ahve access to the ".Save" method that's default on XML types.
$TemplateText = [xml]( $Response.InnerText )
# But to save it, we need a name, which we can get from the template body
# Some templates have back- or forward-slashes. Those are not good for file names, so let's replace with a dash
# The '/' and '\' is not an exhaustive list of 'bad' characters for names - watch for errors
# All SAM templates have the '.apm-template' extension, so we'll add that, too.
$TemplateName = $TemplateText.ArrayOfApplicationTemplate.ApplicationTemplate.Name.Replace('\', '-').Replace('/', '-') + '.apm-template'
# Save the XML file to the file system with overwrite ('destructive write')
$TemplateText.Save(( Join-Path -Path $ExportPath -ChildPath $TemplateName ))
Write-Host "`t[Exported as '$TemplateName']" -ForegroundColor Green
}
[View:/cfs-file/__key/communityserver-discussions-components-files/45/Export_2D00_SamTemplates.ps1:240:120]