Create Blank Access Database Using Powershell

2019-08-31 18:49发布

问题:

Normally I'd research the topic I need for a day or 2 and then ask here, but I've been struggling to find anything at all about this topic.

Does anyone know how to use PowerShell (V3) to create a new Access 2010 MDB file, with a defined table and column headings?

Thanks for any guidance.

回答1:

Found some! Just for interests sake for anyone checking, here is the code:

Function Create-DataBase($Db){
 $application = New-Object -ComObject Access.Application
 $application.NewCurrentDataBase($Db,10)
 $application.CloseCurrentDataBase()
 $application.Quit()
}
Function Invoke-ADOCommand($Db, $Command){
 $connection = New-Object -ComObject ADODB.Connection
 $connection.Open("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=$Db")
 $connection.Execute($command)
 $connection.Close()
}
$Db = "C:\ScheduledCodeRun\CollatedData\Test.mdb"
$table = "MyTest"
$Fields = "F1 Counter, F2 Date, F3 Integer, F4 Text"
$command = "Create Table $table($fields)"
If(Test-Path $Db){
    Write-Host 'DB already exists' -fore green
}else{
    Create-DataBase $db
    Invoke-ADOCommand $Db 'Create Table MyTest(F1 Counter, F2 Date, F3 Integer, F4 Text)'
}