Sunday, October 2, 2022
HomePowershellModifying the New-Guid Cmdlet - tommymaynard.com

Modifying the New-Guid Cmdlet – tommymaynard.com


I’m deep within the weeds proper now writing new content material about my expertise with Azure and Azure PowerShell. So, what a greater time to have an thought, write a fast operate, after which create its personal put up and publish it. Or, possibly not. It’s fast and straightforward and I feel there are folks for which this is likely to be good. You possibly can create a operate that can run rather than a PowerShell cmdlet, so as to add extra options, take them away, or no matter different motive, or causes, one might need. That stated, and you could discover this momentarily, simply because my operate of the identical identify runs as a substitute of the cmdlet, doesn’t imply there isn’t a approach to run the unique cmdlet.

As one would possibly count on, GUIDs are used throughout Azure for identification functions. I’m working into them all over the place. I don’t need to publish the GUIDs related to my Tenant, Subscription, and many others., so I’ve been changing mine with this: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Effectively, since I exploit PowerShell to shave off all of the seconds I can, I now have a operate that can give me an X GUID–we’ll name it–if that’s what I need. We’ll begin with an instance of it getting used, which is able to then be adopted by the code to make that occur. If you happen to’re curious why my New-Guid operate runs rather than the cmdlet with the identical identify from the Microsoft.PowerShell.Utility PowerShell module, then learn up on the PowerShell command priority guidelines. That is what to recollect in regards to the order, nonetheless:

  1. Alias
  2. Operate
  3. Cmdlet
  4. Exterior executable information (applications and non-PowerShell scripts)

New-Guid

Guid
----
d6dde62e-46fc-4f37-b75a-c116f003a270

New-Guid -x

Guid
----
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx


And, right here’s the operate that makes the above invocations a risk. I’ll drop it in my profile script after which it’ll be accessible in my PowerShell session proper after I want it, similar to New-Guid already was.


operate New-Guid {
    [CmdletBinding()]
    Param (
        [Parameter()]
        [switch]$x
    )

    if ($x)
    {
        [PSCustomObject]@{'Guid' = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}
    }
    else
    {
        Microsoft.PowerShell.UtilityNew-Guid
    }
}

If you happen to’re like me, maybe you simply had an thought. Enable it to just accept any single character and create the GUID with the character. Okay, again to what I ought to be doing with this time.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments