您现在的位置是:网站首页> 编程资料编程资料
Powershell小技巧之使用Update-TypeData扩展类型系统_PowerShell_
2023-05-26
571人已围观
简介 Powershell小技巧之使用Update-TypeData扩展类型系统_PowerShell_
脚本
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文件。
您可能感兴趣的文章:
相关内容
- Powershell小技巧之找出脚本中的错误_PowerShell_
- Windows Powershell对象转换成文本_PowerShell_
- Windows Powershell使用管道_PowerShell_
- Windows Powershell使用哈希表_PowerShell_
- Windows Powershell强类型数组_PowerShell_
- Powershell小技巧之获取当前的时间并转换为时辰_PowerShell_
- Powershell小技巧之判断是否包涵大小写_PowerShell_
- PowerShell小技巧之调用CloudFlare的SDK查询网站统计信息_PowerShell_
- PowerShell小技巧实现IE Web自动化_PowerShell_
- Windows Azure VM上配置FTP服务器_PowerShell_
