December 1, 2009 at 5:53 pm
I have written a powershell script that creates a database, but the log segment is defaults not the attributes that I specify. There are no errors during execution. I have scoured web sites and it appears that my powershell code is correct. This is what I have:
$server is an SMO server object that is created with a valid connection to the server.
$syslogname = $databaseName + "_Data"
$loglogname = $databaseName + "_Log"
$dbObject = new-object -typename Microsoft.SqlServer.Management.Smo.Database -argumentlist $server, $databaseName
$dbFileGroup = new-object -typename Microsoft.SqlServer.Management.Smo.FileGroup -argumentlist $dbObject, "PRIMARY"
$dbDatafile = new-object -typename Microsoft.SqlServer.Management.Smo.DataFile -argumentlist $dbFileGrp, $syslogname
$dbDatafile.FileName = "C:\dbLoc\$databaseName.mdf"
$dbDatafile.Size = [double]6144.0
$dbDatafile.GrowthType = "Percent"
$dbDatafile.Growth = [double]10.0
$dbDatafile.IsPrimaryFile = 'True'
$dbObject.FileGroups.Add($dbFileGrp)
$dbFileGrp.Files.Add($dbDatafile)
$dbLogFile = = new-object -typename Microsoft.SqlServer.Management.Smo.LogFile -argumentlist $dbObject, $loglogname
$dbLogfile.FileName = "C:\dbLoc\$databaseName.ldf"
$dbLogfile.Size = [double]1024.0
$dbLogfile.GrowthType = "Percent"
$dbLogfile.Growth = [double]10.0
$dbObject.create()
I am not sure why the log segment as defined here is ignored in the creation of the database. Any help would be appreciated.
December 1, 2009 at 6:28 pm
Not sure if it's just a typo when posting on the forum but the double equals in this line doesn't look right:
$dbLogFile = = new-object -typename Microsoft.SqlServer.Management.Smo.LogFile -argumentlist $dbObject, $loglogname
I don't have Powershell 1.0 installed anywhere to test it though.
December 1, 2009 at 6:31 pm
Yes that is a typo. Thank you for pointing it out.
December 1, 2009 at 6:48 pm
I resolved the issue. I thought I had tried this numerous times and failed, but apparently not. Before the $dbObject.Create() statement, I added:
$dbObject.Logfiles.Add($dbLogFile)
🙂
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply