我发现我经常需要随机数据集来测试或用于我的演示中的例子。我最喜欢的工具是 名称 。这是一个由PowerShell模块编写的 Doug Finke. 这使得它超级易于创造出良好的外观而是随机数据。

PS:> Invoke-Generate '[person]' -Count 3
Heather Rogers
John Bailey
Julia Perez

指数

安装名称

该模块已发布到Psgallery。我们所要做的就是安装它。

    Install-Module  名称  -Scope CurrentUser

调用生成

调用生成 是这个模块的主主页。通过在没有任何参数的情况下调用它,我们得到一个随机文本字符串。

PS:> Invoke-Generate
lhcqalmf

当我们开始提供它们中的模式的模板字符串时,真正的魔法就会发生。经常,我们有一种模式,名称可以让我们构建。在这里,我们为随机数使用“?”和“#”。

PS:> Invoke-Generate "cafe###-???"
cafe176-yhj

模板功能

还支持常用模式的在线模板函数。如果我们需要一个随机名称和地址:

PS:> Invoke-Generate "[person], [address]"
Sarah Garcia, 10096 Tililebuik Commons

我使用nameit的原因是因为它包括易于使用的一组很好的模板函数。

`[alpha]`: selects a random character (constrained by the -Alphabet parameter).
`[numeric]`: selects a random numeric (constrained by the -Numbers parameter).
`[vowel]`: selects a vowel from a, e, i, o or u.
`[phoneticVowel]`: selects a vowel sound, for example ou.
`[consonant]`: selects a consonant from the entire alphabet.
`[syllable]`: generates (usually) a pronounceable single syllable.
`[synonym word]`: finds a synonym to match the provided word.
`[person]`: generate random name of female or male based on provided culture like <FirstName><Space><LastName>.
`[person female]`: generate random name of female based on provided culture like <FirstName><Space><LastName>.
`[person male]`: generate random name of male based on provided culture like <FirstName><Space><LastName>.
`[address]`: generate a random street address. Formatting is biased to US currently.
`[guid]`: generates a random GUID.
`[randomdate]`: generates a random date.
`[state]`: generates a random US state. supports specifying abbr, zip, capital.

音节

音节模板函数将生成看起来像单词的数据,而是发音,更容易记住真正的随机。

PS:> Invoke-Generate '[syllable][syllable][syllable][numeric][numeric]' -Count 5
ugderip87
gedicwa11
haermi85
uksexpop29
jursejcab72

生成随机名称是随机数据集中最常见的数据元素之一。这是来自文章的开始的示例。

PS:> Invoke-Generate '[person]' -Count 3
Heather Rogers
John Bailey
Julia Perez

Some of these template functions support parameters. The [person] template allows you to specify female or male names.

PS:> Invoke-Generate '[person female]' -Count 3
Natasha James
Christine Jenkins
Jennifer Nguyen

PS:> Invoke-Generate '[person male]' -Count 3
Joshua Richardson
Joseph Diaz
Luis Clark

自定义数据集

nameit在数据集中构建的基本集合,但我们可以创建自己的。这些自定义数据集可以像其他模板函数一样使用。为此,我们必须以特殊方式制作哈希表。

    $CustomData = @{
        color   = @('Red','Green','Blue','Black','White')
        weekday = @('Monday','Tuesday','Wednesday','Thursday','Friday')
        team    = @('IT','Accounting','Marketing','Shipping','Administration','Sales')     
    }

Each key of the hashtable becomes a new template function. The values of the key are then randomly selected. In the above example, I have three sets of data called color,weekday, and team. We can then provide this value to 调用生成 to get a random value from that list.

PS:> Invoke-Generate "[color] [weekday] [team]" -CustomData $CustomData
Green Tuesday Marketing

随机对象

One interesting feature is the ability for 调用生成 -AsPSObject to create PSObjects for you. It uses ConvertFrom-StringData internally to try and convert the string into an object. This generally expects you to create key value pairs for it to parse.

    $template = @"
        name    = [person]
        address = [address]
    "@
     调用生成  $Template -AsPSObject -Count 3

这将自动为我们创建这些对象。

name              address      
----              -------      
Christopher Scott 130 Buin Loop
Jonathan Flores   110 Nehle Clb
Jasmine Evans     68 Yegeh Blvd

杰森

如果您发现使用扁平键值对过于限制,那么我们可以始终生成可以转换为对象的JSON。

    $template = @"
    {
        "name" : "[person]",
        "address" : {
            "street":"[address]",
            "state":"[state abbr]"
        }
    }
    "@
     调用生成  $Template -Count 3 | 
        ForEach-Object {ConvertFrom-Json $_}

I removed the -AsPSObject this time because I want the raw string output.

    {
        "name" : "Travis Turner",
        "address" : {
            "street":"489 Juhalpipreduq Sta",
            "state":"AL"
        }
    }

I can then pass this JSON to ConvertFrom-JSON to get my desired object.

    name          address                                  
    ----          -------                                  
    Travis Turner @{street=489 Juhalpipreduq Sta; state=AL}
    Jesse Johnson @{street=44283 Zano Lane; state=PA}      
    Anthony Baker @{street=38215 Zulujjalga Port; state=MO}

把事情简单化

创建具有随机属性的对象也没有任何问题。

    [pscustomobject]@{
        name = Invoke-Generate '[person]'
        address = @{
            street = Invoke-Generate '[address]'
            state  = Invoke-Generate '[state abbr]'
        }
    }

我不必以这种方式制作一些特殊的字符串。这是所有Powershell。这正是我如何创建我的测试对象。

作出的名称

以下是一些我从我的脚本中退出我的脚本,在那里使用nameit。

示例服务器信息

第一个是假服务器报告。显示系统所有者和一些通用审计信息的东西。

[pscustomobject]@{
    ComputerName = Invoke-Generate "Server-[state abbr]##"
    Owner        = Invoke-Generate "[person]"
    Phone        = Invoke-Generate "###-###-####"
    LastUpdate   = Invoke-Generate "[randomdate]"
    Status       = Invoke-Generate '[status]' -CustomData @{
        status = @('Secure','Unpatched','Unsecure')
    }
}

I do get a little clever with generating the status by using custom data inline. This may have been easier to just use a Get-Random. It would have done the same thing.

@('Secure','Unpatched','Unsecure') | Get-Random

这是最终结果看起来的样子:

ComputerName Owner          Phone        LastUpdate Status   
------------ -----          -----        ---------- ------   
Server-AK22  Amy Hill       968-954-5675 01/15/2007 Unsecure 
Server-MN43  Kelly Price    934-790-4090 11/14/1994 Secure   
Server-NE01  Shane Robinson 337-859-7009 03/25/1973 Unpatched
Server-MS06  Evan Parker    792-245-5228 07/31/2009 Secure   
Server-AZ26  Krystal Parker 643-391-6774 10/26/2014 Unsecure 

广告组名称

I had a demo that used Active Directory to generate a PSGraph based on group membership. For that, I generated several AD group names using the [synonym] template function.

    PS:> Invoke-Generate "[synonym opinion]_[synonym committee]" -Count 7

    message_commission
    subjectmatter_citizenscommittee
    idea_nongovernmentalorganization
    legaldocument_citizenscommittee
    substance_administrativebody
    subjectmatter_citizenscommittee
    legalinstrument_administrativeunit

这比使用随机角色更好。

最后的话

上述许多例子是从中取出的 项目的GitHub页面。我发现名称是非常有用的。我知道我的随机数据看起来好多了。