Import packages via CSV

Prev Next

Synopsis

This script will import packages from a simple CSV file. It will use the default PowerShell credential prompt if the username or passwords aren't provided within the script.

Based on the target column, the script will determine whether the package type is "web" or "launch".
Finally, the script will add a snapshot entitled "Imported by PowerShell" to all packages.

Sample script


Param(

    
	[Parameter(Mandatory = $true)]
	[URI]$uri,


	[Parameter()]
	[string]$csvFile = ".\input.csv",

	[Parameter()]
	[string]$username = "",

	[Parameter()]
	[string]$password = ""

)

# Set credentials
if ($username -eq "")
{
	$credentials = Get-Credential
}
else
{
	$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString -String $password -AsPlainText -Force)
}

# Connect to the Application Workspace.
$context = Connect-LiquitWorkspace -URI $uri -Credential $credentials;

# Read packages from the CSV file
$apps = Import-Csv $csvFile;

# Loop through the packages
foreach ($app in $apps)
{

	# Detect a package type
	if ($app.Target.StartsWith("http://", "CurrentCultureIgnoreCase") -or $app.Target.StartsWith("https://", "CurrentCultureIgnoreCase"))
	{
		$type = 'Web'
	}
	else
	{
		$type = 'Launch'
	}

	Write-Host "Importing package with name ""$($app.Name)"" as type ""$type"""

	# Determine Target type.
	try
	{

		# Create a package.
		$package = New-LiquitPackage -Name $app.Name -Type $type  -Offline ($type -eq "Web") -Web ($type -eq "Web")

	}
	catch
	{
        # Only throw if the error message was a duplicate entry
		if (($_.Exception).Code -eq "Request_DuplicateResource")
		{

			# Find the package by name.
			$package = Get-LiquitPackage | Where-Object { $_.Name -eq $app.Name }			

		}
		else
		{
			throw;
		}

	}

	# Create a new empty snapshot (if it doesn't exist).
    $snapshot = $package | New-LiquitPackageSnapshot



    $actionSet = $snapshot | New-LiquitActionSet -Type Launch


	# Add a Web package
	if ($type -eq "Web")
	{

		# Add an action.
		$action = $actionSet | New-LiquitAction -Name "Open URL" -Type "openurl" -Settings @{ url = $app.Target; browser = 0 } -Context User -Enabled $true;

	}
	else
	{

		# Add a Filter Set
		$filterSet = $snapshot | New-LiquitFilterSet

		# Add a File Exists Filter
		$filter = $filterSet | New-LiquitFilter -Type "fileexists" -Settings @{path= $app.Target} -Value "true" -Operator Equal 

		# Add an action.
        $action = $actionSet | New-LiquitAction  -Name "Start Process" -Type "processstart"  -Settings @{ name = $app.Target} -Context User -Enabled $true;

	}

	# Publish the package snapshot
	$snapshot | Publish-LiquitPackageSnapshot -Name "Imported by PowerShell" -stage Production


}

Downloads

Sample csv file

Script

The content in this sample is not supported by Application Workspace. It was contributed by a community member and is published "as is." It seems to have worked for at least one person and might work for you. But please be sure to test, test, test before you do anything drastic with it.