PasswordStateClass.ps1

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Script is converting to secure string.')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '', Justification = 'Script is converting to secure string.')]
# Create Class
Class EncryptedPassword {
    EncryptedPassword ($Password) {
        $result = [string]::IsNullOrEmpty($Password)
        if ($result -eq $false){
            $this.Password = ConvertTo-SecureString -String $Password -AsPlainText -Force
        }
        Else {
            $this.Password = $null
        }
    }
   [SecureString]$Password
}
class PasswordResult {
    # Properties
    [int]$PasswordID
    [String]$Title
    [String]$Username
    $Password
    [String]GetPassword() {
        $result = [string]::IsNullOrEmpty($this.Password.Password)
        If ($this.Password.GetType().Name -ne 'String' -and $result -eq $false) {
            $SecureString = $this.Password.Password
            $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
            return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        }
        if ($result -eq $true){
            return $null
        }
        Else {
            return $this.Password
        }
    }
    DecryptPassword(){
        $result = [string]::IsNullOrEmpty($this.Password.Password)
        If ($this.Password.GetType().Name -ne 'String' -and $result -eq $false) {
            $SecureString = $this.Password.Password
            $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
            $this.Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        }
    }
    [String]$Description
    [String]$Domain
    # Hidden Properties
    [String]$hostname
    [String]$GenericField1
    [String]$GenericField2
    [String]$GenericField3
    [String]$GenericField4
    [String]$GenericField5
    [String]$GenericField6
    [String]$GenericField7
    [String]$GenericField8
    [String]$GenericField9
    [String]$GenericField10
    [int]$AccountTypeID
    [string]$notes
    [string]$URL
    [string]$ExpiryDate
    [string]$allowExport
    [string]$accounttype
    # Constructor used to initiate the default property set.
    PasswordResult() {
        [string[]]$DefaultProperties = 'PasswordID', 'Title','Username','Password','Description','Domain'

        #Create a propertyset name DefaultDisplayPropertySet, with properties we care about
    $propertyset = New-Object System.Management.Automation.PSPropertySet DefaultDisplayPropertySet, $DefaultProperties
    $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$propertyset
    Add-Member -InputObject $this -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers
    }

}
class PasswordHistory : PasswordResult {
    $DateChanged
    [String]$USERID
    [String]$FirstName
    [String]$Surname
    [int32]$PasswordHistoryID
    [String]$PasswordList
    [String]GetPassword() {
        $result = [string]::IsNullOrEmpty($this.Password.Password)
        If ($this.Password.GetType().Name -ne 'String' -and $result -eq $false) {
            $SecureString = $this.Password.Password
            $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
            return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        }
        if ($result -eq $true){
            return $null
        }
        Else {
            return $this.Password
        }
    }
    DecryptPassword(){
        $result = [string]::IsNullOrEmpty($this.Password.Password)
        If ($this.Password.GetType().Name -ne 'String' -and $result -eq $false) {
            $SecureString = $this.Password.Password
            $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
            $this.Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        }
    }
}