get database properties using power shell in sql server 2008 techrepublic

4
Get database properties using PowerShell in SQL Server 2008 | TechRepublic http://www.techrepublic.com/blog/datacenter/get-database-properties-using-powershell-in-sql-server-2008/2814[08/29/2012 3:56:08 PM] Blogs Downloads Newsletters Galleries Q&A Discussions News Research Library Home / Blogs / The Enterprise Cloud The Enterprise Cloud Get database properties using PowerShell in SQL Server 2008 By Tim Chapman July 7, 2010, 8:54 AM PDT Takeaway: Tim Chapman shows how you can use PowerShell scripts in SQL Server 2008 to take an inventory of database properties for SQL Server instances on your network. Windows PowerShell functionality is embedded in SQL Server 2008. PowerShell can be invoked from SQL Server Management Studio so that you can easily take advantage of its SQL Server functionality. PowerShell is great to use on SQL Server instances, but its real power is harnessed when you use it to administer all servers on your network. For this tutorial, I’ll write a PowerShell script that loops through a list of SQL Server instances that I pull from a text file; for each database on that instance, I will run a SQL Script to output the properties for the given database. I’ll also look at how to invoke SQL Server Management Objects in the example and demonstrate how easy the Invoke-SQL cmdlet is to use in PowerShell. Note: If you’re on a computer that does not have PowerShell installed, you can download the PowerShell environment . The server list PowerShell makes reading data from a text file and looping through its contents very easy. For our server list, we’ll create a new text file in Notepad (or your text editor of choice) and write SQL Server instances in the list. For my example, I will include two database instances: Wilma and Wilma\R2Eval (Figure A). Figure A Save this text file to your C:\ drive. We’ll call it in a few minutes. Follow this blog: IT Management Development IT Support Data Center Networks Security Log In Join TechRepublic FAQ Go Pro! ZDNet Asia SmartPlanet TechRepublic

Upload: kaing-menglieng

Post on 08-Apr-2017

2.238 views

Category:

Documents


3 download

