Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Task Scheduling PowerShell Script To Create Access Database

$
0
0
I have this script that I have stolen and modified from other answers here at TechNet:

# Creates new blank Access database.
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\Database.mdb"
$table = "Table1"
$Fields = "col1 Text, col2 Text, col3 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 Table1(col1 Text, col2 Text, col3 Text)'
}
When I run the script in PowerShell ISE, it creates the new Access Database fine. However, when I run it in PowerShell command line, it doesn't.

Can someone please tell me where I'm going wrong?










Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>