Skip to content

Instantly share code, notes, and snippets.

@altimmons
Created March 31, 2021 02:46
Show Gist options
  • Save altimmons/c888b9c6d063283d1978f90157aeaab3 to your computer and use it in GitHub Desktop.
Save altimmons/c888b9c6d063283d1978f90157aeaab3 to your computer and use it in GitHub Desktop.
A powershell script (which can be installed as a module) to create a Shim (from Chocolatey, requires Chocolatey) into the C:\ProgramData\Chocolatey\bin directory. This directory should be added to path, basically, this script links anything application to path, without making the path search depth excessively deep.

function Make-Shim{
<#
.SYNOPSIS
Creates a shim (or batch redirect) for a file that is on the PATH.
Makes a shim in ProgramData/choco/bin to add an exe to path
.DESCRIPTION
Chocolatey installs have the folder `$($env:ChocolateyInstall)\bin`
included in the PATH environment variable. Chocolatey automatically
shims executables in package folders that are not explicitly ignored,
putting them into the bin folder (and subsequently onto the PATH).
When you have other files you want to shim to add them to the PATH or
if you want to handle the shimming explicitly, use this function.
If you do use this function, ensure you also add `Uninstall-BinFile` to
your `chocolateyUninstall.ps1` script as Chocolatey will not
automatically clean up shims created with this function.
.NOTES
Not normally needed for exe files in the package folder, those are
automatically discovered and added as shims after the install script
completes.
.INPUTS
None
.OUTPUTS
None
.PARAMETER program name
program should be in dir of program and prog relative.
E.g. "...Program Files/Adobe/"
for current position
"./Acrobat.exe" for the target
Other ways may or may not work
.PARAMETER Name
Name of the output, this is what will call the program on the CLI.
Will have .exe appended automatically if not present.
Will use the name of the supplied program if not supplied. e.g. the above
example would be shimmed as Acrobat.exe
.PARAMETER GUI (Switch)
If the program is loading a GUI- prenvents command shell from being
blocked waiting for the GUI app to be shut back down.
.PARAMETER Debug (Switch)
print more verbose output. Depug is also passed to shimgen.
.PARAMETER Icon Path
(abs preffered) path to an icon.
#>
[CmdletBinding()]
param(
[parameter(Mandatory=$true, Position=0)][string] $program,
[parameter(Mandatory=$false, Position=1)][string][AllowNull()]$name = $null,
[Parameter(Position=2, Mandatory=$false, ValueFromPipelineByPropertyName=$true)][Switch]$GUI=$false,
[Parameter(Position=3, Mandatory=$false, ValueFromPipelineByPropertyName=$true)][Switch]$DebugCmdlet=$false,
[parameter(Mandatory=$false, Position=4)][string][AllowNull()]$IconPath = $null
)
write-host ( "GUI= $GUI Debug=$DebugCmdlet. Program=$program of type $($program.GetType()), name=$name")
$pPath = Resolve-Path -Path $program
pushd $env:choco
write-host "test is null"
if($name -eq $null -or $name -eq "" -or $name.Length -lt 3){
Write-Host "name is null"
$name = Split-Path $program -Leaf
Write-Host $name
}elseif(-not $name.EndsWith('.exe')){
Write-Host "name is not null"
$name += '.exe'
}
if($DebugCmdlet){
write-Host $pPath
Write-Host $program
Write-Host $name
}
$outpath = join-path -Path $(gl) -ChildPath $name
$pRel = resolve-path -Path $pPath -Relative
$g = ""
if($GUI){$g += " --gui "}
if($DebugCmdlet){$g += " --debug " }
if ($IconPath -ne $null){
#todo
}
$cmd_string = ".\shimgen.exe -o=`"{0}`" -p=`"{1}`" {2}" -f ($outpath, $pRel, $g)
if($DebugCmdlet){
Write-Host $cmd_string
Write-Host $outpath, $pRel, $g
}
Invoke-Expression -Command $cmd_string -Debug:$Deubg -Verbose:$Deubg
if (test-path -Path $outpath){
Write-Host "shimgen created successfully"
}else{
Write-Host "unknown failure."
}
Invoke-Expression "refreshenv"
popd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment