I know I can use the following t-SQL to shrink a log file:
BACKUP LOG db1 TO DISK = '\\server\share\db1_log1.trn'; DBCC ShrinkFile([db1_log], 0);
How do I do the same with SMO? I tried:
$server = new-Object Microsoft.SqlServer.Management.Smo.Server() $dbBackup = new-object Microsoft.SqlServer.Management.Smo.Backup $dbBackup.Action = "Log" $dbBackup.Database = "db1" $dbBackup.Devices.AddDevice("\\server\share\db1_log1.trn", "File") $dbBackup.SqlBackup($server) $db = $srv.Databases.Item("db1") $db.TruncateLog()
But here, I found that the TruncateLog() method does not work with anything higher than SQL 2005. That link recommended using:
$db.RecoveryModel = RecoveryModel.Simple $db.Alter() $db.LogFiles.Shrink(0, ShrinkMethod.TruncateOnly)
However, switching to Simple Recovery Mode is going to break my log chain and I don't want to do that. Shrinking the log file doesn't break my log chain so I'm looking for an equivalent SMO method to do that. Does such a method exist?