Set-Alias

Welcome Forums General PowerShell Q&A Set-Alias

Viewing 9 reply threads
  • Author
    Posts
    • #233467
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor
      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: (:) [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

    • #233473
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

      Is it your “I’m trying to do the impossible” – day? 😉 😀

      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.

    • #233482
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      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

      • This reply was modified 1 month ago by Tom Grassi.
    • #233488
      Participant
      Topics: 0
      Replies: 29
      Points: 272
      Helping Hand
      Rank: Contributor

      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.

      • This reply was modified 1 month ago by AndySvints.
    • #233494
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

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

      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 do not see how $PSDefaultParameterValuses can help

      I expected this. 😉 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. 😉

      This should work like scriptblock does

      You should complain this to the MS Powershell team. 😉

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

    • #233497
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      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

    • #233545
      Participant
      Topics: 3
      Replies: 421
      Points: 1,479
      Helping Hand
      Rank: Community Hero

      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.

    • #233635
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      Guys

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

      $PSDefaultParameterValues [email protected]{“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

    • #233638
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,011
      Helping Hand
      Rank: Community MVP

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

      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.

    • #233650
      Participant
      Topics: 34
      Replies: 110
      Points: 565
      Rank: Major Contributor

      Olaf,

      Functions was the answer the work great.

      Thank you

      On to more powershell

Viewing 9 reply threads
  • You must be logged in to reply to this topic.