none
Using Powershell to create Pervasive database RRS feed

  • Question

  • In my quest to replace a bunch of old scripts and programs (blend of VB, bat files, manual intervention) to automate some processes, I'm trying to create a pervasive.sql database with Powershell. They currently use an old VB script to do this, and the code is as follows:

    Function CreatedB(strPath,strDDFPath,strdBName)
     Set mdtoDatabase = CreateObject("DTO.DtoDatabase.2")
     With mdtoDatabase
      mdtoDatabase.DataPath = strPath
      mdtoDatabase.DdfPath = strDDFPath
      mdtoDatabase.Name = strdBName
      mdtoDatabase.Flags = iFlags
     End With
     result = mdtoSession.Databases.Add(mdtoDatabase)
     '-----------------
     If result > 0 Then
      msgbox "Error Adding DBN. Result: " & cstr(result)
      If cstr(result) = 7012 Then
      Else
       strDBMsg = strDBMsg & "NEED ATTENTION: Error adding DB " & strdBName & ". Result: " & cstr(result)
      End If
     End if
    End Function

    Seems pretty simple! However, I'm not sure how to, or if I can, get this ported to Powershell directly. Not sure that PS has the ability to work with DTO ? I could sure use some guidance on this one...I am open to other suggestions as well. I've been looking at possibly using ado.net, but that seems to connect to "existing" dbs, and I can't really see anything to actually create a new db.

    Thanks in advance!

    mpleaf

    Tuesday, March 25, 2014 5:04 PM

Answers

  • You can run the same code in PowerShell but not if you do not know how to use PowerShell.  Start by studying PowerShell.  Learn how to use COM with PowerShell.

    Here is a partial example.   The rest you can easily do once you have learned PowerShell.

    $mdtoDatabase = New-Object -Com DTO.DtoDatabase
    $mdtoDatabase.DataPath = $Path
    $mdtoDatabase.DdfPath = $DDFPath
    $mdtoDatabase.Name = $dBName
    $mdtoDatabase.Flags = $Flags
    $mdtoSession.Databases.Add($mdtoDatabase)


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, March 25, 2014 5:51 PM
    • Marked as answer by mpleaf Tuesday, March 25, 2014 8:14 PM
    Tuesday, March 25, 2014 5:51 PM

All replies

  • Did you try it?  There is no reason for it to not work.


    ¯\_(ツ)_/¯

    Tuesday, March 25, 2014 5:32 PM
  • Pervasive SQL can use ADO.Net with OLEDB provider.  See Pervasive developer forum for help.


    ¯\_(ツ)_/¯

    Tuesday, March 25, 2014 5:33 PM
  • Try what? The code I put here, is a "piece" of a much larger VB program. That program uses a bunch of inputs from the user to create databases and such. This section, appears to be the part that actually creates the db itself in PSql. That's the part I need to put in PS. I "assume" I cannot just take VB code an put it in PS? :)

    mpleaf

    Tuesday, March 25, 2014 5:36 PM
  • Not sure where that is, if you have a link or something, I would appreciate it. Yeah, it looks like ADO.net can be used, but I can only report/query on existing dbs. I'm not familiar enough to create a new db from that.

    mpleaf

    Tuesday, March 25, 2014 5:40 PM
  • You can run the same code in PowerShell but not if you do not know how to use PowerShell.  Start by studying PowerShell.  Learn how to use COM with PowerShell.

    Here is a partial example.   The rest you can easily do once you have learned PowerShell.

    $mdtoDatabase = New-Object -Com DTO.DtoDatabase
    $mdtoDatabase.DataPath = $Path
    $mdtoDatabase.DdfPath = $DDFPath
    $mdtoDatabase.Name = $dBName
    $mdtoDatabase.Flags = $Flags
    $mdtoSession.Databases.Add($mdtoDatabase)


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, March 25, 2014 5:51 PM
    • Marked as answer by mpleaf Tuesday, March 25, 2014 8:14 PM
    Tuesday, March 25, 2014 5:51 PM
  • Thanks JRV for telling me it's possible, and that I'm in over my head on this. :( I've been doing a lot with the built in functions as of late, but certainly not this "deep'". Unfortunately, I don't have the time to learn this deeply right now. I'm trying, but I can never go fast enough...or in this case, perhaps take the step back that I need to do get some fundamentals in place. Any tutorials that you would recommend that might help me advance this more quickly? Or any other advice other than "learn powershell"? I AM trying! :)

    mpleaf

    Tuesday, March 25, 2014 7:28 PM
  • JRV, by telling me it "was" possible, and giving me the above, I was able to use the get-member to figure out which aspects I needed, and I was actually able to do this! Guess I understood it a little better than I thought! Thanks for the nudge!

    $Path = 'E:\myPath'
    $DDFPath = 'E:\myPath'
    $dBName = 'myDb'
    $Flags = 0
    $TaskUser = 'MyUser'
    $TaskPass = 'MyPass'

    $mdtoSession = New-Object -ComObject DTO.DtoSession
    $connected = $mdtoSession.connect('localhost', $TaskUser, $TaskPass)

    if ($mdtoSession.connected)
        {
        echo 'connected'
        $mdtoDatabase = New-Object -ComObject DTO.DtoDatabase
        $mdtoDatabase.DataPath = $Path
        $mdtoDatabase.DdfPath = $DDFPath
        $mdtoDatabase.Name = $dBName
        $mdtoDatabase.Flags = $Flags
        $mdtoSession.Databases.Add($mdtoDatabase)
        }
        else
        {
        echo 'no go'
        }

    ...and voila! My db was created!

    mpleaf

    Tuesday, March 25, 2014 8:14 PM
  • It is hard to answer  such broad and untargeted questions.  The questions always get better once you get your feet wet and try things.

    The COM model is very much the same for all DB products in VBScript, VB, C# or PowerShell.  Some products provide full Net classes for db management such as MSSQLServer and Oracle.  I believe Pervasive now has Net support.

    Always go the the vendor site/forum before asking questions in a code forum.  It is more likely that you will find an answer or that you will come up with a better question.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, March 25, 2014 9:31 PM
    Tuesday, March 25, 2014 9:30 PM