Búsqueda de sitios web

Diversión del viernes: hace mucho tiempo en un universo PowerShell muy, muy lejano


Con el próximo lanzamiento de la próxima película de Star Wars, pensé en revisar mi script de PowerShell que genera el nombre del universo de Star Wars. Claro, es artificial y completamente impráctico, pero apuesto a que de todos modos sientes curiosidad. Mi versión anterior era un script simple con valores codificados, lo que significaba que tenías que modificar el archivo. Difícilmente amigable con los droides. Lo corregí e hice algunos otros ajustes menores.

Puede encontrar el script en su esencia en GitHub.

New-StarWarsName.ps1:

#generate a fictious name that might exist in the Star Wars universe.

[cmdletbinding()]
Param(
    [Parameter(Mandatory,HelpMessage="Enter your first name. It must be at least 2 characters")]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({$_.length -ge 2})]
    [String]$Name,
    [Parameter(Mandatory,HelpMessage="Enter your Mother's first name. It must be at least 2 characters")]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({$_.length -ge 2})]
    [String]$Mother,
    [Parameter(Mandatory,HelpMessage="Enter your Father's first name. It must be at least 2 characters")]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({$_.length -ge 2})]
    [String]$Father,
    [Parameter(Mandatory,HelpMessage="What state or city were you born in? It must be at least 4 characters and remove spaces.")]
    [ValidateScript({$_.length -ge 4})]
    [string]$State
)

#get a random console color name
$color = [enum]::GetNames([consolecolor]) | Get-Random

#use the -F operator to assemble first and last names using the Substring method.
$first = "{0}{1}{2}{3}" -f $name.substring(0,1).toUpper(),$name.substring(1,1),$mother.Substring(0,2),$father.Substring($father.Length -2,2)
$last = "{0}{1}" -f $color.Substring(0,3),$state.Substring($state.length -4,4)

#join the first and last name together
$yourname = $first,$last -join " "

#display the results.
Write-Host "Greetings, $yourname. At last we meet again." -ForegroundColor Cyan
<#
Copyright (c) 2016-2017 JDH Information Technology Solutions, Inc.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#>

Guarde una copia del script localmente, puede llamarlo como quiera. El script necesita su nombre (o cualquier nombre), los nombres de sus padres y el estado o ciudad en el que nació. Esta versión del script ha parametrizado estos requisitos y tiene validación de parámetros. Este es el tipo de diversión que puedes tener.

Disfruta y que la Fuerza te acompañe.

Artículos relacionados