Classes/TppObject.ps1
class TppObject { [string] $Path [string] $TypeName [guid] $Guid hidden _init ( [string] $Path, [string] $TypeName, [guid] $Guid ) { $this.Path = $Path.Replace('\\', '\') $this.TypeName = $TypeName $this.Guid = $Guid $this | Add-Member -MemberType ScriptProperty -Name Name -Value { Split-Path -Path $this.Path -Leaf } $this | Add-Member -MemberType ScriptProperty -Name ParentPath -Value { # split-path -parent doesn't work on this path so use this workaround $this.Path -replace ('\\+{0}' -f $this.Name), '' } $this | Add-Member -MemberType ScriptMethod -Name ToString -Value { $this.Path } -Force } TppObject ( [string] $Path, [string] $TypeName, [guid] $Guid ) { $this._init($Path, $TypeName, $Guid) } TppObject ([string] $Path) { $info = $Path | ConvertTo-TppGuid -IncludeType $this._init($Path, $info.TypeName, $info.Guid) } TppObject ([string] $Path, [PSObject] $VenafiSession) { $info = $Path | ConvertTo-TppGuid -IncludeType -VenafiSession $VenafiSession $this._init($Path, $info.TypeName, $info.Guid) } TppObject ([guid] $Guid) { $info = ConvertTo-TppPath -Guid $Guid -IncludeType $this._init($info.Path, $info.TypeName, $Guid) } TppObject ([guid] $Guid, [PSObject] $VenafiSession) { $info = ConvertTo-TppPath -Guid $Guid -IncludeType -VenafiSession $VenafiSession $this._init($info.Path, $info.TypeName, $Guid) } [HashTable] ToHashtable() { $hash = @{} $propNames = $this | Get-Member | Where-Object { $_.MemberType -eq 'Property' } | Select-Object -ExpandProperty Name foreach ($prop in $propNames) { if ($this.GetType().GetProperty($prop)) { $hash.Add($prop, $this.$prop) } } return $hash } TppObject ([Hashtable] $InitHash) { if ( -not ($InitHash.Path -and $InitHash.TypeName -and $InitHash.Guid) ) { throw "Name, TypeName, Path, and Guid are required" } $this._init($InitHash.Path, $InitHash.TypeName, $InitHash.Guid) } } |