# Load SSH hosts function Load-SSHDatabase { if (Test-Path $DatabasePath) { return Get-Content $DatabasePath | ConvertFrom-Json } else { Write-Host "Database file $DatabasePath not found." -ForegroundColor Red return @() } } # Tab-completion function function Tab-CompleteSSHConnect { param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) # Load database $hosts = Load-SSHDatabase # Match partial names based on input if ($wordToComplete -ne "") { # Return matching names $hosts | Where-Object { $_.Name -like "$commandName*" } | ForEach-Object { $_.Name } } else { # Return all names if no input is provided $hosts | ForEach-Object { $_.Name } } } # Register tab completion for `sshconnect` Register-ArgumentCompleter -CommandName "sshconnect" -ScriptBlock ${function:Tab-CompleteSSHConnect} # SSH function function sshconnect { param( [Parameter(Position = 0, Mandatory = $true)] [string]$HostName ) # Load the database $hosts = Load-SSHDatabase # Find the host matching the input $hostcon = $hosts | Where-Object { $_.Name -eq $HostName } if ($hostcon) { # Build and execute the SSH command Write-Host "Connecting to $($hostcon.Name) ($($hostcon.Host)) as $($hostcon.User)..." -ForegroundColor Cyan $sshCommand = "ssh {0}@{1}" -f $hostcon.User, $hostcon.Host Invoke-Expression $sshCommand } else { Write-Host "Host '$HostName' not found in database." -ForegroundColor Red } }