PowerShell笔记 - 3.数组及哈希表

本系列是一个重新学习PowerShell的笔记,内容引用自PowerShell中文博客

数组

命令返回的数组

PS C:\> $arr = ls                                                                                                       PS C:\> $arr.GetType()                                                                                                  
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\> $arr[0].GetType()                                                                                               
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DirectoryInfo                            System.IO.FileSystemInfo

PS C:\> $arr.Length                                                                                                     29
PS C:\> $arr | Select-String "pdf"   
PS C:\Code> $arr -is [array]                                                                                            True
PS C:\Code> $arr | Select-Object -First 1 | Format-List *                                                               

PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\0605
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName       : 0605
PSDrive           : C
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : True
Mode              : d-----
BaseName          : 0605
Target            : {}
LinkType          :
Name              : 0605
FullName          : C:\0605
Parent            : Code
Exists            : True
Root              : C:\
Extension         :
CreationTime      : 2020/6/5 11:03:21
CreationTimeUtc   : 2020/6/5 3:03:21
LastAccessTime    : 2020/6/5 11:03:24
LastAccessTimeUtc : 2020/6/5 3:03:24
LastWriteTime     : 2020/6/5 11:03:24
LastWriteTimeUtc  : 2020/6/5 3:03:24
Attributes        : Directory

PS C:\Code> $arr | Where-Object {$_.Name -eq "0605"}                                                                    

    Directory: C:\Code

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/6/5     11:03                0605

创建数组

#基础数组
PS C:\Code> $arr = 1..3
PS C:\Code> $arr.GetType()                                                                                              
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\Code> $arr                                                                                                        1
2
3
PS C:\Code> $arr = 1,"花",(Get-Date)                                                                                    PS C:\Code> $arr                                                                                                        1
花

2021年9月10日 14:22:47

PS C:\Code> $arr.GetType()                                                                                              
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\Code> $arr.Count                                                                                                  3

#空数组
PS C:\Code> $arr = @()                                                                                                  PS C:\Code> $arr.GetType()                                                                                              
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

#一个元素的数组
PS C:\Code> $arr = ,"花"                                                                                                PS C:\Code> $arr.GetType()                                                                                              
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

PS C:\Code> $arr.Count                                                                                                  1

#查看其基类属性及方法
PS C:\Code> $arr.GetType().BaseType | Format-List *

操作数组

PS C:\Code> $arr = 1..5                                                                                                 PS C:\Code> $arr[($arr.Length -1)]                                                                                      5

#倒序
PS C:\Code> $arr[($arr.Length -1)..0]                                                                                   5
4
3
2
1

##顺序
PS C:\Code> $arr[0..($arr.Length -1)]                                                                                   1
2
3
4
5

#指定索引
PS C:\Code> $arr[0,2,4]                                                                                                 1
3
5

#添加
PS C:\Code> $arr+=6                                                                                                     PS C:\Code> $arr[5]                                                                                                     6

#删除
PS C:\Code> $arr = $arr[0..2] + $arr[5]                                                                                 PS C:\Code> $arr                                                                                                        1
2
3
6

#数组在创建后已指定数组的大小,只可重新写入新的数组,而不可对数组通过索引删除
PS C:\Code> $arr.Remove(1)                                                                                              Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $arr.Remove(1)
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

#因为数组是引用类型,在复制数组时应该使用Clone方法
PS C:\Code> $arr_copy = $arr.Clone()                                                                                    PS C:\Code> $arr_copy += 7                                                                                              PS C:\Code> $arr_copy.Count                                                                                             5
PS C:\Code> $arr.Count                                                                                                  4

#设置强类型数组
PS C:\Code> [int[]] $arr = @()                                                                                          PS C:\Code> $arr +=1                                                                                                    PS C:\Code> $arr += "2"                                                                                                 PS C:\Code> $arr += "a"                                                                                                 Cannot convert value "a" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ $arr += "a"
+ ~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

HashMap

哈希表操作

#创建
PS C:\Code> $hash = @{ Name = "花"; Age = 3}                                                                            PS C:\Code> $hash                                                                                                       
Name                           Value
----                           -----
Age                            3
Name                           花

#查看所有Key及Value
PS C:\Code> $hash.Keys                                                                                                  Age
Name
PS C:\Code> $hash.Values                                                                                                3
花

#添加或删除Key
PS C:\Code> $hash.Class = "干饭"                                                                                        PS C:\Code> $hash.Age = 4                                                                                               PS C:\Code> $hash                                                                                                       
Name                           Value
----                           -----
Name                           花
Age                            4
Class                          干饭                                                                                     

PS C:\Code> $hash.Remove("Class")                                                                                       PS C:\Code> $hash                                                                                                       
Name                           Value
----                           -----
Name                           花
Age                            4

#格式化输出
PS C:\Code> $hash | Format-Table                                                                                        
Name                           Value
----                           -----
Name                           花
Age
posted @ 2021-09-10 15:06  门前有根大呲花  阅读(204)  评论(0编辑  收藏  举报