# Add-W10MAppPackage.ps1 # # PowerShell script partially based on the autogenerated Add-AppDevPackage.ps1 to facilitate Windows 10 Mobile application # package deployment to devices. Made by Rodrigo 'r2d2rigo' Diaz # # Copy to a folder containing a .appx ARM package and run, it will automatically deploy the application package to # a Windows 10 Mobile device connected to the computer running the script. # # Parameters: # -Force (boolean): skips any user input for finishing execution. # -WindowsSdkPath (string): path to the WinAppDeployCmd.exe tool in case it's not installed in the default location. # -DeviceIp (string): IP address of the mobile device the application will be deployed to. param ( [switch]$Force = $false, [string]$WindowsSdkPath = $null, [string]$DeviceIp = $null ) $ErrorActionPreference = "Stop" $ProgramFilesPath = ${Env:ProgramFiles(x86)} if (!$ProgramFilesPath) { $ProgramFilesPath = ${Env:ProgramFiles} } $DefaultWindowsSdkPath = Join-Path $ProgramFilesPath "\Windows Kits\10\bin\x86" $WinAppDeployCmdToolFilename = "WinAppDeployCmd.exe" $DefaultDeviceIp = "127.0.0.1" $ErrorCodes = Data { ConvertFrom-StringData @' Success = 0 NoScriptPath = 1 NoPackageFound = 2 ManyPackagesFound = 3 SdkNotFound = 4 DeployToolNotFound = 5 '@ } $ErrorMessages = Data { ConvertFrom-StringData @' Success = Application installation finished. NoScriptPath = ERROR: Path for script couldn't be found. NoPackageFound = ERROR: Application package could not be found. ManyPackagesFound = ERROR: Found more than one application package in the specified directory. SdkNotFound = ERROR: Windows 10 SDK is not installed in specified location. DeployToolNotFound = ERROR: WinAppDeployCmd tool not found in specified directory. '@ } function PrintErrorAndExit($ErrorMessage, $ErrorCode) { Write-Host ([Environment]::NewLine + $ErrorMessage + [Environment]::NewLine) -foregroundcolor Red if (!$Force) { Pause } exit $ErrorCode } function PrintMessageAndExit($Message, $ExitCode) { Write-Host ([Environment]::NewLine + $Message + [Environment]::NewLine) if (!$Force) { Pause } exit $ExitCode } $ScriptPath = $null try { $ScriptPath = (Get-Variable MyInvocation).Value.MyCommand.Path $ScriptDir = Split-Path -Parent $ScriptPath } catch {} if (!$ScriptPath) { PrintErrorAndExit $ErrorMessages.NoScriptPath $ErrorCodes.NoScriptPath } # Set default Windows SDK path if no custom one has been defined if (!$WindowsSdkPath) { $WindowsSdkPath = $DefaultWindowsSdkPath } # Check that the Windows SDK folder exists if (!(Test-Path $WindowsSdkPath)) { PrintErrorAndExit ($ErrorMessages.SdkNotFound + " " + $WindowsSdkPath) $ErrorCodes.SdkNotFound } # Check that the WinAppDeployCmd.exe tool exists in the specified path $WinAppDeployCmdToolPath = Join-Path $WindowsSdkPath $WinAppDeployCmdToolFilename if (!(Test-Path $WinAppDeployCmdToolPath)) { PrintErrorAndExit ($ErrorMessages.DeployToolNotFound + " " + $WinAppDeployCmdToolPath) $ErrorCodes.DeployToolNotFound } # List all .appx files in the script directory $PackagePath = Get-ChildItem (Join-Path $ScriptDir "*.appx") | Where-Object { $_.Mode -NotMatch "d" } # List all .appxbundle files in the script directory $BundlePath = Get-ChildItem (Join-Path $ScriptDir "*.appxbundle") | Where-Object { $_.Mode -NotMatch "d" } $NumberOfPackages = $PackagePath.Count + $BundlePath.Count; # There must be exactly 1 package/bundle if ($NumberOfPackages -lt 1) { PrintErrorAndExit $ErrorMessages.NoPackageFound $ErrorCodes.NoPackageFound } elseif ($NumberOfPackages -gt 1) { PrintErrorAndExit $ErrorMessages.ManyPackagesFound $ErrorCodes.ManyPackagesFound } if ($PackagePath.Count -eq 1) { $DeveloperPackagePath = $PackagePath Write-Host "Application package found:" $DeveloperPackagePath.FullName } else { $DeveloperPackagePath = $BundlePath Write-Host "Application bundle found: " $DeveloperPackagePath.FullName } if (!$DeviceIp) { $DeviceIp = $DefaultDeviceIp } # Call WinAppDeployCmd.exe with the specified parameters to deploy application package $Arguments = "install -file """ + $DeveloperPackagePath + """ -ip " + $DeviceIp Write-Host "Executing" $WinAppDeployCmdToolPath $Arguments Start-Process $WinAppDeployCmdToolPath $Arguments -NoNewWindow -Wait PrintMessageAndExit $ErrorMessages.Success $ErrorCodes.Success