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

Problem outputting to a file.
Last Post 30 Sep 2008 08:49 PM by Shay. 12 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
New Member
New Member
Posts:83

--
03 Sep 2008 01:04 PM  

Hi guys, another day another query,

 

I have created the script below to validate some details of some user accounts.  The script runs fine unless I output to a file:

 

ForEach ($user in $users) {get-qaduser "$user" -sizelimit 0 -IncludedProperties altRecipient | select name,altRecipient,accountexpires,pass*,accountisdisabled,lastlog*,canonicalname } | export-csv -path d:\Leaver-Validation.csv

 

If I try to ouput the script to a file PowerShell errors with the following:

An empty pipe element is not permitted.
At line:1 char:186
+ foreach ($user in $users) {get-qaduser "$user" -sizelimit 0 -IncludedProperties altRecipient | select name,altRecipie
nt,accountexpires,pass*,accountisdisabled,lastlog*,canonicalname } |  <<<< export-csv -path d:\Leaver-Validation.csv

 

I'm lost on why this happening, Does anyone have any Ideas?

 

Thanks

 

Lee

 

SynJunkieUser is Offline
New Member
New Member
Posts:83

--
03 Sep 2008 01:11 PM  

Please ignore this. I moved my curly braces to the end and it ran fine.

ForEach ($user in $users) {get-qaduser "$user" -sizelimit 0 -IncludedProperties altRecipient | select name,altRecipient,accountexpires,pass*,accountisdisabled,lastlog*,canonicalname | export-csv -path d:\Leaver-Validation.csv}
SynJunkieUser is Offline
New Member
New Member
Posts:83

--
03 Sep 2008 01:20 PM  
Further to my last post, on inspecting the outputted file, it seems that PowerShell overwrites the outfile for each object, so the script doesn't work as required. Strange?

Regards

Lee
ShayUser is Offline
Basic Member
Basic Member
Posts:214

--
03 Sep 2008 04:40 PM  

Try with the foreach-object cmdlet instaed, it is more pipeline friendly then the foreach statment:

$users | foreach { get-qaduser $_ -IncludedProperties altRecipient | select name,altRecipient,accountexpires,pass*,accountisdisabled,lastlog*,canonicalname} | export-csv -path d:\Leaver-Validation.csv

BTW, the -sizeLimit 0 is not needed, you use it only when you want to get all user objects. In this case you already have the objects in $users.

SynJunkieUser is Offline
New Member
New Member
Posts:83

--
04 Sep 2008 04:13 PM  
once again your suggestion works great. Thanks for the tip on the -sizelimit option as well.

Do you by any chance know of any good resources where I can get an explanation of where to use paranthesis () and curly braces {} . I really need to understand them more than I currently do.

Thanks

Lee
aleksandarUser is Offline
New Member
New Member
Posts:11

--
05 Sep 2008 11:17 AM  
There are so many different cases of usage of () and {}. I think you can't find that kind of an info in just one article. You could try with:

1. official Windows PowerShell Quick Reference
http://www.microsoft.com/downloads/details.aspx?familyid=df8ed469-9007-401c-85e7-46649a32d0e0&displaylang=en

2. Bruce Payette's Free Windows PowerShell Quick Reference Card
http://blogs.msdn.com/powershell/archive/2008/05/30/new-free-windows-powershell-quick-reference-card.aspx

3. Ben Pearce's Cheat Sheet
http://blogs.msdn.com/powershell/archive/2007/01/24/powershell-cheat-sheet.aspx

Hope this helps,
Aleksandar
http://powershellers.blogspot.com
halr9000User is Offline
Basic Member
Basic Member
Posts:303

--
05 Sep 2008 01:49 PM  
Also don't forget the cheatsheet that comes with powershell when you install it. It's called "Quick Reference" and should be in your Windows PowerShell 1.0 start menu folder. If it's not, you can grab it here.
SynJunkieUser is Offline
New Member
New Member
Posts:83

--
15 Sep 2008 01:13 PM  
Thanks guys. that'll do me for now.
slogickUser is Offline
New Member
New Member
Posts:23

--
30 Sep 2008 02:31 PM  

Ok..I am having the same problem...can someone help..below is the code

Import-csv c:\file.csv | foreach {get-qaduser -samaccountname $_.samaccountname | export-csv -path c:\fileout.csv}

 

it is just overwriting each line in the .csv out file.

ShayUser is Offline
Basic Member
Basic Member
Posts:214

--
30 Sep 2008 02:40 PM  
Move the export-csv call to the end of the command:

Import-csv c:\file.csv | foreach {get-qaduser -samaccountname $_.samaccountname} | export-csv -path c:\fileout.csv


slogickUser is Offline
New Member
New Member
Posts:23

--
30 Sep 2008 02:43 PM  

Nevermind, I had my curly brace in the wrong place. lol

slogickUser is Offline
New Member
New Member
Posts:23

--
30 Sep 2008 03:06 PM  

Shay,

one more question...I think I have ad accounts that may not return anything so I want to be able to show my query variable in the first column of the .csv file like this

$_.samaccountname,samaccountname,displayname,givenname,etc...

 

How do I pipe the variable out to the .csv file.

ShayUser is Offline
Basic Member
Basic Member
Posts:214

--
30 Sep 2008 08:49 PM  

If I follow, you want to write a none existent account (piped) samaccount's name to the first coulmn and leave all other propeties null?

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