Skip to main content

Modified AppAttach Scripts

These scripts are a modified version of four powershell scripts created by Ryan Mangan.

These scripts rely on a JSON config file to mount, register and provision packages. They have been modified by us to accept the config file from a command-line argument rather than being hardcoded. This allows us to use the same scripts to provision different sets of packages at a time.

Stage

<#  
    .NOTES
    ===========================================================================
     Created on:    19/08/2020 20:31
     Created by:    Ryan Mangan
     Organization:  Ryan Mangans IT Blog ltd
   Website:       https://ryanmangansitblog.com
     Filename:      AppattachStage.ps1
    ===========================================================================
    .DESCRIPTION
        MSIX App Attach Staging Script

        This script reads the Json Configuration file, mounts the VHD with no Drive letter and as read only.
        A new MSIX Junction is created with the package name
        Package Manager is run for staging the package (Windows.Management.Deployment.PackageManager)

        Point to note, ensure the VHD and json are in the same location as the script for this to work correctly.


        Its recommended that all scripts are signed to remove the need to elevate or change the remote execution policy on the host this script is being run.
#>

param(
    [string]$configPath = $null
)

if ($null -eq $configPath)
{
    throw "Please provide a path to a config file";
}

$configFile = Resolve-Path $configPath | Get-Content -Raw | ConvertFrom-Json;

foreach ($package in $configFile)
{
    $vhdSrc = Join-Path $PSScriptRoot $package.vhdFileName;
    $packageName = $package.packageName;
    $parentFolder = "\" + $package.parentFolder + "\";
    $volumeGuid = $package.volumeGuid;
    $msixJunction = $package.msixJunction;

    try
    {
        Mount-Diskimage -ImagePath $vhdSrc -NoDriveLetter -Access ReadOnly;
        Write-Host ("Mounting of " + $vhdSrc + " was completed!") -BackgroundColor Green;
    }
    catch
    {
        Write-Host ("Mounting of " + $vhdSrc + " has failed!") -BackgroundColor Red;
    }

    $msixDest = "\\?\Volume{" + $volumeGuid + "}\";

    if (!(Test-Path $msixJunction))
    {
        New-Item $msixJunction -ItemType Directory;
    }

    $msixJunction = Join-Path $msixJunction $packageName;
    if (Test-Path $msixJunction)
    {
        throw "The path $msixJunction already exists!";
    }

    cmd.exe /c mklink /j $msixJunction $msixDest

    $lec = $LASTEXITCODE;
    if (0 -ne $lec)
    {
        throw "mklink returned exit code $lec";
    }

    [Windows.Management.Deployment.PackageManager, Windows.Management.Deployment, ContentType = WindowsRuntime] | Out-Null;

    Add-Type -AssemblyName System.Runtime.WindowsRuntime;

    $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.ToString() -eq 'System.Threading.Tasks.Task`1[TResult] AsTask[TResult,TProgress](Windows.Foundation.IAsyncOperationWithProgress`2[TResult,TProgress])' })[0];
    $asTaskAsyncOperation = $asTask.MakeGenericMethod([Windows.Management.Deployment.DeploymentResult], [Windows.Management.Deployment.DeploymentProgress]);

    $packageManager = [Windows.Management.Deployment.PackageManager]::new();
    $path = $msixJunction + $parentFolder + $packageName;
    $path = ([System.Uri]$path).AbsoluteUri;
    $asyncOperation = $packageManager.StagePackageAsync($path, $null, "StageInPlace");
    $task = $asTaskAsyncOperation.Invoke($null, @($asyncOperation));
    $task;
}

Register

<#  
    .NOTES
    ===========================================================================
     Created on:    19/08/2020 20:31
     Created by:    Ryan Mangan
     Organization:  Ryan Mangans IT Blog ltd
     website:       https://ryanmangansitblog.com
     Filename:      AppattachRegister.ps1
    ===========================================================================
    .DESCRIPTION
        MSIX App Attach Register Script

        This script reads the Json Configuration file, and
        Registers the Application for the user. 

        Point to note, ensure the VHD and json are in the same location as the script for this to work correctly.


        Its recommended that all scripts are signed to remove the need to elevate or change the remote execution policy on the host this script is being run.
#>

param(
    [string]$configPath = $null
)

if ($null -eq $configPath)
{
    throw "Please provide a path to a config file";
}

$configFile = Resolve-Path $configPath | Get-Content -Raw | ConvertFrom-Json;

foreach ($package in $configFile)
{
    $packageName = $package.packageName;
    $path = Join-Path $env:ProgramFiles ("WindowsApps\" + $packageName + "\AppxManifest.xml");
    Add-AppxPackage -Path $path -DisableDevelopmentMode -Register;
}

Deregister

<#  
.NOTES
===========================================================================
 Created on:    19/08/2020 20:31
 Created by:    Ryan Mangan
 Organization:  Ryan Mangans IT Blog ltd
 website:       https://ryanmangansitblog.com
 Filename:      AppattachDeregister.ps1
===========================================================================
.DESCRIPTION
    MSIX App Attach Deregister Script

    This script reads the Json Configuration file, and
    Deregisters the Application for the user. 

    Point to note, ensure the VHD and json are in the same location as the script for this to work correctly.


    Its recommended that all scripts are signed to remove the need to elevate or change the remote execution policy on the host this script is being run.
#>

param(
[string]$configPath = $null
)

if ($null -eq $configPath)
{
throw "Please provide a path to a config file";
}

$configFile = Resolve-Path $configPath | Get-Content -Raw | ConvertFrom-Json;

$configFile = Get-Content $configFPath -Raw | ConvertFrom-Json;

foreach ($package in $configFile)
{
$packageName = $package.packageName;
Remove-AppxPackage -PreserveRoamableApplicationData $packageName
}

Destage

<#  
    .NOTES
    ===========================================================================
     Created on:    19/08/2020 20:31
     Created by:    Ryan Mangan
     Organization:  Ryan Mangans IT Blog ltd
     website:   https://ryanmangansitblog.com
     Filename:      AppattachDestage.ps1
    ===========================================================================
    .DESCRIPTION
        MSIX App Attach Destage Script

        This script reads the Json Configuration file, and
        Destage's the Application and MSIX Junction from the host. 

        Point to note, ensure the VHD and json are in the same location as the script for this to work correctly.


        Its recommended that all scripts are signed to remove the need to elevate or change the remote execution policy on the host this script is being run.
#>

param(
    [string]$configPath = $null
)

if ($null -eq $configPath)
{
    throw "Please provide a path to a config file";
}

$configFile = Resolve-Path $configPath | Get-Content -Raw | ConvertFrom-Json;

foreach ($package in $configFile)
{
    $vhdSrc = Join-Path $PSScriptRoot $package.vhdFileName;
    $packageName = $package.packageName;
    $msixJunction = Join-Path $package.msixJunction $packageName;

    Remove-AppxPackage -AllUsers -Package $packageName;
    Dismount-DiskImage -ImagePath $vhdSrc;

    if (Test-Path $msixJunction)
    {
        Remove-Item $msixJunction -Force -Recurse;
    }
}
This page was last reviewed on 26 January 2024. It needs to be reviewed again on 26 January 2025 by the page owner platops-build-notices .
This page was set to be reviewed before 26 January 2025 by the page owner platops-build-notices. This might mean the content is out of date.