当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 数据类型备忘单:String、Char、DateTime、Bool、Array、Hashtable、PSObject、Void、Math、TimeSpan

作者:精品下载站 日期:2024-12-14 07:31:33 浏览:14 分类:玩电脑

PowerShell 数据类型备忘单:String、Char、DateTime、Bool、Array、Hashtable、PSObject、Void、Math、TimeSpan


这篇博文主要是关于 PowerShell 数据类型的。什么是 PowerShell 数据类型?数据类型定义您可以使用 PowerShell 对象执行哪些操作。例如,日期时间对象支持添加年份,使用 bool 对象您可以判断某件事是真还是假。

目的不是单独解释每种数据类型,而只是提供数据类型作为备忘单,此外我将介绍 PowerShell 中使用的最常见的数据类型。我还将为每种数据类型提供一个示例,以激发有关使用数据类型的一些想法。

这是包含一些示例的列表。


### 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

将此代码复制到 PowerShell ISE (ise.exe) 会话中并发现这些值。例如,$o 显示阿诺德和我!

[玩转系统] PowerShell 数据类型备忘单:String、Char、DateTime、Bool、Array、Hashtable、PSObject、Void、Math、TimeSpan

下次使用 PowerShell 再见!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