【问题标题】:Powershell Get-QADUser results to SQL tablePowershell Get-QADUser 结果到 SQL 表
【发布时间】:2012-07-23 17:47:43
【问题描述】:

我正在使用一个字符串查询 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()

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

【问题讨论】:

    标签: sql powershell pipe


    【解决方案1】:

    假设测试对象如下:

    $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 块的参考,请参阅 here

    【讨论】:

      【解决方案2】:

      这里我使用的是 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
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-07
        • 2015-01-31
        • 2021-08-05
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        相关资源
        最近更新 更多