1RedOne / 1redone.github.io

FoxDeploy's GitHub.io Page
https://1redone.github.io/
MIT License
6 stars 3 forks source link

Adding tab-completion to your PowerShell Functions #35

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Adding tab-completion to your PowerShell Functions

FoxDeploy.com, Stephen Owen's technical blog about PowerShell, Systems Administration, GUI Design and Programming. .

https://www.foxdeploy.com/blog/adding-tab-completion-to-your-powershell-functions.html

Ayanmullick commented 3 years ago

I'm able to do tab completion for the parameter and the parameter values while running the command from the script pane.

function Hello-World 
    {param( [ValidateSet('Green','Red','Blue')] $Color)
     Write-Host "Hello world!" -Foreground $Color
     }

However, tab completion doesn't work when the file is in the Scripts folder like other scripts installed using Install-Script.

$DestPath = [Environment]::GetFolderPath('MyDocuments')+"\PowerShell\Scripts"
(New-Object System.Net.WebClient).DownloadFile("https://github.com/Ayanmullick/test/raw/master/Hello-World.ps1","$DestPath\Hello-World.ps1")
Invoke-Expression "$DestPath\Hello-World.ps1"
Ayanmullick commented 3 years ago

I'm able to do tab completion for the parameter and the parameter values while running the command from the script pane.

function Hello-World 
    {param( [ValidateSet('Green','Red','Blue')] $Color)
     Write-Host "Hello world!" -Foreground $Color
     }

However, tab completion doesn't work when the file is in the Scripts folder like other scripts installed using Install-Script.

$DestPath = [Environment]::GetFolderPath('MyDocuments')+"\PowerShell\Scripts"
(New-Object System.Net.WebClient).DownloadFile("https://github.com/Ayanmullick/test/raw/master/Hello-World.ps1","$DestPath\Hello-World.ps1")
Invoke-Expression "$DestPath\Hello-World.ps1"

Removing the function wrapper in the file being downloaded and adding the default script folder path to $env:PATH resolved the issue.

If (($env:PATH -split ';') -notcontains $DestPath) {$env:Path += ";$DestPath"}

searbr commented 1 year ago

This was great, thank you on helping me get dynamic tab completion. One part that through me off was that I didn't realize that setting the dynamic param's name is important and must match the name of the variable in the begin.

Set the dynamic parameters' name

$ParameterName = 'Service'

I thought it was just setting the initial value, but if you don't properly set this, the the dynamic portion will fail.