Set-Alias

Set-alias -Name esxi11 -Value Connect-VIServer -Server ESXI-11 -user root -password "***********"

Set-Alias : A parameter cannot be found that matches parameter name ‘Server’.
At line:1 char:48

  • Set-alias -Name esxi11 -Value Connect-VIServer -Server ESXI-11 -u …
  •                                            ~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Set-Alias], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

If I pace quotes around the command it saves it

but will not run

PS C:\util> esxi11 esxi11 : The term ‘Connect-VIServer -Server ESXI-11 -user root -password “************”’ is not recognized as the
name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1

  • esxi11
  •   + CategoryInfo          : ObjectNotFound: (Connect-VIServe...ord "##My95hog":String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    
    

Any ideas?

Thank you

Tom

Is it your “I’m trying to do the impossible” - day? :wink: :smiley:

You cannot provide a complex command including the parameters and values to the parameter -Value of Set-Alias or New-Alias.

You may take a look at $PSDefaultParameterValues. But anyway - you should not at all store your passwords in such a way as it is highly insecure.

Olaf

Why not an Alias command is suppose to help you with making your life simpler with out typing out a long command line.

I do not see how $PSDefaultParameterValuses can help

Also it does not work even if I leave off the -user and the -password

This should work like scriptblock does

Hello Tom,

You can create alias for cmdlet or executable but not for cmdlet with some arguments.

In order to accomplish what you are describing you should use function.

Function CD32 {
Set-Location -Path C:\Windows\System32
}
PS> Set-Alias -Name Go -Value CD32

In your case it will be something like

Function esxi11 {
Connect-VIServer -Server Esxi11 ...
}

Reference:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-alias?view=powershell-7
Hope that helps.

Hmmm yes and no. An alias is a shorter version of the command. Not the command including parameters and values. For example gci is short for Get-ChildItem.

I expected this. :wink: Did you read the information in the link I posted? You could set -Server ESXI-11 as the default parameter for Connect-VIServer. When you additionally create an alias for Connect-VIServer with the name esxi you wouldn’t have to type that much anymore … only the password would be needed then. :wink:

You should complain this to the MS Powershell team. :wink:

Edit:
You still could create a function with everything you want in it.

Olaf,

I am new to this $PSdefaultParameterValues command

PS C:\util> $PSDefaultParameterValues.Add({“ESXI11”})

Cannot find an overload for “Add” and the argument count: “1”.
At line:1 char:1

  • $PSDefaultParameterValues.Add({“ESXI11”})
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    
    
    
    
    

I should be able to do this then if I get the $PSdefaultParameterVaules set right

Set-alias -Name vc1 -Value Connect-VIServer $PSdefaultParametersVaules

An example would be great

Thank you

Tom

All of the answers and examples are in the docs and blog posts everywhere. You’ve been told several times you can’t save arguments in an alias yet you keep trying. You could potentially save it all like you want in a variable (with parameters, an anonymous function) but that’s very advanced in comparison to the commands you’re already struggling with. I feel you would have a lot more success, and therefore a lot more enjoyment, if you slow down and actually learn how powershell and these Cmdlets work. Read and play around. Break things down into smaller chunks. Best of luck Tom.

Guys

I got it working now found nice video with examples of how to use $PSdefaultParameters

$PSDefaultParameterValues =@{“Connect-VIServer:Server”=“ESXI11”}
Set-alias -Name vc1 -Value Connect-VIServer

Now when I type vc1 I get prompted for my credentials which is great.

Just one other question on this.
I have several servers ESXI11 ESXI12 ESXI13 etc.

Any thoughts On how I can setup those When you run $PSDefaultParmeterValues it overlays the previous one if using the same cmdletname

Thanks
Tom

That’s the disadvantage for this case … it’s the DEFAULT.

But I mentioned it before and others as well … you could create functions for this. One function for each server.

Olaf,

Functions was the answer the work great.

Thank you

On to more powershell