header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
writing memberOf to SQL using get-QADuser
Last Post 17 Dec 2009 07:29 AM by Bobdee. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BobdeeUser is Offline
Basic Member
Basic Member
Posts:130
Avatar

--
16 Dec 2009 05:55 AM

    Hi,

    I'm using get-QADuser to query AD, then I am writing those results to a local DB.  However, when I write the memberOf attribute to the DB, I find that it only writes sytemString[] which I believe is because ADSI stores this data as an array??

    Could anyone point me in the right direction of how to convert this into something I could write to a MSSQL DB?

    Here is basically what I am using - I am using more ADSI attributes, but it was confusing when I pasted into this post.
    ----
    invoke-expression sqlcmd

    Get-qaduser xxxxx -IncludeAllProperties -SizeLimit 0 | select samaccountname,memberOf | foreach {

     $sql= "INSERT INTO robdb.dbo.robdb.dbo.Test_tbl (samaccountname,memberOf) VALUES ( '{0}','{1}' )" -f $_.SamAccountName,$_.memberof
     
     invoke-sqlcmd $sql
    }
    ----
    Thanks

    Rob.

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    17 Dec 2009 02:58 AM
    You need to join the value to one string:

    $sql= "INSERT INTO robdb.dbo.robdb.dbo.Test_tbl (samaccountname,memberOf) VALUES ( '{0}','{1}' )" -f $_.SamAccountName,[string]::join(";",$_.memberof)

    With PowerShell 2.0 you can use the -join operator:

    $sql= "INSERT INTO robdb.dbo.robdb.dbo.Test_tbl (samaccountname,memberOf) VALUES ( '{0}','{1}' )" -f $_.SamAccountName, ($_.memberof -join ";")

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    17 Dec 2009 06:11 AM
    Hi agian,

    Thanks for the response. It has worked - however, I'm now finding that the Insert is attempting to write more prefixes to the table than allowed - giving the error - The object name 'robdb.dbo.robdb.dbo.Test_tbl' contains more than the maximum number of prefixes. The maximum is 3.

    I've tried a replace function in the SQL, which gave the same response

    $sql= "INSERT INTO robdb.dbo.robdb.dbo.Test_tbl (samaccountname,replace(memberOf,''',''',' ') VALUES ( '{0}','{1}' )" -f $_.SamAccountName, ($_.memberof -join ";")

    write-host $sql

    invoke-sqlcmd $sql

    Now I wonder is it possible to remove the quote marks in the memberOf array so that the SQL can insert it as needed?

    Thanks again

    Rob

    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    17 Dec 2009 06:11 AM
    Using PSv2
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    17 Dec 2009 06:40 AM
    Try with PowerShell's replace operator:

    $sql= "INSERT INTO robdb.dbo.robdb.dbo.Test_tbl (samaccountname,memberOf) VALUES ( '{0}','{1}' )" -f $_.SamAccountName, (($_.memberof -join ";") -replace "'"," ")


    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    BobdeeUser is Offline
    Basic Member
    Basic Member
    Posts:130
    Avatar

    --
    17 Dec 2009 07:29 AM
    Shay, you are a legend. Thank you very much, it is much appreciated.

    Merry Christmas.

    Robbie.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer