Example Script to Create Application Catalog, Import Packages, and Perform Testing
Edition:Powershell cmdlets are enabled with AdminStudio Enterprise Edition and with Workflow Manager.
You can use the example PowerShell script that is provided in this section to perform the following tasks:
• | Create Application Catalog—Create a new Application Catalog database. |
• | Import packages—Import all of the .msi files from a specified directory into the Application Catalog. |
• | Perform testing—Run all selected tests, and report the summary of errors and warnings. |
The following script uses the Set-ASConfigPlatform, New-ASCatalogPlatform, Invoke-ASImportPackage, and Test-ASPackage AdminStudio PowerShell cmdlets to create a new Application Catalog, import packages, and then test those packages and view the test results. The AdminStudio PowerShell cmdlets and parameters are highlighted:
###############################################################
# Read Command Line Parameters
###############################################################
param ($CatalogName = $(Read-Host "Enter New Catalog Name")
###############################################################
# Input required from users
###############################################################
$folder = "C:\code\Demo"
$ConnectionString = 'PROVIDER=MSOLEDBSQL19;Data Source=localhost;Initial Catalog=MyNewCat;Integrated Security=SSPI;'
###############################################################
# Non-User Settings
###############################################################
$shive = "HKLM:\SOFTWARE\Wow6432Node\InstallShield\AdminStudio\15.0\"
$slocation = "Product Location"
$sAsLoc = (Get-ItemProperty $shive $slocation).$slocation
$sCurrentLoc = [Environment]::CurrentDirectory=(Get-Location-PSProvider FileSystem).ProviderPath
$sAsLoc = $sAsLoc + "Common\"
$global:oPkgArray = @()
$global:oPkgArrayFail = @()
###############################################################
# Functions
###############################################################
function Import ($s)
{
$f = [System.IO.File]::GetAttributes($s)
$d = ($f-band [System.IO.FileAttributes]::Directory)
if (!$d)
{
Write-Host 'Importing:' $s
$obj = Invoke-ASImportPackage-PackagePath $s;
if ($obj.GetType().FullName-eq 'AdminStudio.Platform.Helpers.PackageHelper')
{
#Write-Host 'Success' $s
$global:oPkgArray = $global:oPkgArray + $obj
}
else
{
Write-Host 'Failured to import:' $s -foregroundcolor red
$global:oPkgArrayFail = $global:oPkgArrayFail + $obj
}
}
}
function LoadDLL ($s)
{
$FileName = $sAsLoc + $s
import-module-name $FileName
}
function PrepAS ()
{
cd $sAsLoc
LoadDLL 'AdminStudio.Platform.PowerShellExtensions.dll'
LoadDLL 'AdminStudio.Utilities.dll'
LoadDLL 'AdminStudio.SCCM.Model.dll'
Set-ASConfigPlatform-ConnectionString $ConnectionString
}
function Write-Host-Indent ()
{
Write-Host ' '-nonewline
}
function Write-Host-Drawline ()
{
Write-Host '**************************************'-foregroundcolor yellow
}
function Test ($o)
{
Write-Host 'Testing Package:' $o.DisplayedProductName-nonewline
Write-Host ' RowId:' $o.RowID-foregroundcolor gray
$oTestResults = Test-ASPackage-PackageId $o.RowID
$errors = 0;
$warn = 0;
foreach ($oTestResult in $oTestResults.Stats)
{
$errors = $errors + $oTestResult.Errors
$warn =$warn + $oTestResult.Warnings
}
Write-Host-Indent
Write-Host 'Errors:' $errors
Write-Host-Indent
Write-Host 'Warnings:' $warn
}
################################################################
# Main Loop
###############################################################
$tBegin = Get-Date
Write-Host 'Begin:' $tBegin-foregroundcolor gray
Write-Host-Drawline
Write-Host ' Import from Folder and Test'
Write-Host-Drawline
Write-Host 'Directory =' $folder-foregroundcolor gray
Write-Host 'ConnectionString =' $ConnectionString-foregroundcolor gray
Write-Host 'AdminStudio Directory =' $sAsLoc-foregroundcolor gray
Write-Host-Drawline
###############################################################
# Load Required DLLs
###############################################################
PrepAS
Write-Host
###############################################################
# Create Catalog
###############################################################
Write-Host 'Creating New Catalog' $CatalogName-foregroundcolor yellow
New-ASCatalog-CatalogName $CatalogName
Write-Host
Write-Host 'Importing Applications from' $folder-foregroundcolor yellow
# Iterate Toplevel Folder Only for Importing
foreach ($file in Get-Childitem-include '*.msi' -Recurse $folder)
{
Import ($file)
}
Write-Host 'Packages that Import Succeeded:' $global:oPkgArray.Count
Write-Host 'Packages that Import Failed:' $global:oPkgArrayFail.Count
$tEnd = Get-Date
$tDiff = $tEnd - $tBegin
Write-Host 'End:' $tEnd-foregroundcolor gray
Write-Host 'Total Time:' $tDiff.Hours' hours' $tDiff.Minutes' minutes ' $tDiff.Seconds 'seconds'
###############################################################
#Run tests
###############################################################
foreach ($oPkg in $global:oPkgArray)
{
Test ($oPkg);
}
###############################################################
# Write out end time
cd $sCurrentLoc
$tEnd = Get-Date
$tDiff = $tEnd - $tBegin
Write-Host 'End:' $tEnd-foregroundcolor gray
Write-Host 'Total Time:' $tDiff.Hours' hours' $tDiff.Minutes' minutes ' $tDiff.Seconds 'seconds'
Output
When you run the script in Example Script, you will see output similar to the following:
PS C:\code\Script> .\MyScript.ps1
Enter New Catalog Name: MyScript
Begin: 2/18/2016 11:27:57 AM
**************************************
Import from Folder and Test
**************************************
Directory = C:\code\Demo
ConnectionString = PROVIDER=MSOLEDBSQL19;Data Source=localhost;Initial Catalog=MyNewCat;Integrated Security=SSPI;
AdminStudio Directory = C:\Program Files (x86)\AdminStudio\2016\Common\
**************************************
Creating New Catalog MyScript
Importing Applications from C:\code\Demo
Importing: C:\code\Demo\Firefox_MSI\Firefox.msi
Packages that Import Succeeded: 1
Packages that Import Failed: 0
End: 2/18/2016 11:28:00 AM
Total Time: 0 hours 0 minutes 3 seconds
Testing Package: Mozilla_Firefox RowId: 2
Errors: 0
Warnings: 382
End: 3/18/2016 11:28:35 AM
Total Time: 0 hours 0 minutes 37 seconds