脚本
Update-TypeData -TypeName 'System.DateTime' -MemberName '时辰' -MemberType 'ScriptProperty' -Value { $timeAliasArray='子丑寅卯辰巳午未申酉戌亥' $hour = $this.Hour [int]$index=0 if($hour -eq 22){ $index=11 } else{ $index=[math]::Floor( ( $hour+1 ) % 23 / 2 ) } return $timeAliasArray[ $index ] + "时" }
演示
上面的命令执行后,DateTime就多了一个属性,我们来验证下:
PS> (get-date).时辰 子时 PS> $t=Get-Date -Hour 17 PS> $t.时辰 酉时
总结
Update-TypeData与Add-Member相比,前者扩展的类型,后者扩展的实例。
如何永久扩展类型,可以放在Profile文件中,也可以直接修改ETS文件。
您可能感兴趣的文章:
- Shell时间(date)相关命令
- linux shell实现随机数几种方法分享(date,random,uuid)
- PowerShell中使用正则和ValidateSet验证参数合法性
- PowerShell中iso8601格式日期和DateTime对象互转实例
- Shell脚本实现随机数多种方法介绍(date、random、uuid)
- PowerShell中使用Get-Date获取日期时间并格式化输出的例子
- PowerShell中Get-Date对象的常用属性和方法介绍
- shell语言date的用法实例
本文由 华域联盟 原创撰写:华域联盟 » Powershell小技巧之使用Update-TypeData扩展类型系统
转载请保留出处和原文链接:https://www.cnhackhy.com/14482.htm