Special method for evaluating Database Properties through conditional statements?

  • Passing an Instance name for $Instance, this loops through all databases on the instance. Gathers their name and Recovery model. Writes the name and recovery model. However, the conditional statement always evaluates false, despite the database being in Full recovery Mode. Is there some special way to evaluate database properties?

    $InstanceObject = New-Object (’Microsoft.SqlServer.Management.Smo.Server’) -argumentlist $InstanceName

    # loop through all databases within this instance

    $InstanceObject.Databases |

    foreach-object {

    # get database object into a variable

    $DatabaseObject = $_

    $DatabaseName = $DatabaseObject.Name

    $RecoveryModel = $DatabaseObject.RecoveryModel

    write-host $DatabaseName $RecoveryModel

    if($RecoveryModel -eq "Full"){write-host "$DatabaseName is in $RecoveryModel recovery mode"}

    }

  • Got it

    $RecoveryModel = "$($DatabaseObject.RecoveryModel)"

  • Glad you go it to work but as an alternative consider

    $InstanceObject = New-Object (’Microsoft.SqlServer.Management.Smo.Server’) -argumentlist $InstanceName

    $InstanceObject.Databases | Select Name, RecoveryModel | Foreach {

    Write-Host $_.Name $_.RecoveryModel

    if ($_.Recoverymodel.ToString() -eq 'Full') {

    Write-Host $_.Name 'is in' $_.RecoveryModel 'recovery mode'

    }

    }

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply