Powershell add database to HA

A Powershell class that contains functions that can be used to add a database into an Availability Group for HA.

class AddToHAEntity
{
    [string]$TempPath
    [string]$DBName
    [string]$SQL01
    [string]$SQL02
    [string]$PrimaryAG
    [void] Cleanup(){   
        $this.DeleteFile($this.GetTempBak())
        $this.DeleteFile($this.GetTempTrn())
    }    
    [void] BackupDatabase([string]$sql){    
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
        $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $sql
        $db = $srv.Databases.Item($this.DBName)
        $db.RecoveryModel = 'Full'
        $db.Alter()
     
        # Create .bak file
        $this.GetTempBak()    
        Backup-SqlDatabase -Database $this.DBName `
        -BackupFile $this.GetTempBak() `
        -ServerInstance $sql `
        -Initialize
        
        # Create .trn file
        $this.GetTempTrn()
        Backup-SqlDatabase -Database $this.DBName `
        -BackupFile $this.GetTempTrn() `
        -ServerInstance $sql `
        -BackupAction 'Log' `
        -Initialize
    
    }
    [void] RestoreBackup([string]$sql){
        Restore-SqlDatabase -Database $this.DBName `
        -BackupFile $this.GetTempBak() `
        -ServerInstance $sql `
        -NoRecovery

        Restore-SqlDatabase -Database $this.DBName `
        -BackupFile $this.GetTempTrn() `
        -ServerInstance $sql `
        -RestoreAction 'Log' `
        -NoRecovery
    }
    [void] AddDbToAGGroup([string]$sql){
        Add-SqlAvailabilityDatabase -Database $this.DBName -Path "SQLSERVER:\SQL\$sql\DEFAULT\AvailabilityGroups\$($this.PrimaryAG)"
    }
    [void] DeleteFile([string]$filename){   
        if ([System.IO.File]::Exists($filename))
        {
            [System.IO.File]::Delete($filename)
        }
    }
    [void] DeleteFromAG([string]$sql,[string]$ag,[string]$dbname){
        Import-Module SQLPS -DisableNameChecking
        Remove-SqlAvailabilityDatabase -Path "SQLSERVER:\SQL\$sql\DEFAULT\AvailabilityGroups\$ag\AvailabilityDatabases\$dbname"
    }
    [void] DeleteDB([string]$sql,[string]$dbname){
        Import-Module SQLPS -DisableNameChecking
        
        Invoke-Sqlcmd -ServerInstance $sql  -Query "USE [master] IF ((SELECT state_desc FROM sys.databases WHERE [name]='$dbname') = 'RESTORING')
        BEGIN DROP DATABASE [$dbname]; END
        ELSE ALTER DATABASE [$dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;"
        
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null        
        $smoServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $sql
        if($smoServer.Databases.Contains($dbname))
        {
            $smoServer.KillAllProcesses($dbname)
            Invoke-Sqlcmd -Query "DROP DATABASE $dbname;"
        }
    }
    [hashtable] GetDatabases([string]$sql){
        $props = @{}
        Get-ChildItem "SQLSERVER:\SQL\$sql\Default\Databases" | ForEach-Object {
            $props.Add($_.Name, $_.Status)
        }
        return $props
    }
    [hashtable] GetAGGroups([string]$sql){
        $props = @{}
        Get-ChildItem "SQLSERVER:\SQL\$sql\Default\AvailabilityGroups" | ForEach-Object {
            $props.Add($_.Name, $_.PrimaryReplicaServerName)
        }
        return $props
    }
    [hashtable] GetAGListener([string]$sql,[string]$ag){
        $props = @{}     
        Get-ChildItem "SQLSERVER:\SQL\$sql\DEFAULT\AvailabilityGroups\$ag\AvailabilityGroupListeners" | ForEach-Object {
            $props.Add($ag, $_.DisplayName)
        }
        return $props
    }
    hidden [string] GetTempBak(){
        return "$($this.TempPath)$($this.DBName)_TempBK.bak"
    }
    hidden [string] GetTempTrn(){
        return "$($this.TempPath)$($this.DBName)_TempBK.trn"
    }
}
#[AddToHAEntity]$data = New-Object AddToHAEntity
#$data.TempPath = "\\Example\Backups\"
#$data.DBName = "database_example"
#$data.SQL01 = "sql_server_01"
#$data.SQL02 = "sql_server_02"
#$data.PrimaryAG = "ag_group_01"
#$data.DeleteFromAG($data.SQL01,$data.PrimaryAG,$data.DBName)
#$data.DeleteDB($data.SQL02,$data.DBName)
#$data.BackupDatabase($data.SQL01)
#$data.RestoreBackup($data.SQL02)
#$data.AddDbToAGGroup($data.SQL01)
#$data.AddDbToAGGroup($data.SQL02)
#$data.Cleanup()