新規にAccessのaccdbファイルを作成し、テーブルにデータを挿入するスクリプトを作ってみました。
CreateAccessDbAndInsertData.ps1
#引数の値を変数に格納(ユーザーID, 名前, 備考)
$userid = $args[0]
$name = $args[1]
$remarks = $args[2]
#DBファイル名
$scriptPath = $MyInvocation.MyCommand.Path
$currentPath = Split-Path -Parent $scriptPath
$db = Join-Path $currentPath "test.accdb"
If(-not(Test-Path -Path $db))
{
#DBファイルが存在しない場合作成する
$application = New-Object -ComObject Access.Application
$application.NewCurrentDataBase($db,12)
$application.CloseCurrentDataBase()
$application.Quit()
#テーブル作成
$connection = New-Object -ComObject ADODB.Connection
$connection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$db")
$table = "T_Users"
$fields = "ID Counter, ユーザーID Text, 名前 CHAR, 備考 Text"
$command = "Create Table $table `($fields`)"
$connection.Execute($command)
$connection.Close()
}
$connection = New-Object -ComObject ADODB.Connection
$connection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$db")
$insCmd = "Insert into T_Users `(ユーザーID, 名前, 備考`) Values `('$userid', '$name', '$remarks'`)"
$connection.Execute($insCmd)
$connection.Close()
このスクリプトファイルと同じ場所に「test.accdb」というaccdbファイルを作成し、「T_Users」というテーブルを作成しています。あと、実行する際には、32ビットで実行するか、64ビットで実行するかに気を付けてください。
私の環境では32ビットじゃないと実行できませんでした。
これはおそらく、インストールされているAccessが32ビット用だからだと思われます。
実行例
PS C:\work\access> .\CreateAccessDbAndInsertData.ps1 '100201' '鈴木一郎' 'びこう'
Properties : {Preserve on Abort, Blocking Storage Objects, Use Bookmarks, Skip Deleted Bookmarks...}
AbsolutePosition :
ActiveConnection : ADODB.ConnectionClass
BOF :
Bookmark :
CacheSize : 1
CursorType : adOpenForwardOnly
EOF :
Fields : {}
LockType : adLockReadOnly
MaxRecords : 0
RecordCount :
Source : Create Table T_Users (ID Counter, ユーザーID Text, 名前 CHAR, 備考 Text)
AbsolutePage :
EditMode :
Filter : 0
PageCount :
PageSize : 10
Sort :
Status :
State : 0
CursorLocation : adUseServer
MarshalOptions : adMarshalAll
DataSource : ADODB.RecordsetClass
ActiveCommand : System.__ComObject
StayInSync : True
DataMember :
Index :
Properties : {Preserve on Abort, Blocking Storage Objects, Use Bookmarks, Skip Deleted Bookmarks...}
AbsolutePosition :
ActiveConnection : ADODB.ConnectionClass
BOF :
Bookmark :
CacheSize : 1
CursorType : adOpenForwardOnly
EOF :
Fields : {}
LockType : adLockReadOnly
MaxRecords : 0
RecordCount :
Source : Insert into T_Users (ユーザーID, 名前, 備考) Values ('100201', '鈴木一郎', 'びこう')
AbsolutePage :
EditMode :
Filter : 0
PageCount :
PageSize : 10
Sort :
Status :
State : 0
CursorLocation : adUseServer
MarshalOptions : adMarshalAll
DataSource : ADODB.RecordsetClass
ActiveCommand : System.__ComObject
StayInSync : True
DataMember :
Index :

実際に作成されたtest.accdbファイルを開いてみると、このようにテーブルにデータが格納されています。
<参考サイト>
https://gallery.technet.microsoft.com/office/28a08699-7d60-492c-bc97-d6456a6c3739
スポンサーリンク