AboutSQL Server, Analytics, .Net, Machine Learning, R, Python Archives
About Me
Mitch Wheat has been working as a professional programmer since 1984, graduating with a honours degree in Mathematics from Warwick University, UK in 1986. He moved to Perth in 1995, having worked in software houses in London and Rotterdam. He has worked in the areas of mining, electronics, research, defence, financial, GIS, telecommunications, engineering, and information management. Mitch has worked mainly with Microsoft technologies (since Windows version 3.0) but has also used UNIX. He holds the following Microsoft certifications: MCPD (Web and Windows) using C# and SQL Server MCITP (Admin and Developer). His preferred development environment is C#, .Net Framework and SQL Server. Mitch has worked as an independent consultant for the last 10 years, and is currently involved with helping teams improve their Software Development Life Cycle. His areas of special interest lie in performance tuning |
Sunday, February 27, 2011Powershell: Get SQL Server Default File Paths using SMOI recently needed to find the location of SQL Server’s default data file path in order to create multiple database data files as part of an automated production install. After looking at and discarding a few options that included reading the registry directly, SQL Server Management Objects (SMO) seemed a logical choice. Talking to one of my colleagues, Piers, whose Powershell wizardary has to be experienced to fully appreciate, we (well he!) fired up a Powershell GUI and we took a look at the methods available. As an aside, if you are not aware of this ‘trick’ it’s worth explicitly mentioning: In Powershell, first load the relevant assembly into memory (which in this instance is Microsoft.SqlServer.Smo): > [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") > $null Then, create an instance of the type you are interested in (the Server type): > $smoServer = new-object Microsoft.SqlServer.Management.Smo.Server “servername” and pipe the object instance default method output through Get-Member to list all the Events, Methods and Properties exposed: > $smoServer gm So having done that we found a property named DefaultFile which looked promising, but it just returned an empty string. After a bit of digging around, it transpires that it only returns a path if the current location is different to where the master DB is located, so here it is in Powershell: function Get-SQLServerDefaultDataFilePath Update: Piers pointed out that Books Online contains a useful section on programming tasks using SQL Server Management Objects (SMO) 1 Comments:
As an aside, there's some nice examples for using SMO - including via PowerShell - in Books Online these days: By piers7, at February 28, 2011 12:28 pm << Home |
ContactMSN, Email: mitch døt wheat at gmail.com LinksFavorites
Blogs |