header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Array to CSV
Last Post 25 Sep 2008 03:53 PM by Shay. 5 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
OxnardUser is Offline
New Member
New Member
Posts:3
Avatar

--
24 Sep 2008 05:06 PM  

Hi folks,

I'm on Powershell since a week now, so I still got typical beginner problems. This is my current one:

I got a two dimensional array. Let's call it $array with the values 1, 2 and 3 in the first row an a, b and c in the second.

Now I want to put the content of the array in a csv in the following order:

line 1: 1;2;3
line 2: a;b;c

How can I achieve that? I think I need an output variable for each line, but I'm getting errors by filling that one. This is one of my several tests with combinations of brackets and quotes:


foreach ($row in $arrayΎ .. ($array.count -1)]) {
    $output = "$row[0];$row[1];$row[2]"
    Write-Output $output | Out-Host
  }

Can you guys help me?

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
25 Sep 2008 11:49 AM  
Can you share the array code?
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
OxnardUser is Offline
New Member
New Member
Posts:3
Avatar

--
25 Sep 2008 02:40 PM  

Hi Shay,

this is the summary of my problem. I figured out a workaround to get the output I want, but this is not the kind of solutions I prefer.

function create_line([string] $dn)
  $line = @()
  $line += $dn.split(",")[ 0].replace('"CN=', "").replace("\", "")
  $line += $dn.split(",")[ 1].replace(" ", "")
  $line += (dsget user $dn -disabled)[ 1]
  $line += $dn.split(",")[ 2].replace("OU=", "")
  $usrlist += $line
}
# --Main--
$usrlist = @()
$usr_accounts = dsquery user "ou=user,dc=example,dc=net" -limit 0
foreach($usr in $usr_accounts) {
  create_line $usr
}
foreach ($ln in $usrlist) {
  $op0 = $ln[ 0]
  $op1 = $ln[ 1]
  $op2 = $ln[ 2]
  $op3 = $ln[ 3]
  $output = "$op0;$op1;$op2;$op3"
  Write-Output $output | Out-File -path "D:\scripts\Accountlists\User.csv" -append
}

Thanks in advance!

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
25 Sep 2008 02:45 PM  
If I follow, you want a list of AD users (user ou and user cn) and export it to a csv file? Am I right?
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
OxnardUser is Offline
New Member
New Member
Posts:3
Avatar

--
25 Sep 2008 02:51 PM  
Yes, you 're right. But I need that list in a special style.
Lastname;Name;Account Enabled;OU
ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
25 Sep 2008 03:53 PM  
With Quest AD cmdlests you can simply do:


Get-QADUser -sizeLimit 0 | foreach {
"{0};{1};{2};{3}" -f $_.sn,$_.firstName,$_.accountIsDisabled,$_.ParentContainerDN
} | Out-File -path D:\scripts\Accountlists\User.csv -append


You can download the cmdlets for free at:

http://www.quest.com/powershell/activeroles-server.aspx

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer