Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Special method for evaluating Database Properties through conditional statements? Expand / Collapse
Author
Message
Posted Friday, August 9, 2013 11:24 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, September 22, 2014 8:57 AM
Points: 50, Visits: 460
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"}

}
Post #1482868
Posted Friday, August 9, 2013 11:42 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, September 22, 2014 8:57 AM
Points: 50, Visits: 460
Got it

$RecoveryModel = "$($DatabaseObject.RecoveryModel)"
Post #1482872
Posted Friday, August 9, 2013 2:34 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 8:05 AM
Points: 60, Visits: 893
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'
}
}

Post #1482934
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse