SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Special method for evaluating Database Properties through conditional statements?


Special method for evaluating Database Properties through conditional statements?

Author
Message
Jeff Shurak
Jeff Shurak
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 763
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"}

}

Jeff Shurak
Jeff Shurak
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 763
Got it

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

bruce 1565
bruce 1565
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 1022
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'
}
}


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search