April 19, 2020 at 5:47 pm
Good morning all
I am looking to create a sql server database if the database does not exist on my instance
I use this script for this
Import-Module "SQLPS" -DisableNameChecking
Import-Module -Name "${PSScriptRoot}\Module\logModule.psm1" -DisableNameChecking
Add-Type -AssemblyName "Microsoft.SqlServer.Smo"
$servers= 'DESKTOP-95KKMMH\PRODUCTION','DESKTOP-95KKMMH'
$database='produit'
foreach($server in $servers)
{
Set-Location SQLSERVER:\SQL\$server
$db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -Argumentlist $server, $database
if($db -ne $null)
{
$db.Create()
}
else
{
InitLog -PathLog "${PSScriptRoot}\${Instance}\DbaWhoIsActive.log" -NameScript "PsBackupFull"
Log -message "Server : ${$server}"
Install-DbaWhoIsActive -SqlInstance $server -Database $database
}
}
attached the error that I have if the base exists
thanks for your help
April 20, 2020 at 10:57 am
Your New-Object command is running before you've done any existence check. If the database already exists then you'll get an error.
John
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy