about_Automatic_Variables will show you a list of variable that we shouldn’t be using. One of those variable is host. Hence it is impossible to create a function that has a parameter called host. But host is a very common word, and a lot of API are using this name as input …
Question : Is there a way to create a function that can have host as input variable ?
I would like to be able to run this command : myfunction -host myhostname without messing up with the Powershell built-in variable host. I thought about creating an alias [Alias("host")][Parameter(mandatory)][string]$hostX. But by doing so, which is the most elegant solution I found up to now, I would have both host and hostX as autocompletion possible switch, which might bring confusion. I thought about hiding it using DontShow but this is also hiding the Alias.
Anyone would have a solution that would allow to use a function that has a -host switch ?
I don’t think -Host is particularly discoverable. As a PowerShell user, I would be expecting a -ComputerName parameter. Rather than $hostX why not use $ComputerName and alias that?
Even with that solution, I think you’re asking for trouble as you’re inviting users who don’t know that $host is an automatic variable to try things that won’t work like:
Because I am implementing an Open API module using my converter : OpenApi-To-PowerShell. The code is self generated, and I need to write some automation to solve the issue. I thought about having a matching table, but … I would have loved to have the exact same variables as in the Open API configuration file. This brings clarity to whoever whishes to use the converter. Assigning new variable names is bringing confusion, and I wanted to stick as much as possible to the Open API reference.
Actually I am appending an X, exactly like matt suggested. The code is already in github in the last commit. But I would love to have not to do so.
$host = 'myHost' does not work. $host is a protected variable you can not modify.