Powershell 取消设置访问 .mdb 密码 - 密码已知

Powershell Unset Access .mdb Password - Password Known

我组织的共享驱动器上有一个 .mdb,它受密码保护并且需要保持密码保护。我有密码。我使用本地副本进行数据分析。共享驱动器对我来说非常慢,所以我每天使用 powershell 复制它。我需要手动删除密码,因为我可用的分析软件不支持.mdb 密码。

下面的代码成功打开了 .mdb,因此我可以手动取消设置密码,但我想自动取消设置密码。

$path = "C:\path\to\db\"
$file = "db.mdb"  
$access = New-Object -com Access.Application
$access.Visible = $True
$access.OpenCurrentDataBase($path + $file, $True, "password")

我认为我的问题是我不理解 PowerShell 的对象模型实现。我想使用 Database.NewPassword 更改密码。然而,在 VB 实现中,它需要一个数据库对象,当我用 $db = $access.OpenCurrentDataBase($path + $file, $True, "password") 代替 $access.OpenCurrentDataBase($path + $file, $True, "password") 时,$db 变量是 $null.

有没有办法在 PowerShell 中引用数据库对象?

您可以为此使用 DAO,DbEngine.Compact method. It's a pain that powershell does not allow naming parameters and skipping the optionals, though, unless you want to do complicated stuff

$path = "C:\path\to\db\"
$file = "db.mdb"
$newfile = "decrypted.mdb"
$dbe= New-Object -com DAO.DbEngine.120
$dbe.Compact($path + $file, $path + $newfile, ";LANGID=0x0409;CP=1252;COUNTRY=0", 64, ";PWD=password")

您可以使用 Access DDL 管理数据库密码。

ALTER DATABASE PASSWORD newpassword oldpassword

这是我使用 PowerShell 删除密码的方式:

$path = "C:\Temp\DbPwd_foo.mdb"
$newpassword = "Null"
$oldpassword = "foo"
$accessApp = new-object -com access.application
$accessApp.OpenCurrentDatabase($path, -1, $oldpassword)
$sql = "ALTER DATABASE PASSWORD " + $newpassword + " " + $oldpassword
$accessApp.CurrentProject.Connection.Execute($sql)
$accessApp.Quit()

您可以通过交换 $newpassword$oldpassword 的值并在不带密码参数的情况下调用 OpenCurrentDatabase 来恢复以前的密码。