使用Powershell创建空白访问数据库

时间:2014-08-13 03:45:20

标签: powershell ms-access-2010 powershell-v3.0

通常我会研究一天或两天所需的主题,然后在这里问一下,但我一直在努力寻找关于这个话题的任何内容。

是否有人知道如何使用PowerShell(V3)创建具有已定义的表格和列标题的新Access 2010 MDB文件?

感谢任何指导。

1 个答案:

答案 0 :(得分:1)

发现了一些!只是为了任何人检查的利益,这里是代码:

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)'
}