问题
I have a multi-user Access database which is compacted daily using a powershell script. This script can't compact the database if there are still users logged in. If there is a user logged in, I would like be able to idenify the user(s) who forgot to logout of the database and remind them to log out at the end of the day.
If I were to write this in VB it would work like this:
Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
cn.open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=...mdb")
Set rs = cn.OpenSchema(adSchemaProviderSpecific, ,"{947bb102-5d43-11d1-bdbf-00c04fb92675}")
Then loop through the recordset and get the information I want.
What I would like to do is translate this to PowerShell so it can run with the compact script. I have tried the following:
$objCon = New-Object -ComObject ADODB.Connection
$objRs = New-Object -ComObject ADODB.Recordset
$objCon.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=...mdb")
$objRs= $objCon.OpenSchema([ADODB.SchemaEnum]::adSchemaProviderSpecific,$null,'{947bb102-5d43-11d1-bdbf-00c04fb92675}')
$objRs.MoveFirst()
Then loop through the recordset and find the information I need.
The ps code errors out on the OpenSchema line with:
Exception calling "OpenSchema" with "3" argument(s): "Object or provider is not capable of performing requested operation." At FindUsers.ps1:8 char:27 + $objRs= $objCon.OpenSchema <<<< ([ADODB.SchemaEnum]::adSchemaProviderSpecific,$null,'{947bb102-5d43-11d1-bdbf-00c04fb92675}') + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
The script won't execute if I remove the middle $null variable or replace it with '', and I don't know if the command is translated from VB to powershell correctly. I've searched on Google and SO and haven't found any solutions. What do I need to do to use the OpenSchema command?
回答1:
This works for me using the newer ACE drivers included with Office 2007 and higher:
$filepath = "C:\Users\u00\Documents\Northwind.mdb"
[guid]$guid = '947bb102-5d43-11d1-bdbf-00c04fb92675'
$ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$filepath`";Persist Security Info=False;"
$conn = new-object System.Data.OleDb.OleDbConnection($ConnectionString)
$conn.open()
$conn.GetOleDbSchemaTable($guid,$null)
$conn.close()
COMPUTER_NAME LOGIN_NAME
------------- ----------
Z002 Admin
来源:https://stackoverflow.com/questions/13477778/find-users-logged-into-access-database-using-powershell