0% found this document useful (0 votes)
133 views1 page

Powershell Script To Truncate SQL Log Files For All User Databases

This PowerShell script truncates the SQL log files for user databases on a SQL server. It uses the Invoke-SQLCMD cmdlet to query the sysdatabases and sys.master_files tables to get the database and log file names. For each user database, it sets the database to simple recovery, shrinks the log file, then sets the database back to full recovery. Logging messages are written to a log file with the server name and date in the filename.

Uploaded by

opedro9850
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views1 page

Powershell Script To Truncate SQL Log Files For All User Databases

This PowerShell script truncates the SQL log files for user databases on a SQL server. It uses the Invoke-SQLCMD cmdlet to query the sysdatabases and sys.master_files tables to get the database and log file names. For each user database, it sets the database to simple recovery, shrinks the log file, then sets the database back to full recovery. Logging messages are written to a log file with the server name and date in the filename.

Uploaded by

opedro9850
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

# Script to truncate SQL log file for all user DBs on current SQL server

# Sam Boutros - 6/5/2014 - V1.0


# 7/28/14 - V1.1 - Cosmetic re-write..
# Truncate-Log.ps1
#
function Log {
[CmdletBinding()]
param(
[Parameter (Mandatory=$true,Position=1,HelpMessage="String to be saved to
log file and displayed to screen: ")][String]$String,
[Parameter (Mandatory=$false,Position=2)][String]$Color = "White",
[Parameter (Mandatory=$false,Position=3)][String]$Logfile =
$myinvocation.mycommand.Name.Split(".")[0] + "_" + (Get-Date -format
yyyyMMdd_hhmmsstt) + ".txt"
)
write-host $String -foregroundcolor $Color
((Get-Date -format "yyyy.MM.dd hh:mm:ss tt") + ": " + $String) | out-file
-Filepath $Logfile -append
}
#
# Import-Module SQLPS # See notes..
$Logfile = (Get-Location).path + "\Truncate_" + $env:COMPUTERNAME + (Get-Date
-format yyyyMMdd_hhmmsstt) + ".txt"
# skipping first 4 databases: master, tempdb, model, msdb
(Invoke-SQLCMD -Query "SELECT * FROM sysdatabases WHERE dbid > 4") | ForEach-Object
{
$SQLLogString = "N'" + (Invoke-SQLCMD -Query ("SELECT name FROM
sys.master_files WHERE database_id = " + $_.dbid + " AND type = 1;")).name + "'"
Set-Location -Path ($Logfile.Split(":")[0] + ":")
log ("Truncating log file $SQLLogString for database " + $_.name + "
(database_id = " + $_.dbid + ")") Cyan $Logfile
Invoke-SQLCMD -Query ("USE [" + $_.name + "]; ALTER DATABASE [" + $_.name + "]
SET RECOVERY SIMPLE WITH NO_WAIT;")
Invoke-SQLCMD -Query ("USE [" + $_.name + "]; DBCC SHRINKFILE($SQLLogString,
1); ALTER DATABASE [" + $_.name + "] SET RECOVERY FULL WITH NO_WAIT")
}

You might also like