Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a SQL Server database inventory


This recipe will explain the process of retrieving database properties and saving it to a file.

Getting ready

Log in to your SQL Server instance. Check which user databases are available for you to investigate. The same databases should appear in your resulting file after you run the PowerShell script.

How to do it...

To create a SQL Server Database Inventory, follow these steps:

  1. Open PowerShell ISE as administrator.

  2. Import the SQLPS module and create a new SMO Server object:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName

    Add the following script and run:

    #specify folder and filename to be produced
    $folder = "C:\Temp"
    $currdate = Get-Date -Format "yyyy-MM-dd_hmmtt"
    $filename = "$($instanceName)_db_$($currdate).csv"
    $fullpath = Join-Path $folder $filename
    
    $result = @()
    
    ...