Búsqueda de sitios web

Hoja de referencia sobre tipos de datos de PowerShell: cadena, carácter, fecha y hora, bool, matriz, tabla hash, PSObject, Void, Math, TimeSpan


Esta publicación de blog trata sobre los tipos de datos de PowerShell. ¿Qué es un tipo de datos de PowerShell? Los tipos de datos definen lo que puede hacer con un objeto de PowerShell. Por ejemplo, un objeto de fecha y hora admite la adición de años; con un objeto bool puedes determinar si algo es verdadero o falso.

El objetivo no es explicar cada tipo de datos individualmente, sino sólo proporcionar los tipos de datos como una hoja de trucos y, además, cubriré los tipos de datos más comunes utilizados en PowerShell. También le daré un ejemplo para cada tipo de datos para generar algunas ideas sobre cómo trabajar con tipos de datos.

Aquí está la lista con algunos ejemplos para jugar.


### String ###

$string='Patrick Gruenauer'

# Determine the length of the string ...

$string.Length

# Grab the first two letters ...

$first2=$string.Substring(0,2)

### Character ###

$char=[char]0x263a

### Byte ###

$byte=[byte]('0x'+'FE')

### DateTime ###

$birthday=[datetime]'03.23.1976'

# Calculate your time on earth ...

$timeonearth=(Get-Date) - $birthday

### Boolean ###

# Is it there or is it not there? Yes it is there! ==> True

$path=[bool](Get-ChildItem -Path C:\Windows -ErrorAction SilentlyContinue)

### Array ###

$array=[array]('Peter','Margit')

# Retrieve the first item in array

$array[0]

### Hashtable ###

$hash=[hashtable]@{Arnold = '1';Alex= '9'}

# Add an object to the hash table

$hash.Add('Markus','10')

### PSObject ###

# Declaring as an array ...

$o=@()

# Creating ordered hash tables ...

$obj=[ordered]@{
firstname = 'Patrick'
lastname = 'Gruenauer'
age = '43'
}

$obj2=[ordered]@{
firstname = 'Arnold'
lastname = 'Schwarzenburger'
age = '71'
}

# Adding hash tables to $o and creating an object

$o += New-Object -Type PSObject -Property $obj
$o += New-Object -Type PSObject -Property $obj2

### Void ###

# Send output to null

$none=[void]

### Math ###

# Round to 5 decimals ...

$math=[math]::Round('12.221349876',5)

### TimeSpan ###

# How many hours has a year?

[TimeSpan]::FromDays(365).Totalhours

Copie este código en su sesión de PowerShell ISE (ise.exe) y descubra los valores. Por ejemplo, ¡$o nos muestra a Arnold y a mí!

¡Hasta la próxima con PowerShell!

Artículos relacionados