locked
Guidance for writing a Connect- Cmdlet RRS feed

  • Question

  • Hi gang,

    I'm considering writing a collection of PowerShell Cmdlets to manage instances of an online service, and since they all require an instance URL and a Token for authentication, I thought it'd make sense to have a "Connect-" Cmdlet that you run first (similar to "Connect-AzureAD" etc).

    Is there any official guidance out there on how to write these "Connect-" Cmdlets? Like if I were to invoke my hypothetical Cmdlet like this:

    >Connect-Whatever -Url $url -Credential $creds

    ... how do I persist the information so that subsequent "-Whatever" Cmdlets can access it? What exactly does a "Connect-" Cmdlet do?

    Thanks,
    Matt

    Thursday, December 12, 2019 4:21 AM

All replies

  • Your question indicates that you do not have any basic PowerShell or Windows training or experience. The question, as asked, cannot be answered.

    Please review the following to understand how to ask a technical question and to undersand the minimum requierments to maqke your question understandable.

    I also recommend taking some time to learn basic PowerShell as it will help you to ask a good technical question. 

    As for the part that is understandable I have to ask what you mean by "persist".  Windows has many methods for persisting data. You will have to choose the method.  Note that we do not "persist" account passwords unless they are encrypted and that most online services have methods for persisting a connection.  You will have to handle this on a case-by-case basis.  There is no one-size-fits-all.  The question has been asked here many times.  Only specific situations can be addressed.


    \_(ツ)_/

    Thursday, December 12, 2019 6:05 AM
  • I use VMware PowerCLI quite often, and they have a Connect-VIServer cmdlet that you may want to model:

    https://blogs.vmware.com/PowerCLI/2013/03/back-to-basics-connecting-to-vcenter-or-a-vsphere-host.html

    Also perhaps:

    https://code.vmware.com/docs/10197/cmdlet-reference/doc/Connect-VIServer.html

    I don't know if VMware publishes their source code.


    • Edited by Marco.Shaw Thursday, December 12, 2019 9:33 PM
    Thursday, December 12, 2019 9:33 PM
  • Thanks Marko - you appear to have answered a question that the moderators have deemed unanswerable, so that's quite an achievement. :)

    I've discovered $PsCmdlet.SessionState and have had some success with that - I'll take a look at the links you've provided too.

    I think our mate jrv needs a lesson in diplomacy - I'm unlikely to ever visit this forum again after that reply. Oh well.

    Friday, December 13, 2019 8:32 AM
  • Thanks Marko - you appear to have answered a question that the moderators have deemed unanswerable, so that's quite an achievement. :)

    I've discovered $PsCmdlet.SessionState and have had some success with that - I'll take a look at the links you've provided too.

    I think our mate jrv needs a lesson in diplomacy - I'm unlikely to ever visit this forum again after that reply. Oh well.

    On the contrary. The question you asked was not about crating a specific CmdLet but about how to create a single CmdLet that can manages all connections. Of course you can crate a CmdLet that connects to a specific service.   If that is what you had asked then any CmdLet templae will work but what is used for VMWare will not work for other online services. 

    If you had read the links I posted perhaps you could have reworded your question so that it would be more specific to a single issue.

    There is nothing special about a CmdLet that connects to a service. It is just a CmdLet.  Almost all services that are available already have that CmdLet which makes your question even more vague.

    Marko took a guess but it will not help you with most services.  You will have to learn the service and understand what is required for a connection if there is not a module available.

    Learning PowerShell fully will help you better understand the technology and will allow you to understand what a CmdLet is.  Once you understand what a CmdLet name is and why you will begin to understand why your question, as asked, cannot be answered.  The only rules that apply are the rules of how to create any CmdLet.  The real name of a scripted CmdLet is "Advanced Function".

    For help with understanding these items read the following:

    help about_Functions_Advanced
    help about_function*


    \_(ツ)_/

    Friday, December 13, 2019 8:47 AM