TRANSCRIPT

  • Get database properties using PowerShell in SQL Server 2008 | TechRepublic

    http://www.techrepublic.com/blog/datacenter/get-database-properties-using-powershell-in-sql-server-2008/2814[08/29/2012 3:56:08 PM]

    Blogs Downloads Newsletters Galleries Q&A Discussions News

    Research Library

    Home / Blogs / The Enterprise Cloud

    The Enterprise Cloud

    Get database properties usingPowerShell in SQL Server2008By Tim ChapmanJuly 7, 2010, 8:54 AM PDT

    Takeaway: Tim Chapman shows how you can use PowerShell scripts in SQL Server 2008 totake an inventory of database properties for SQL Server instances on your network.

    Windows PowerShell functionality is embedded in SQL Server 2008. PowerShell can be invokedfrom SQL Server Management Studio so that you can easily take advantage of its SQL Serverfunctionality.

    PowerShell is great to use on SQL Server instances, but its real power is harnessed when youuse it to administer all servers on your network. For this tutorial, Ill write a PowerShell script thatloops through a list of SQL Server instances that I pull from a text file; for each database on thatinstance, I will run a SQL Script to output the properties for the given database. Ill also look athow to invoke SQL Server Management Objects in the example and demonstrate how easy theInvoke-SQL cmdlet is to use in PowerShell.

    Note: If youre on a computer that does not have PowerShell installed, you can download thePowerShell environment.

    The server listPowerShell makes reading data from a text file and looping through its contents very easy. For ourserver list, well create a new text file in Notepad (or your text editor of choice) and write SQLServer instances in the list. For my example, I will include two database instances: Wilma andWilma\R2Eval (Figure A).

    Figure A

    Save this text file to your C:\ drive. Well call it in a few minutes.

    Follow this blog:

    IT Management Development IT Support Data Center Networks Security

    Log In Join TechRepublic FAQ Go Pro!ZDNet Asia SmartPlanet TechRepublic

    http://ad.doubleclick.net/click;h=v8/3ce0/0/0/%2a/d;44306;0-0;0;74341537;31-1/1;0/0/0;;~sscs=%3fhttp://www.techrepublic.com/http://www.techrepublic.com/http://www.techrepublic.com/blogshttp://www.techrepublic.com/downloadshttp://www.techrepublic.com/newslettershttp://www.techrepublic.com/photoshttp://www.techrepublic.com/forum/questionshttp://www.techrepublic.com/forum/discussionshttp://www.techrepublic.com/newshttp://www.techrepublic.com/research-libraryhttp://www.techrepublic.com/http://www.techrepublic.com/blogshttp://www.techrepublic.com/blog/datacenterhttp://ad.doubleclick.net/click;h=v8/3ce0/0/0/%2a/j;44306;0-0;0;74341537;3823-300/100;0/0/0;;~sscs=%3fhttp://www.techrepublic.com/search?q=tim+chapmanhttp://technet.microsoft.com/en-us/scriptcenter/dd772285.aspxhttp://www.microsoft.com/downloads/details.aspx?FamilyId=60cb5b6c-6532-45e0-ab0f-a94ae9ababf5&displaylang=enhttp://www.microsoft.com/downloads/details.aspx?FamilyId=60cb5b6c-6532-45e0-ab0f-a94ae9ababf5&displaylang=enhttp://i.techrepublic.com.com/blogs/get-database-properties-using-powershell_figa.jpghttp://www.techrepublic.com/blog/datacenter?mode=rsshttp://www.techrepublic.com/alerts/add?url=http%3A%2F%2Fwww.techrepublic.com%2Fblog%2Fdatacenter%3Fmode%3Drss&title=The+Enterprise+Cloud+on+TechRepublic&source=http%3A%2F%2Fwww.techrepublic.com%2Fblog%2Fdatacenter%2Fget-database-properties-using-powershell-in-sql-server-2008%2F2814&frequency=weekly&rss_type=38http://www.techrepublic.com/members/login?regSrc=global-reghttp://www.techrepublic.com/members/join?regSrc=global-reghttp://www.techrepublic.com/faq/generalhttp://www.techrepublic.com/prohttp://www.zdnetasia.com/http://www.smartplanet.com/http://www.techrepublic.com/

  • Get database properties using PowerShell in SQL Server 2008 | TechRepublic

    http://www.techrepublic.com/blog/datacenter/get-database-properties-using-powershell-in-sql-server-2008/2814[08/29/2012 3:56:08 PM]

    The SQL fileNow I will write a SQL script that will call the function DATABASEPROPERTY and several of itsproperties; this script will be called by our PowerShell script. The contents of this file are below:

    DECLARE @db SYSNAMESET @db = DB_NAME()SELECT @db AS DatabaseName, IsAnsiNullDefault AS DBProperty,DATABASEPROPERTY(@db, IsAnsiNullDefault) AS ValueUNION ALLSELECT @db AS DatabaseName, IsAnsiNullsEnabled, DATABASEPROPERTY(@db,IsAnsiNullsEnabled)UNION ALLSELECT @db AS DatabaseName, IsAnsiWarningsEnabled, DATABASEPROPERTY(@db,IsAnsiWarningsEnabled)UNION ALLSELECT @db AS DatabaseName, IsAutoClose, DATABASEPROPERTY(@db, IsAutoClose)UNION ALLSELECT @db AS DatabaseName, IsAutoCreateStatistics, DATABASEPROPERTY(@db,IsAutoCreateStatistics)UNION ALLSELECT @db AS DatabaseName, IsAutoShrink, DATABASEPROPERTY(@db, IsAutoShrink)UNION ALLSELECT @db AS DatabaseName, IsAutoUpdateStatistics, DATABASEPROPERTY(@db,IsAutoUpdateStatistics)UNION ALLSELECT @db AS DatabaseName, IsBulkCopy, DATABASEPROPERTY(@db, IsBulkCopy)UNION ALLSELECT @db AS DatabaseName, IsCloseCursorsOnCommitEnabled,DATABASEPROPERTY(@db, IsCloseCursorsOnCommitEnabled)UNION ALLSELECT @db AS DatabaseName, IsDboOnly, DATABASEPROPERTY(@db, IsDboOnly)UNION ALLSELECT @db AS DatabaseName, IsDetached, DATABASEPROPERTY(@db, IsDetached)UNION ALLSELECT @db AS DatabaseName, IsEmergencyMode, DATABASEPROPERTY(@db,IsEmergencyMode)UNION ALLSELECT @db AS DatabaseName, IsFulltextEnabled, DATABASEPROPERTY(@db,IsFulltextEnabled)UNION ALLSELECT @db AS DatabaseName, IsInLoad, DATABASEPROPERTY(@db, IsInLoad)UNION ALLSELECT @db AS DatabaseName, IsInRecovery, DATABASEPROPERTY(@db, IsInRecovery)UNION ALLSELECT @db AS DatabaseName, IsInStandBy, DATABASEPROPERTY(@db, IsInStandBy)UNION ALLSELECT @db AS DatabaseName, IsLocalCursorsDefault, DATABASEPROPERTY(@db,IsLocalCursorsDefault)UNION ALLSELECT @db AS DatabaseName, IsNotRecovered, DATABASEPROPERTY(@db,IsNotRecovered)UNION ALLSELECT @db AS DatabaseName, IsNullConcat, DATABASEPROPERTY(@db, IsNullConcat)UNION ALLSELECT @db AS DatabaseName, IsOffline, DATABASEPROPERTY(@db, IsOffline)UNION ALLSELECT @db AS DatabaseName, IsParameterizationForced, DATABASEPROPERTY(@db,IsParameterizationForced)UNION ALLSELECT @db AS DatabaseName, IsQuotedIdentifiersEnabled, DATABASEPROPERTY(@db,IsQuotedIdentifiersEnabled)UNION ALLSELECT @db AS DatabaseName, IsReadOnly, DATABASEPROPERTY(@db, IsReadOnly)

  • Get database properties using PowerShell in SQL Server 2008 | TechRepublic

    http://www.techrepublic.com/blog/datacenter/get-database-properties-using-powershell-in-sql-server-2008/2814[08/29/2012 3:56:08 PM]

    UNION ALLSELECT @db AS DatabaseName, IsRecursiveTriggersEnabled, DATABASEPROPERTY(@db,IsRecursiveTriggersEnabled)UNION ALLSELECT @db AS DatabaseName, IsShutDown, DATABASEPROPERTY(@db, IsShutDown)UNION ALLSELECT @db AS DatabaseName, IsSingleUser, DATABASEPROPERTY(@db, IsSingleUser)UNION ALLSELECT @db AS DatabaseName, IsSuspect, DATABASEPROPERTY(@db, IsSuspect)UNION ALLSELECT @db AS DatabaseName, IsTruncLog, DATABASEPROPERTY(@db, IsTruncLog)UNION ALLSELECT @db AS DatabaseName, Version, DATABASEPROPERTY(@db, Version)

    Copy the script above into Notepad and save the script as DatabasePropertiesSQLScript.txt onyour C:\ drive.

    PowerShell scriptNow lets define our PowerShell script. In this script, Ill need to invoke a SQL Server ManagementObjects (SMO) object so that I am able to loop through the databases on our SQL Serverinstances. In the first line of the script, I load the SMO assembly so I can make use of its objects.

    In the second line, I load the contents of our InstanceList.txt file into an object named $servers.Once Ive loaded the object with the server list, I am able to loop through these servers using aforeach loop.

    In the next line, I am creating a new SMO server object and assigning it to the $sqlserver object.Notice that I am passing the name of the server to this function call; I am doing this so I can tellSMO which instance name I want my $sqlserver object to represent. Once the SMO server objecthas been defined, it exposes a Databases object, which is an enumeration of the databasespresent on the SQL Server instance. I can take advantage of the Databases property to loopthrough each of these databases on the instance.

    As I loop through each database, I call a SQL Server specific PowerShell cmdlet named invoke-sqlcmd. This command allows you to run a SQL Server command against a specific SQL Serverinstance and database; it also allows you to run a SQL Script from a file. This is where the SQLscript that contains our DATABASEPROPERTY calls comes into play. I am now able todynamically pass the server and database names into this command and call the SQL script foreach database that we loop through.

    [System.Reflection.Assembly]::LoadWithPartialName(Microsoft.SqlServer.SMO) | Out-Null

    $servers = get-content c:\InstanceList.txt

    foreach($server in $servers){$sqlserver = new-object Microsoft.SqlServer.Management.Smo.Server $serverforeach ($db in $sqlserver.Databases){invoke-sqlcmd -inputfile c:\DatabasePropertiesSQLScript.txt -database $db.name -ServerInstance$sqlserver.name -IgnoreProviderContext}}

    Copy the above script and paste into a text editor. Save this file to your C:\ drive under the namePSLooperScript.ps1.

    Now that all of our scripts are defined, we need to run them. To run the scripts, follow these steps:

    1. Open SQL Server Management Studio 2008.

    2. Right-click a server instance and select Start PowerShell. This opens a command-typewindow where you can enter PowerShell commands. Since weve defined our PowerShellscripts in a file, all we need to do is call the file from the interface.

    3. At the PowerShell prompt, type the location of the PowerShell script you just saved and hit

  • Get database properties using PowerShell in SQL Server 2008 | TechRepublic

    http://www.techrepublic.com/blog/datacenter/get-database-properties-using-powershell-in-sql-server-2008/2814[08/29/2012 3:56:08 PM]

    Join Login

    Get IT Tips, news, and reviews delivered directly to your inbox by subscribing to TechRepublics freenewsletters.

    About Tim Chapman

    Join the TechRepublic Community and join the conversation! Signing-up isfree and quick, Do it now, we want to hear your opinion.

    [Enter].

    C:\PSLooperScript.ps1

    If everything works properly, youll see a long list of databases and their properties in yourPowerShell window.

    SummaryIn this tutorial, we used PowerShell and SMO to loop through a list of SQL Server instances from atext file. We then looped through each database on the SQL Server instance and ran a SQLServer script that output the properties for the given database.

    This simple example shows how powerful it can be to use PowerShell with SQL Server. This scriptcould easily be built upon to write scripts for administering your SQL Servers, gathering instance-related information, and handling security or policies on your SQL Server 2008 instances.

    TechRepublics Servers and Storage newsletter, delivered on Monday and Wednesday, offers tipsthat will help you manage and optimize your data center. Automatically sign up today!

    Full Bio Contact

    Component upgrade vs.replacement: What wouldyou do?

    RAID 50 offers a balance ofperformance, storagecapacity, and data integrity

    http://www.techrepublic.com/members/join?regSrc=disc-starthttp://www.techrepublic.com/members/join?regSrc=disc-starthttp://www.techrepublic.com/members/login?regSrc=disc-starthttp://www.techrepublic.com/members/login?regSrc=disc-starthttp://www.techrepublic.com/newslettershttp://www.techrepublic.com/newslettershttp://nl.com.com/MiniFormHandler?brand=techrepublic&list_id=e040http://www.techrepublic.com/blog/datacenter/component-upgrade-vs-replacement-what-would-you-do/2798http://www.techrepublic.com/blog/datacenter/component-upgrade-vs-replacement-what-would-you-do/2798http://www.techrepublic.com/blog/datacenter/component-upgrade-vs-replacement-what-would-you-do/2798http://www.techrepublic.com/blog/datacenter/component-upgrade-vs-replacement-what-would-you-do/2798http://www.techrepublic.com/blog/datacenter/raid-50-offers-a-balance-of-performance-storage-capacity-and-data-integrity/2822http://www.techrepublic.com/blog/datacenter/raid-50-offers-a-balance-of-performance-storage-capacity-and-data-integrity/2822http://www.techrepublic.com/blog/datacenter/raid-50-offers-a-balance-of-performance-storage-capacity-and-data-integrity/2822http://www.techrepublic.com/blog/datacenter/raid-50-offers-a-balance-of-performance-storage-capacity-and-data-integrity/2822

    techrepublic.comGet database properties using PowerShell in SQL Server 2008 | TechRepublic

    NxbC1zZXJ2ZXItMjAwOC8yODE0AA==: form1: q: button3: