28. Februar 2015 01:51
function CreateNewDB80_NAVDEMO
{
$BasicPath = resolve-path $args
$TargetPath = (get-item $args).parent.FullName
$BackupPath = '\SQLDemoDatabase\CommonAppData\Microsoft\Microsoft Dynamics NAV\80\Database\Demo Database NAV (8-0).bak'
$FullBackupPath = [string]::Concat($BasicPath,$BackupPath)
# Use own servername here
$MySQLServerName = 'MyServerName\NAVDEMO'
$MyNewDatabaseName = Read-host "Name of new database"
# Adapt this block to the employed environment, as written here it is suited for SQL Server 2012 with an instance NAVDEMO
$RelocateData = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "Demo Database NAV (8-0)_Data", "C:\Program Files\Microsoft SQL Server\MSSQL11.NAVDEMO\MSSQL\DATA\$MyNewDatabaseName.mdf"
$RelocateLog = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "Demo Database NAV (8-0)_Log", "C:\Program Files\Microsoft SQL Server\MSSQL11.NAVDEMO\MSSQL\DATA\$MyNewDatabaseName.ldf"
$file = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile($RelocateData,$RelocateLog)
$myarr=@($RelocateData,$RelocateLog)
Restore-SqlDatabase -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -BackupFile $FullBackupPath -RelocateFil $myarr
Write-host "Creating local service account for database $MyNewDatabaseName"
import-module sqlps -disablenamechecking
$CreateServiceAccountUser = "CREATE USER [MyServerName\kkowa_000] FOR LOGIN [MyServerName\kkowa_000]"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $CreateServiceAccountUser
$AddServiceAccountAsDbo = "exec sp_addrolemember 'db_owner', 'MyServerName\kkowa_000'"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $AddServiceAccountAsDbo
#Set network service as dbo
Write-host "Creating Network Service account for database $MyNewDatabaseName"
$CreateServiceAccountUser = "CREATE USER [NT-AUTORITÄT\Netzwerkdienst] FOR LOGIN [NT-AUTORITÄT\Netzwerkdienst]"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $CreateServiceAccountUser
$AddServiceAccountAsDbo = "exec sp_addrolemember 'db_owner', 'NT-AUTORITÄT\Netzwerkdienst'"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $AddServiceAccountAsDbo
pop-location # Finished with SQL commands so popping back to normal PS prompt
}
function CreateNewDB71_NAVDEMO
{
$BasicPath = resolve-path $args
$TargetPath = (get-item $args).parent.FullName
$BackupPath = '\SQLDemoDatabase\CommonAppData\Microsoft\Microsoft Dynamics NAV\71\Database\Demo Database NAV (7-1).bak'
$FullBackupPath = [string]::Concat($BasicPath,$BackupPath)
# Use own servername here
$MySQLServerName = 'MyServerName\NAVDEMO'
$MyNewDatabaseName = Read-host "Name of new database"
# Adapt this block to the employed environment, as written here it is suited for SQL Server 2012 with an instance NAVDEMO
$RelocateData = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "Demo Database NAV (7-1)_Data", "C:\Program Files\Microsoft SQL Server\MSSQL11.NAVDEMO\MSSQL\DATA\$MyNewDatabaseName.mdf"
$RelocateLog = New-Object 'Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' -ArgumentList "Demo Database NAV (7-1)_Log", "C:\Program Files\Microsoft SQL Server\MSSQL11.NAVDEMO\MSSQL\DATA\$MyNewDatabaseName.ldf"
$file = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile($RelocateData,$RelocateLog)
$myarr=@($RelocateData,$RelocateLog)
Restore-SqlDatabase -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -BackupFile $FullBackupPath -RelocateFil $myarr
Write-host "Creating local service account for database $MyNewDatabaseName"
import-module sqlps -disablenamechecking
$CreateServiceAccountUser = "CREATE USER [MyServerName\kkowa_000] FOR LOGIN [MyServerName\kkowa_000]"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $CreateServiceAccountUser
$AddServiceAccountAsDbo = "exec sp_addrolemember 'db_owner', 'MyServerName\kkowa_000'"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $AddServiceAccountAsDbo
#Set network service as dbo
Write-host "Creating Network Service account for database $MyNewDatabaseName"
$CreateServiceAccountUser = "CREATE USER [NT-AUTORITÄT\Netzwerkdienst] FOR LOGIN [NT-AUTORITÄT\Netzwerkdienst]"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $CreateServiceAccountUser
$AddServiceAccountAsDbo = "exec sp_addrolemember 'db_owner', 'NT-AUTORITÄT\Netzwerkdienst'"
Invoke-Sqlcmd -ServerInstance $MySQLServerName -Database $MyNewDatabaseName -Query $AddServiceAccountAsDbo
pop-location # Finished with SQL commands so popping back to normal PS prompt
}