What's the correct PowerShell verb / pattern for get-or-create?

I want to create several PowerShell functions that create an item, or return an existing item if it already exists. For example. login to database, folder, etc.

What is the correct verb for this? Not Ensure

in Approved Verbs , and I'm not sure what else applies. I considered New-X -IfExists ReturnExisting

, but I would rather follow the general scheme instead of inventing my own.

Are there any built-in PowerShell commands that get or build so I can copy the approach?

+3


source to share


4 answers


To the approved list of verbs :



New and set
New verb is used to create a new resource. The Set verb is used to modify an existing resource, optionally creating a resource if it doesn't exist, such as the Set-Variable cmdlet.

+2


source


Use Get-Verb

to view a list of all approved verbs. But for your case, I would probably split the function into two functions. That is, if you care about PowerShell idioms. Use "Create" to create and "Get" to get an existing one. OTOH, if it's just for you, use whatever you like. :-)



+1


source


Ok with the folder, I would guess New

, because you are creating a resource and you can program your function so that if it already exists, you return the object rather than re-creating it.

I have a function called the New-Box

function to create a new mailbox in our exchange environment, if the mailbox already exists, I get my function to return the details and then I try to create a new one.

"A new verb is used to create a new resource.

0


source


You can easily get a nit picky with this, but I feel like Resolve could be something that could satisfy both needs and use in its absence. While it is not only suitable for its other cmdlet Resolve-Path

, you can argue that you are trying to resolve (question, question of uncertainty, etc.) finally; settle; solve the problem of. It's definitely a matter of personal choice, and as Keith Hill said: to each his own.

source

0


source







All Articles