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 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply