mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
New-Guid add -Empty and -InputObject parameters (#20014)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Management.Automation;
|
||||
|
||||
@@ -9,16 +11,48 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// The implementation of the "new-guid" cmdlet.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsCommon.New, "Guid", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097130")]
|
||||
[Cmdlet(VerbsCommon.New, "Guid", DefaultParameterSetName = "Default", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097130")]
|
||||
[OutputType(typeof(Guid))]
|
||||
public class NewGuidCommand : Cmdlet
|
||||
public class NewGuidCommand : PSCmdlet
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a guid.
|
||||
/// Gets or sets a value indicating that the cmdlet should return a Guid structure whose value is all zeros.
|
||||
/// </summary>
|
||||
protected override void EndProcessing()
|
||||
[Parameter(ParameterSetName = "Empty")]
|
||||
public SwitchParameter Empty { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the value to be converted to a Guid.
|
||||
/// </summary>
|
||||
[Parameter(Position = 0, ValueFromPipeline = true, ParameterSetName = "InputObject")]
|
||||
[System.Diagnostics.CodeAnalysis.AllowNull]
|
||||
public string InputObject { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Guid.
|
||||
/// </summary>
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
WriteObject(Guid.NewGuid());
|
||||
Guid? guid = null;
|
||||
|
||||
if (ParameterSetName is "InputObject")
|
||||
{
|
||||
try
|
||||
{
|
||||
guid = new(InputObject);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrorRecord error = new(ex, "StringNotRecognizedAsGuid", ErrorCategory.InvalidArgument, null);
|
||||
WriteError(error);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
guid = ParameterSetName is "Empty" ? Guid.Empty : Guid.NewGuid();
|
||||
}
|
||||
|
||||
WriteObject(guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,17 +2,43 @@
|
||||
# Licensed under the MIT License.
|
||||
Describe "New-Guid" -Tags "CI" {
|
||||
|
||||
It "returns a new guid" {
|
||||
It "Returns a new guid" {
|
||||
$guid = New-Guid
|
||||
$guid | Should -BeOfType System.Guid
|
||||
}
|
||||
|
||||
It "should not be all zeros" {
|
||||
It "Should not be all zeros" {
|
||||
$guid = New-Guid
|
||||
$guid.ToString() | Should -Not -BeExactly "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
|
||||
It "should return different guids with each call" {
|
||||
It "Should be all zeros" {
|
||||
$guid = New-Guid -Empty
|
||||
$guid.ToString() | Should -BeExactly "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
|
||||
It "Should convert a string to a guid" {
|
||||
$guid1 = New-Guid
|
||||
$guid2 = New-Guid -InputObject $guid1.ToString()
|
||||
$guid3 = New-Guid $guid1.ToString()
|
||||
$guid2 | Should -BeOfType System.Guid
|
||||
$guid1.ToString() | Should -BeExactly $guid2.ToString()
|
||||
$guid1.ToString() | Should -BeExactly $guid3.ToString()
|
||||
}
|
||||
|
||||
It "Should convert a string to a guid, value from pipeline" {
|
||||
$guids = '11c43ee8-b9d3-4e51-b73f-bd9dda66e29c','0f8fad5bd9cb469fa16570867728950e' | New-Guid
|
||||
$guids[0].ToString() | Should -BeExactly '11c43ee8-b9d3-4e51-b73f-bd9dda66e29c'
|
||||
$guids[1].ToString() | Should -BeExactly '0f8fad5b-d9cb-469f-a165-70867728950e'
|
||||
}
|
||||
|
||||
It "Should accept pipeline input" {
|
||||
$guids = 1..10 | foreach-object { [guid]::newguid() }
|
||||
$observed = $guids.Foreach({$_.ToString()}) | New-Guid
|
||||
$observed | Should -Be $guids
|
||||
}
|
||||
|
||||
It "Should return different guids with each call" {
|
||||
$guid1 = New-Guid
|
||||
$guid2 = New-Guid
|
||||
$guid1.ToString() | Should -Not -Be $guid2.ToString()
|
||||
|
||||
Reference in New Issue
Block a user