关于powershell自定义对象 PSCustomObject
2022-07-29 本文已影响0人
mudssky
01.创建一个PSCustomObject
这是powershell中创建一个对象比较简单的方法
$myObject = [PSCustomObject]@{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
然后你就可以像对象一样使用了,虽然哈希表本来也支持这样操作
$myObject.Name
也可以转化已经存在的哈希表
$myHashtable = @{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$myObject = [pscustomobject]$myHashtable
过时的方法
在早期版本的powershell中可以这样做,
主要是太麻烦了。
$myHashtable = @{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$myObject = New-Object -TypeName PSObject -Property $myHashtable
还有一种方法,就是把哈希表保存成json,然后在读入转化为对象
$myObject | ConvertTo-Json -depth 1 | Set-Content -Path $Path
$myObject = Get-Content -Path $Path | ConvertFrom-Json
02.属性操作
001.添加属性
$myObject | Add-Member -MemberType NoteProperty -Name 'ID' -Value 'KevinMarquette'
$myObject.ID
002.移除属性
$myObject.psobject.properties.remove('ID')
003.遍历属性
$myObject | Get-Member -MemberType NoteProperty | Select -ExpandProperty Name
我们可以从 properties属性中得到同样的列表
$myobject.psobject.properties.name
004.动态访问属性
$myObject.Name
也可以用字符串
$myObject.'Name'
还可以用变量,不过这个语法我还是觉得太别扭
$property = 'Name'
$myObject.$property
005.转换PSCustomObject为哈希表
运用上面获得属性列表的方法进行遍历。
$hashtable = @{}
foreach( $property in $myobject.psobject.properties.name )
{
$hashtable[$property] = $myObject.$property
}
006.测试属性存在
if( $null -ne $myObject.ID )
上面的方法有一个问题就是有可能值就是$null
if( $myobject.psobject.properties.match('ID').Count )
03.添加对象方法
我们可以用Add-Member,和哈希表打包参数的形式,添加函数方法
其中函数定义在脚本块里。
$ScriptBlock = {
$hashtable = @{}
foreach( $property in $this.psobject.properties.name )
{
$hashtable[$property] = $this.$property
}
return $hashtable
}
$memberParam = @{
MemberType = "ScriptMethod"
InputObject = $myobject
Name = "ToHashtable"
Value = $scriptBlock
}
Add-Member @memberParam
然后我们就可以调用了,总感觉就很麻烦,比起js直接就能写函数来说。
$myObject.ToHashtable()
对象和值类型
$first = 1
$second = $first
$second = 2
$third = [PSCustomObject]@{Key=3}
$fourth = $third
$fourth.Key = 4
对象的copy方法
属于是浅拷贝
$third = [PSCustomObject]@{Key=3}
$fourth = $third.psobject.copy()
$fourth.Key = 4
PSTypeName
常见的方式
$myObject.PSObject.TypeNames.Insert(0,"My.Object")
还有另一种方法
$myObject = [PSCustomObject]@{
PSTypeName = 'My.Object'
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
04. 默认属性集 DefaultPropertySet
powershell中有一个配置决定默认显示那些属性。
$defaultDisplaySet = 'Name','Language'
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$MyObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers
Update-TypeData
这个方法更容易使用
$TypeData = @{
TypeName = 'My.Object'
DefaultDisplayPropertySet = 'Name','Language'
}
Update-TypeData @TypeData
$myObject | Format-List *
ScriptProperty
$TypeData = @{
TypeName = 'My.Object'
MemberType = 'ScriptProperty'
MemberName = 'UpperCaseName'
Value = {$this.Name.toUpper()}
}
Update-TypeData @TypeData
05.作为函数参数
param( [PSTypeName('My.Object')]$Data )
作为函数输出
function Get-MyObject
{
[OutputType('My.Object')]
[CmdletBinding()]
param
(
...