Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 5
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 1
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 2
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 4
The MAK
Part I and Part II of this series showed you how to install PowerShell and simple SMO and WMI cmdlets. In Part III, I showed you how to script PowerShell and connect to SQL Server. In Part IV, I will show you how to use the PowerShell script to loop through the contents of the file and connect to other servers.
In this article, I will continue the discussion by showing you how to use PowerShell and PowerShell scripts in database creation and other issues.
Method 1
We acknowledge that we will create a database 'MyDatabase', on the 'HOME' server with default features.
Execute the following command:
[System.Reflection.Assembly] :: LoadWithPartialName ("Microsoft.SqlServer.SMO")
$ Server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'HOME'
$ DataBase = new-object ('Microsoft.SqlServer.Management.Smo.Database')
($ Server, "MyDataBase")
$ DataBaseDataFile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile')
($ FileGrowth, "MyDatabase_Data")
$ DataBaseDataFile.FileName = "D: MyDatabase_Data.mdf"
$ DataBaseLogFile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile')
($ DataBase, "MyDatabase_Log")
$ DataBaseLogFile.FileName = "D: NewDB_Log.ldf"
$ DataBase.Create ()
Figure 1.0
The above command created a database named 'MyDatabase' on the 'HOME' server, using the default values and paths.
Figure 1.1
Figure 1.2
Method 2
Suppose that we have to compose a new database named 'MyDatabase1' on the 'HOME' server. Let's assume the data file will be 25MB with the Data file and the log file located on drive D.
Execute the following command in PowerShell:
[System.Reflection.Assembly] :: LoadWithPartialName ("Microsoft.SqlServer.SMO")
$ Server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'HOME'
$ DataBase = new-object ('Microsoft.SqlServer.Management.Smo.Database')
($ Server, "MyDataBase1")
$ FileGrowth = new-object ('Microsoft.SqlServer.Management.Smo.FileGroup')
($ DataBase, "PRIMARY")
$ DataBase.FileGroups.Add ($ FileGrowth)
$ DataBaseDataFile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile')
($ FileGrowth, "MyDatabas1e_Data")
$ FileGrowth.Files.Add ($ DataBaseDataFile)
$ DataBaseDataFile.FileName = "D: MyDatabase1_Data.mdf"
$ DataBaseDataFile.Size = [double] (25.0 * 1024.0)
$ DataBaseDataFile.GrowthType = "Percent"
$ DataBaseDataFile.Growth = 25.0
$ DataBaseDataFile.MaxSize = [double] (100.0 * 1024.0)
$ DataBaseLogFile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile') ($ DataBase, "MyDatabase1_Log")
$ DataBaseLogFile.FileName = "D: MyDatabase1_Log.ldf"
$ DataBase.Create ()
The above Cmdlet creates a database 'Mydatabase1' on the 'HOME' server with a 25MB data file, both the Data file and the Log log file are created on drive D (Figure 1.3 and 1.4).
Figure 1.3
Figure 1.4
Method 3
Create a script that allows us to create any database on any server as well as any size and path that we like.
Create the file below as shown below and save it with the file createdb.ps1 . (Figure 1.5)
param ([string] $ ServerName, [string] $ DatabaseName, [Double] $ DataSize, [string] $ DataPath, [string] $ LogPath)
echo "Creating Database ." echo "----------------------" echo "Input ." echo "Server Name: $ ServerName" echo "Database Name: $ DatabaseName" echo "Data Size: $ DataSize" echo "Data File Path: $ DataPath" echo "Log File Path: $ LogPath"
$ LogicalDataFile = $ DatabaseName + "_Data" $ LogicalLogFIle = $ DatabaseName + "_Log" $ datapath1 = $ DataPath + "" + $ DatabaseName + "_Data.mdf" $ Logpath1 = $ LogPath + "" + $ DatabaseName + "_Log. ldf "$ DataSize1 = [double] ($ DataSize * 1024.0)
[System.Reflection.Assembly] :: LoadWithPartialName ("Microsoft.SqlServer.SMO")
$ Server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $ ServerName $ DataBase = new-object ('Microsoft.SqlServer.Management.Smo.Database') ($ Server, $ DatabaseName) $ FileGrowth = new-object ('Microsoft.SqlServer.Management.Smo.FileGroup') ($ DataBase, "PRIMARY") $ DataBase.FileGroups.Add ($ FileGrowth)
$ DataBaseDataFile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile') ($ FileGrowth, $ LogicalDataFile) $ FileGrowth.Files.Add ($ DataBaseDataFile)
$ DataBaseDataFile.FileName = $ datapath1
$ DataBaseDataFile.Size = [double] ($ DataSize1) $ DataBaseDataFile.GrowthType = "Percent" $ DataBaseDataFile.Growth = 25.0 $ DataBaseDataFile.MaxSize = [double] (100.0 * 1024.0)
$ DataBaseLogFile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile') ($ DataBase, $ LogicalLogFIle) $ DataBaseLogFile.FileName = $ Logpath1
$ DataBase.Create () echo "Output ." echo "Logical name of Data is $ LogicalDataFile" echo "Logical name of Log is $ LogicalLogFile" echo "Data File Path is $ datapath1" echo "Log file path is $ LogPath1 "echo" Size of the file is $ DataSize1 "
Figure 1.5
Now execute the PowerShell script file as shown in the figure below (Figure 1.6).
./createdb.ps1 HOME MyDatabase2 30 D: D:
Note :
- HOME is the server name
- MyDatabase2 is the database name
- 30 is the data size calculated in MB
- D: is the location of the data path
- D: is the location of the log path
Output
PS C: ps> ./createdb.ps1 HOME MyDatabase2 30 D: D: Creating Database . ---------------------- Input . Server Name: HOME Database Name: MyDatabase2 Data Size: 30 Data File Path: D: Log File Path: D:
GAC Version Location --- ------- -------- True v2.0.50727 C: WINDOWSassemblyGAC_MSILMicrosoft.SqlServer.SMO9.0.242.0__89845dcd8080cc91Microsoft.Sql. Output . Logical name of Data is MyDatabase2_Data Logical name of Log is MyDatabase2_Log Data File is D: MyDatabase2_Data.mdf Log file path is D: MyDatabase2_Log.ldf Size of the file is 30720
Figure 1.6
The code below creates the desired output.
Command code
[System.Reflection.Assembly] :: LoadWithPartialName ("Microsoft.SqlServer.SMO")
Output
GAC Version Location
--- ------- --------
True v2.0.50727 C: WINDOWSassemblyGAC_MSILMicrosoft.SqlServer.SMO9.0.242.0__89845dcd8080cc91Microsoft.Sql .
This can be avoided by redirecting the output to NULL.
param ([string] $ ServerName, [string] $ DatabaseName, [Double] $ DataSize, [string] $ DataPath, [string] $ LogPath)
echo "Creating Database ." echo "----------------------" echo "Input ." echo "Server Name: $ ServerName" echo "Database Name: $ DatabaseName" echo "Data Size: $ DataSize" echo "Data File Path: $ DataPath" echo "Log File Path: $ LogPath"
$ LogicalDataFile = $ DatabaseName + "_Data" $ LogicalLogFIle = $ DatabaseName + "_Log" $ datapath1 = $ DataPath + "" + $ DatabaseName + "_Data.mdf" $ Logpath1 = $ LogPath + "" + $ DatabaseName + "_Log. ldf "$ DataSize1 = [double] ($ DataSize * 1024.0)
[System.Reflection.Assembly] :: LoadWithPartialName ("Microsoft.SqlServer.SMO") | out-null
$ Server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $ ServerName $ DataBase = new-object ('Microsoft.SqlServer.Management.Smo.Database') ($ Server, $ DatabaseName) $ FileGrowth = new-object ('Microsoft.SqlServer.Management.Smo.FileGroup') ($ DataBase, "PRIMARY") $ DataBase.FileGroups.Add ($ FileGrowth)
$ DataBaseDataFile = new-object ('Microsoft.SqlServer.Management.Smo.DataFile') ($ FileGrowth, $ LogicalDataFile) $ FileGrowth.Files.Add ($ DataBaseDataFile)
$ DataBaseDataFile.FileName = $ datapath1
$ DataBaseDataFile.Size = [double] ($ DataSize1) $ DataBaseDataFile.GrowthType = "Percent" $ DataBaseDataFile.Growth = 25.0 $ DataBaseDataFile.MaxSize = [double] (100.0 * 1024.0)
$ DataBaseLogFile = new-object ('Microsoft.SqlServer.Management.Smo.LogFile') ($ DataBase, $ LogicalLogFIle) $ DataBaseLogFile.FileName = $ Logpath1
$ DataBase.Create () echo "Output ." echo "Logical name of Data is $ LogicalDataFile" echo "Logical name of Log is $ LogicalLogFile" echo "Data File Path is $ datapath1" echo "Log file path is $ LogPath1 "echo" Size of the file is $ DataSize1 "
Execute the PowerShell script file as shown below (Figure 1.7)
./createdb.ps1 HOME MyDatabase2 30 D: D:
Figure 1.7
Output
PS C: ps> ./createdb.ps1 HOME MyDatabase2 30 D: D:
Creating Database .
----------------------
Input .
Server Name: HOME
Database Name: MyDatabase2
Data Size: 30
Data File Path: D:
Log File Path: D:
Kết quả .
Logical name of Data is MyDatabase2_Data
Logical name of Log is MyDatabase2_Log
Data File Path is D: MyDatabase2_Data.mdf
Log file path is D: MyDatabase2_Log.ldf
Size of tập tin là 30720
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 6
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 7
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 8
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 9
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 10
Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 11
You should read it
- Database monitoring in MS SQL Server
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 9
- Use the ALTER DATABASE command to migrate DATABASE in SQL Server
- Learn about the role concept in SQL Server
- The difference between Truncate and Delete in Microsoft SQL Server
- Basic operations with Database in Microsoft SQL Server
- Steps to install Microsoft SQL Server on Windows 10
- SQL Server 2019 - Microsoft Relational Database Management System
May be interested
- 10 tips with PowerShell in Windows Server 2008 - Part 1in fact, there are a lot of windows server 2008 tasks that we can do a lot faster with powershell than the gui-based application or tool. in the following article, we will introduce you some basic and most frequently used operations with powershell ...
- Test SQL Server with Windows PowerShell - Part 5in this part 5, we will check whether we can connect to sql server and see if we can query some properties related to sql server.
- Test SQL Server with Windows PowerShell - Part 6part 6 will show you how to check all existing databases in the sql server instance and query the database properties.
- Instructions on how to use PowerShell in Windows Server 2012what is powershell? windows powershell is a command-line shell language interpreter and scripting language specifically designed for system administrators. built on the .net framework, windows powershell helps it professionals control and automate windows operating system administration tasks as well as applications running on windows server environments.
- Test SQL Server with Windows PowerShell - Part 1instead of using vbscript, bat files, or sql client implementations, ... we'll show you the power of windows powershell in checking sql servers status.
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 1as you probably know, windows powershell is a new command-line utility and a scripting language that provides a command-line environment for computer interaction and administration surveys. in addition, it provides a scripting capability for fish
- Test SQL Server with Windows PowerShell - Part 3in part 3, i will show you how to find some hardware and operating system information from the host machine.
- Next time, Microsoft will release PowerShell updates via Windows Update Windowsmicrosoft is making it easier to update powershell on windows 10 and windows server devices by releasing updates via microsoft update in the future.
- Test SQL Server with Windows PowerShell - Part 4in this next section, i will show you the information about network card and hard drive from the server.
- Test SQL Server with Windows PowerShell - Part 7in this section, i will show you how to get that information in top 10 queries based on cpu performance.