是否有在 PowerShell 中创建 LocalDB 数据库(不是实例)的单行程序?
Is there an one-liner to create LocalDB database (not instance) in PowerShell?
我想以 MDF 和 LDF 文件保留在脚本执行文件夹中的方式创建 LocalDB 数据库。
我知道我们可以在 PowerShell 中使用 $
运算符轻松地进行命令替换,因此,如果我声明类似 $pwd
的内容,它将用以下输出进行替换pwd
命令。
从 here,我知道 Invoke-SqlCmd
不是处理 LocalDB 实例的直接方法。
我想通过 PowerShell 执行以下命令。我怎样才能做到这一点?
sqlcmd -S "(localdb)\MSSQLLocalDB" -Q "
CREATE DATABASE [Test] ON PRIMARY (
NAME = N'Test',
FILENAME = N'<$pwd output here>\Test.mdf'
)
LOG ON (
NAME = N'Test_log',
FILENAME = N'<$pwd output here>\Test_log.ldf'
)"
这可能比您要求的要多得多,但它会解决您的问题。
这将询问您是否要从模板恢复,如果否,它将创建一个空数据库。
$backupFilePath=''
$TempDB=''
$TempDBlog=''
$DBName=''
$Logfilepath=''
$Datafilepath=''
$confirmation = Read-Host "Do you want to use a DB template? [y/n]"
If($confirmation.ToUpper().StartsWith("Y") )
{
$dbCommand = "use [master];RESTORE DATABASE [$DBName] " + "FROM DISK = N'$backupFilePath'" + "WITH FILE = 1, NOUNLOAD, STATS = 10, " + "move '$TempDB' to '$Datafilepath" + "\" + "$DBName" + ".mdf', " + "move '$TempDBlog' to '$Logfilepath" + "\" + "$DBName" + ".ldf';" + "alter database [$DBName] modify file (name=N'$TempDB ', newname=N'$DBName" + "_data1');" + "alter database [$DBName] modify file (name=N'$TempDBlog', newname=N'$DBName" + "_log1');" + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100;"
}
else {
$dbCommand = "use [master];CREATE DATABASE [$DBName] ON " + "( NAME='$DBName" + "_data1', FILENAME='$Datafilepath" + "\" + "$DBName" + "_data1.mdf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB)" + "LOG ON ( NAME='$DBName" + "_log1', FILENAME='$Logfilepath" + "\" + "$DBName" + "_log1.ldf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB); " + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100; " + "set AUTO_CLOSE OFF;"
}
有人将不得不把它编辑成漂亮的代码中断,因为它不允许我这样做。
我想以 MDF 和 LDF 文件保留在脚本执行文件夹中的方式创建 LocalDB 数据库。
我知道我们可以在 PowerShell 中使用
$
运算符轻松地进行命令替换,因此,如果我声明类似$pwd
的内容,它将用以下输出进行替换pwd
命令。从 here,我知道
Invoke-SqlCmd
不是处理 LocalDB 实例的直接方法。我想通过 PowerShell 执行以下命令。我怎样才能做到这一点?
sqlcmd -S "(localdb)\MSSQLLocalDB" -Q " CREATE DATABASE [Test] ON PRIMARY ( NAME = N'Test', FILENAME = N'<$pwd output here>\Test.mdf' ) LOG ON ( NAME = N'Test_log', FILENAME = N'<$pwd output here>\Test_log.ldf' )"
这可能比您要求的要多得多,但它会解决您的问题。
这将询问您是否要从模板恢复,如果否,它将创建一个空数据库。
$backupFilePath=''
$TempDB=''
$TempDBlog=''
$DBName=''
$Logfilepath=''
$Datafilepath=''
$confirmation = Read-Host "Do you want to use a DB template? [y/n]"
If($confirmation.ToUpper().StartsWith("Y") )
{
$dbCommand = "use [master];RESTORE DATABASE [$DBName] " + "FROM DISK = N'$backupFilePath'" + "WITH FILE = 1, NOUNLOAD, STATS = 10, " + "move '$TempDB' to '$Datafilepath" + "\" + "$DBName" + ".mdf', " + "move '$TempDBlog' to '$Logfilepath" + "\" + "$DBName" + ".ldf';" + "alter database [$DBName] modify file (name=N'$TempDB ', newname=N'$DBName" + "_data1');" + "alter database [$DBName] modify file (name=N'$TempDBlog', newname=N'$DBName" + "_log1');" + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100;"
}
else {
$dbCommand = "use [master];CREATE DATABASE [$DBName] ON " + "( NAME='$DBName" + "_data1', FILENAME='$Datafilepath" + "\" + "$DBName" + "_data1.mdf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB)" + "LOG ON ( NAME='$DBName" + "_log1', FILENAME='$Logfilepath" + "\" + "$DBName" + "_log1.ldf', SIZE=1GB, MAXSIZE=UNLIMITED, FILEGROWTH=200MB); " + "alter database [$DBName] set read_committed_snapshot on with rollback immediate;" + "alter database [$DBName] set COMPATIBILITY_LEVEL = 100; " + "set AUTO_CLOSE OFF;"
}
有人将不得不把它编辑成漂亮的代码中断,因为它不允许我这样做。