1

I am trying to write some PowerShell aliases in my profile.ps1 file. The problem is that I can not get commands with a variable number of parameters to work properly.

For example, I want to have an alias that links "kg" to "kubectl get", which is followed by a resource and sometimes an option. So "kg pods -A" and "kg pod" should both work afterwords.

According to this post, I need to define a function that can be seen below, my problem is that it ignores all arguments after the first. So "kg pods -A" returns the result of "kg pods". This post explains how to pass multiple arguments to a function, up it does not work for some reason. (I am using Powershell in Cmder if this is relevant). Could it be that the "-" causes an issue?

The relevant part of the "profile.ps1":

Function get ($restOfLine, $restOfLine2) { kubectl get $restOfLine $restOfLine2}
Set-Alias kg get
Manuel
  • 649
  • 5
  • 22

1 Answers1

2

As soon as you explicitly declare parameters in a function, PowerShell will attempt to bind input arguments to those parameters, by parsing the command invocation expression and treating any sequence starting with - as a parameter name.

Remove the parameters and pass $args directly to avoid this interference:

function get { kubectl get @args }
Set-Alias kg get

kg post -A will now pass post and -A to kubectl as-is

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206