【问题标题】:PowerShell and MS Access databasePowerShell 和 MS Access 数据库
【发布时间】:2018-01-15 08:43:46
【问题描述】:

我们想使用 PowerShell 创建 AD 用户。使用 CSV 这不是问题,使用脚本很容易。下一级我们想用 PowerShell 和 MS Access 数据库创建 AD 用户。现在我们有以下问题: 我们可以读取 Access 数据库,我们将它加载到一个对象中,但是当我们启动我们的脚本时,它说它是一个对象而不是一个字符串。

因此,当我们将对象转换为字符串时,它会加载字符串中的所有行并创建一个具有所有名称的用户。

PowerShell 脚本是:

$DatabaseName = "c:\temp\Nordwind.mdb"
$Query = "SELECT * FROM Users "
$ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=$DatabaseName"
$Connection = New-Object System.Data.OleDb.OleDbConnection $ConnectionString
$Command  = New-Object System.Data.OleDb.OleDbCommand $Query, $Connection
$Connection.Open()
$Adapter = New-Object System.Data.OleDb.OleDbDataAdapter $Command
$Dataset = New-Object System.Data.DataSet
[void] $Adapter.Fill($DataSet)
$Connection.Close()
$x = Dataset.Tables
foreach ($u in $x) {
    New-ADUser -Name $u.name ...
}

这是 PowerShell 中的错误:

无法将“System Object[]”转换为参数“String”所需的类型“System.String”。

我们可以用线条转换

$Name = [string]u.name
New-ADUser -Name $Name ...

当我们要添加 10 个用户时,它会添加一个名称为 10 个的用户。 我们需要帮助才能使用 PowerShell 从 Access 数据库中读取和转换一行。

【问题讨论】:

    标签: powershell ms-access scripting


    【解决方案1】:

    数据集包含一个表列表,因此当您需要迭代表中的行时,您正在迭代数据集中的表(即使它只是一个表)。

    改变这个:

    $x = Dataset.Tables
    foreach ($u in $x) {
        New-ADUser -Name $u.name ...
    }
    

    进入这个:

    $x = Dataset.Tables[0]
    foreach ($u in $x) {
        New-ADUser -Name $u.name ...
    }
    

    问题应该会消失。

    【讨论】:

    • 这很容易找到:-D
    【解决方案2】:

    感谢以上起点! 由于我是新人,我只是在 MS Access 中使用一张包含 7 条用户记录的表进行测试。

    ID  User
    1   Joe
    2   Bob
    3   Ray
    4   Tom
    5   Bill
    6   Brian
    7   Sam
    

    我将上面的脚本修改成这样:

    #Get the db
    $DatabaseName = "c:\scripts\Test1.accdb"
    #Sql
    $Query = "SELECT * FROM Users"
    #Specify the connection string and type
    $ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=$DatabaseName"
    #Create the new object Connection String
    $Connection = New-Object System.Data.OleDb.OleDbConnection $ConnectionString
    #Create the command we are sending to the db
    $Command  = New-Object System.Data.OleDb.OleDbCommand $Query, $Connection
    #Open the connection
    $Connection.Open()
    #Looks like it's sending the command to the DB here
    $Adapter = New-Object System.Data.OleDb.OleDbDataAdapter $Command
    #Looks to be building the dataset
    $Dataset = New-Object System.Data.DataSet
    #Not sure what this does, but looks to use what the $DateSet contains and send it through to be used by the adapter
    [void] $Adapter.Fill($DataSet)
    
    #Gets the tables in the dataset, and starts at column 0.
    $datasetTables = $Dataset.Tables[0]
    #Loop through each record in the table
    foreach ($record in $datasetTables) {
        #Writes the value of Field1, in this case the users name
        write-host $record.User
    }
    #close connection
    $Connection.Close()
    

    返回:

    Joe
    Bob
    Ray
    Tom
    Bill
    Brian
    Sam
    

    感谢所有让我来到这里的线程!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2011-10-27
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多