我正在尝试通过在文件夹中搜索* .dacpac文件来在Sql Server中创建数据库。对于每个.dacpac发现我需要创建一个DB,其名称为找到.dacpac的文件夹。
实施例
C:\ Folder \ contains
创建Db的脚本
cd "C:\Program Files (x86)\Microsoft SQL Server\130\DAC\bin"
$createDB = .\sqlpackage.exe /Action:Publish /SourceFile:"c:\Folder\Project1\mydatabase.dacpac" /TargetDatabaseName:"Project1" /TargetServerName:"(LocalDB)\MSSQLLocalDB"
我可以使用以下
获取要用作TargetDatabaseName的文件夹名称 PS C:\> $FolderName = Get-ChildItem C:\Folder | select -ExpandProperty Name
$FolderName
Project1
Project2
Project3
我正在尝试使用foreach循环来查找dacpac文件并将它们用作SourceFile,然后查找FolderName以用作TargetDatabaseName
$FolderName = Get-ChildItem C:\Folder | select -ExpandProperty Name
$dacpacfile = Get-ChildItem C:\Folder -Recurse -Include *.dacpac
foreach ($dacpac in $dacpacfile){
$createDB = .\sqlpackage.exe /Action:Publish /SourceFile:$dacpacfile /TargetDatabaseName:$FolderName /TargetServerName:"(LocalDB)\MSSQLLocalDB"}
收到的错误是:
。\ sqlpackage.exe: *无法从中加载包 “C:\文件夹\ PROJECT1 \ mydatabase.dacpac C:\文件夹\ Project2的\ mydatabase.dacpac C:\文件夹\项目3 \ mydatabase.dacpac”。在行:4 char:21 + ... $ createDB =。\ sqlpackage.exe / Action:Publish / SourceFile:$ dacpacfile ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo:NotSpecified:(* 不能l ... tabase.dacpac'。:String)[],RemoteException + FullyQualifiedErrorId:NativeCommandError
我希望我需要创建两个数组,然后尝试逐步完成它们,但是无法做到这一点,我们将非常感谢任何帮助。
答案 0 :(得分:0)
在这里,试试这个:
$dacpacFiles = Get-ChildItem C:\project -Recurse -Include *.dacpac
cd "C:\Program Files (x86)\Microsoft SQL Server\140\DAC\bin"
foreach ($dacpacFile in $dacpacFiles)
{
$dir = Split-Path $dacpacFile.Directory -Leaf
$filePath = $dacpacFile.FullName
$createDB = .\sqlpackage.exe /Action:Publish /SourceFile:$filePath /TargetDatabaseName:$dir /TargetServerName:"."
}