Last active
November 22, 2023 22:04
-
-
Save ninmonkey/ec3783c7eb9e70a9a7d864df44435859 to your computer and use it in GitHub Desktop.
Invoking-Native-Command-Example-Winget.ps1
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
using namespace System.Collections.Generic | |
# $PSStyle.OutputRendering = 'Ansi' | |
@' | |
I used winget as a quick example case. | |
if you're doing more with it, check out the export as json and detailed logging options | |
https://learn.microsoft.com/en-us/windows/package-manager/winget/troubleshooting | |
https://learn.microsoft.com/en-us/windows/package-manager/winget/export#exporting-files | |
'@ | |
function GetBin { | |
<# | |
.NOTES | |
using Get-Command -CommandType Application is usually good enough. here's a slightly stricter version | |
#> | |
param( | |
[string]$Name, | |
[switch]$AsPattern, | |
[Alias('One')] | |
[switch]$FirstOnly | |
) | |
if( $FirstOnly ){ | |
$ExecutionContext.InvokeCommand.GetCommands($Name, 'Application', $AsPattern) | |
| Select -first 1 | |
return | |
} | |
$ExecutionContext.InvokeCommand.GetCommands('python', 'Application', $AsPattern) | |
} | |
function InvokeWinget { | |
<# | |
.SYNOPSIS | |
using @() expressions to build the arguments | |
#> | |
param( | |
[string]$Query | |
) | |
[List[Object]]$BinArgs = @() | |
$Config = @{ | |
VerboseLogs = $True | |
DisableInteractive = $True | |
} | |
# Option A) | |
# You can build native commands up using an implicit array | |
# and conditions inside | |
$BinArgs = @( | |
'list' | |
if( -not [String]::IsNullOrWhiteSpace( $Query )) { | |
'--query' | |
$Query | |
} | |
if( $Config.VerboseLogs ) { '--verbose-logs' } | |
if( $Config.DisableInteractive ) { '--disable-interactivity' } | |
) | |
$BinArgs | Join-String -op "toInvoke`n winget " -sep ' ' | write-host -back 'blue' | |
} | |
function InvokeWingetList { | |
<# | |
.SYNOPSIS | |
using [List]s to build the arguments as arrays | |
.NOTES | |
I'm using [List[Object]] rather than [List[String]] on purpose | |
it makes it easier to use expressions without getting errors. | |
now you can use pretty much anything like: | |
List.AddRange( @( | |
# ... | |
) ) | |
#> | |
param( | |
[ArgumentCompletions( | |
'Microsoft.PowerShell', | |
'junegunn.fzf', | |
'DaxStudio.DaxStudio', | |
'Microsoft.PowerBI' | |
)] | |
[string]$Query | |
) | |
[List[Object]]$BinArgs = @() | |
$Config = @{ | |
VerboseLogs = $True | |
DisableInteractive = $True | |
} | |
# Option B) | |
# or using an array/list | |
[List[Object]]$BinArgs = @( 'list' ) | |
# You can use essentially the same thing wrapped in one AddRange@() | |
$BinArgs.AddRange(@( | |
if( -not [String]::IsNullOrWhiteSpace( $Query )) { | |
'--query' | |
$Query | |
} | |
if( $Config.VerboseLogs ) { '--verbose-logs' } | |
if( $Config.DisableInteractive ) { '--disable-interactivity' } | |
)) | |
$BinArgs | Join-String -op "toInvoke1`n winget " -sep ' ' | write-host -back 'blue' | |
# $winget = GetBin -name 'WinGet' -FirstOnly | |
# & $winget @BinArgs | |
# Option C) | |
## Or if it makes more sense to build them up 1 at a time, use Add() | |
[List[Object]]$BinArgs2 = @( 'list' ) | |
if( -not [String]::IsNullOrWhiteSpace( $Query )) { | |
$BinArgs2.Add( '--query' ) | |
$BinArgs2.Add( $Query ) | |
} | |
if( $Config.VerboseLogs ) { | |
$BinArgs2.Add('--verbose-logs' ) | |
} | |
if( $Config.DisableInteractive ) { | |
$BinArgs2.Add('--disable-interactivity' ) | |
} | |
$BinArgs | Join-String -op "toInvoke2`n winget " -sep ' ' | write-host -back 'blue' | |
$winget = GetBin -name 'WinGet' -FirstOnly | |
& $winget @BinArgs2 | |
} | |
InvokeWingetList Microsoft.PowerShell | |
# or | |
# "to invoke:`n winget {0}" -f @( | |
# $BinArgs -join ' ' | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment