How to check VLF information in SQL Server (PowerShell)
Introduction
This script will list the actual log size, used log space, number of actual VLFs, number of used VLFs and other related information of all databases in an instance. Besides, the script will proactively list potential log size and the number of potential VLFs.
Scenarios
The script will list information about VLFs of all the databases in an instance.
Script
You can use this script in this way:
1. Run Microsoft PowerShell as Administrator
2. Run the script in the form: &Path
For example: & "D:\Syncup\OneScript\Julie\GetVLFsInformation.ps1"
3. Press "Enter" and enter the server name.
Note: Server name should be in the format: Server Name\ Instance Name. If it's a default instance, please enter the server name.
4. Press "Enter" and choose between Windows and SQL authentication. If you choose SQL Server authentication, please enter the correct user name and password.
5. When the script finishes running, we'll get the following figure:
Here are some code snippets for your reference.
## Windows Authentication
if($choice -eq 0)
{
#Create SqlConnection object and define connection string
$Connection = New-Object System.Data.SqlClient.SqlConnection
$ConnectionB = New-Object System.Data.SqlClient.SqlConnectionStringBuilder
$ConnectionB["Data Source"] = $ServerName
$ConnectionB["Database"] = $DatabaseName
$ConnectionB["Trusted_Connection"] = "SSPI"
$Connection.ConnectionString = $ConnectionB.ConnectionString
}
## Windows Authentication
if($choice -eq 0)
{
#Create SqlConnection object and define connection string
$Connection = New-Object System.Data.SqlClient.SqlConnection
$ConnectionB = New-Object System.Data.SqlClient.SqlConnectionStringBuilder
$ConnectionB["Data Source"] = $ServerName
$ConnectionB["Database"] = $DatabaseName
$ConnectionB["Trusted_Connection"] = "SSPI"
$Connection.ConnectionString = $ConnectionB.ConnectionString
}
SQL Server 2005/SQL Server 2008 R2 /SQL Server 2012
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.