Last active
June 1, 2024 06:39
-
-
Save jimkring/b9989e17c028f10cc882571fc56f07bb to your computer and use it in GitHub Desktop.
PowerShell script to modify a File nipkg to show in Add or Remove Programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2024 JKI. All rights reserved. | |
# Licensed under the MIT License. | |
### About ### | |
# This a powershell script to modify a .nipkg file such that the packaged LabVIEW executable application shows in the Windows Add or Remove Programs list | |
# See NIPM docs on this feature here: | |
# https://www.ni.com/docs/en-US/bundle/package-manager/page/instructions-xml-file-packages.html#GUID-4607C6C4-C7F0-4544-93CD-F098A4271D43__GUID-18C9D8AE-B61B-41B2-8D54-4B604F34D18D | |
### How this Script Works ### | |
# - unpacks the .nipkg file (using `nipkg.exe unpack`) | |
# - modifies the `instructions` xml file, adding an `<osUninstallEntry ux="oem"/>` item to `<instructions/>` | |
# - repacks the .nipkg file (using `nipkg.exe pack`) | |
# Usage: | |
# see the usage message below, which is shown if no arguments are provided to this script | |
[CmdletBinding()] | |
param ( | |
[string]$nipkgPath, | |
[string]$ux = "oem" | |
) | |
# Usage:if no arguments are provided, display the usage | |
if ($args.Length -eq 0) { | |
Write-Host "Error: No arguments provided" | |
Write-Host "Usage: .\set_nipkg_os_uninstall_entry.ps1 -nipkgPath <path> [-ux <oem>] [-ux <ni>]" | |
Write-Host " -nipkgPath: path to the .nipkg file" | |
Write-Host " -ux: [Optional] user experience (oem|ni) where oem is the default value" | |
Write-Host " oem: Hides all dialog text and links referring to NI during a modify or uninstall operation." | |
Write-Host " ni: Shows normal Package Manager interface elements during a modify or uninstall operation." | |
exit 1 | |
} | |
# convert to path | |
$nipkgPath = Convert-Path $nipkgPath | |
# Check if the nipkgPath is a valid file | |
if (-not (Test-Path $nipkgPath)) { | |
Write-Host "Error: '$nipkgPath' is not a valid file path" | |
exit 1 | |
} | |
# Check if the nipkgPath is a valid nipkg file | |
if (-not ($nipkgPath -like "*.nipkg")) { | |
Write-Host "Error: '$nipkgPath' is not a valid nipkg file" | |
exit 1 | |
} | |
# nipkg.exe program path | |
$nipkgExe = "C:\Program Files\National Instruments\NI Package Manager\nipkg.exe" | |
# temporary unpack directory (same location as the nipkg file, but with a `.unpacked` extension) | |
$unpackDir = "$nipkgPath.unpacked" | |
# Execute the nipkg.exe unpack command | |
Write-Debug "Unpacking '$nipkgPath'" | |
& $nipkgExe unpack $nipkgPath $unpackDir | |
# Check if the unpack directory exists | |
if (-not (Test-Path $unpackDir)) { | |
Write-Host "Error: Unpack directory $unpackDir does not exist" | |
exit 1 | |
} | |
# `instructions` xml file path | |
$instructionsPath = "$unpackDir\data\instructions" | |
# Check if the instructions file exists | |
if (-not (Test-Path $instructionsPath)) { | |
Write-Host "Error: Instructions file $instructionsPath does not exist" | |
exit 1 | |
} | |
Write-Debug "Modifying package instructions xml file" | |
# Load the instructions xml file | |
[xml]$instructions = Get-Content $instructionsPath | |
# Check if the `osUninstallEntry` element exists | |
if (-not $instructions.instructions.osUninstallEntry) { | |
# Add the `osUninstallEntry` element | |
$osUninstallEntry = $instructions.CreateElement("osUninstallEntry") | |
$osUninstallEntry.SetAttribute("ux", $ux) | |
$instructions.instructions.AppendChild($osUninstallEntry) | |
} else { | |
# Modify the `osUninstallEntry` element | |
$instructions.instructions.osUninstallEntry.SetAttribute("ux", $ux) | |
} | |
# Save the modified instructions xml file | |
Write-Debug "Saving modified package instructions xml file" | |
$instructions.Save($instructionsPath) | |
# move/rename the original nipkg file to a backup file | |
Write-Debug "Backing up the original nipkg file" | |
$backupNipkgPath = "$nipkgPath.bak" | |
Move-Item $nipkgPath $backupNipkgPath -Force | |
# Execute the nipkg.exe pack command and repack the nipkg file into the original nipkg file's director | |
Write-Debug "Repacking the nipkg file" | |
$parentDir = Split-Path $nipkgPath -Parent | |
& $nipkgExe pack $unpackDir $parentDir | |
# Check if the repacked nipkg file exists | |
Write-Debug "Verifying repacked nipkg file" | |
if (-not (Test-Path $nipkgPath)) { | |
Write-Host "Error: Repacked nipkg file $nipkgPath does not exist" | |
exit 1 | |
} | |
# Remove the unpack directory | |
Write-Debug "Removing the unpack directory" | |
Remove-Item $unpackDir -Recurse | |
Write-Debug "Removing the backup nipkg file" | |
Remove-Item $backupNipkgPath | |
Write-Host "Done" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment