3

我正在使用一个字符串查询 Active Directory,该字符串遍历我们系统中的每个域控制器并返回一组结果。该脚本适用于 export-csv,但因为我们希望保留自定义信息字段中的所有数据(它包含回车符),所以我想将其直接导出到 SQL 表中。

Powershell 报告的错误内容如下:

使用“0”参数调用“ExecuteNonQuery”的异常:“插入错误:列名或>提供的值的数量与表定义不匹配。”

这是一个非常冗长的响应,我创建并命名了每个表的列以完全匹配 get-object 的输出。

这是管道的输出:

SamAccountName     : testuser
DisplayName        : Test User (COMPANY)
info               : Test Entry 1234567890

                 Test for output.
                 Entering


                 Multiple lines.
whenCreated        : 09/11/2004 09:08:42
whenChanged        : 19/07/2012 09:25:21
AccountExpires     :
pwdLastSet         : 13/06/2012 07:43:43
LastLogonTimestamp : 18/07/2012 15:38:35
userAccountControl : 512
Name               : Test User
LastLogon          :
DC                 : DCNAME1

这是代码:

##--AD data output to SQL script, you need the Quest plugin!

$SamAccountName = Read-Host "Enter the username to query for last logon"

##--Query domain for all domain controllers and funnel into a forEach loop

Get-QADComputer -ComputerRole DomainController | Foreach-Object{
$dc = $_.Name

##--Query each domain controller for the user object and retrieve the LastLogon timestamp

$user = Get-QADUser -Service $dc  -SamAccountName $SamAccountName -IncludedProperties info,pwdLastSet,AccountExpires,userAccountControl | Select-Object SamAccountName,displayName,info,whenCreated,whenChanged,accountExpires,pwdLastSet,lastLogonTimestamp,userAccountControl,name,LastLogon,@{n='DC';e={$dc}} |

out-host

##--Open database connection

$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=SQLSERVER; Initial Catalog=ADomain; Integrated Security=SSPI")
$conn.Open()

##--AAGH! How to grab the results of the Select-Object above?

$cmd = $conn.CreateCommand()
$cmd.CommandText ="INSERT extract VALUES ('$user')"
$cmd.ExecuteNonQuery()

##--Don't forget to close it!

$conn.Close()

现在我搞砸了一些可能很明显的事情,非常感谢任何帮助。

4

2 回答 2

0

假设以下测试对象:

$exampleObject = New-Object PSObject -Property @{SamAccountName="TestSAN";DisplayName="TestDN"}

$user = $exampleObject | Select-Object SamAccountName, DisplayName

##--AAGH! How to grab the results of the Select-Object above?
$commandText = "INSERT extract VALUES ('$($user.SamAccountName)','$($user.DisplayName)'"

我还将 sql connect 和 close 移到循环外部并将其放在 try/finally 块中,因此它只执行一次,而不是每个 DC 条目,并且在执行期间出现异常时仍会调用 Close的 try 块。有关在Powershell 中使用 try/catch/finally 块的参考信息,请参见此处。

于 2012-07-25T15:54:08.513 回答
0

这里我使用的是 SqlServerCmdletSnapin,它是 Invoke-sqlcmd。此脚本使用 MSSQL 数据库 EMPLOYEE 和表 EMPLOYEE_DOMAIN。希望能帮助到你。对我来说很好..

Add-PSSnapin Quest.ActiveRoles.ADManagement
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100

$db_server = "10.3.18.55"
$db = "EMPLOYEE"
$table = "EMPLOYEE_DOMAIN"
$username = "import"
$pwd = "Okinawa84561"

# First, clear existing table
$sql_query_del = "DELETE FROM $table"
Invoke-Sqlcmd -ServerInstance $db_server -Database $db -Username $username -Password $pwd -Query $sql_query_del

# Get users with employeeID only and write their accountname and employeeID to DB
Get-QADUser -IncludeAllProperties | ? {$_.employeeID} | select sAMAccountName, employeeID | foreach {
    $an = $_.sAMAccountName
    $eid = $_.employeeID
    Write-Host " sAMAccountName : $an       employeeID : $eid"

    $sql_query = "INSERT INTO $table (employeeID, domainName) VALUES ('$eid', '$an')"
    Invoke-Sqlcmd -ServerInstance $db_server -Database $db -Username $username -Password $pwd -Query $sql_query

}
于 2013-02-05T09:05:57.283 回答