Database creation in Powershell 1.0 with SQL Server 2005

  • 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.

  • 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.

  • Yes that is a typo. Thank you for pointing it out.

  • 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