Solved

Powershell Output help

Posted on 2015-01-03
6
132 Views
Last Modified: 2015-01-08
I have wriiten the following SQL script to execute using powershell.

 
  cls
foreach ($svr in get-content "demo.txt")
{
  $con = "server=MC1-PQ10X.RF.LILLY.COM\SQL01;database=mylilly_WSS_Content_INCTSK0014840;Integrated Security=sspi" 

  $cmd = "SELECT
Docs.DirName + '/' + Docs.LeafName AS 'Item Name',
DocVersions.UIVersion, 
(DocVersions.UIVersion/512) as Version_Label, DocVersions.Level, DocVersions.TimeCreated
FROM DocVersions FULL OUTER JOIN Docs ON Docs.Id = DocVersions.Id
--   INNER JOIN Webs On Docs.WebId = Webs.Id
--INNER JOIN Sites ON Webs.SiteId = SItes.Id
WHERE (DirName LIKE '%globalcontentrepository%')
AND (IsCurrentVersion = '0')
AND (DocVersions.Id IN ('$svr'))"

  $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)

  $dt = new-object System.Data.DataTable

  $da.fill($dt) |out-null

  $dt | Export-Csv music.csv -Encoding ascii -NoTypeInformation

 }

Open in new window


The problem I'm facing with the above code is regarding the output. For every $svr, this code is creating a new csv file. The input file is containing around 1000 inputs. My requirement is that all the output should get stored in the csv file at once rather than creating new file.

Please look into this. Kindly give your suggestions.
Thanks in advance
0
Comment
Question by:Anisha Singh
  • 3
  • 3
6 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40529959
There are two ways to do that.
Change lines 23-25 to
  $data += @($dt) 
)
$data | Export-Csv music.csv -Encoding ascii -NoType

Open in new window

or embed the foreach in a subexpression and export the pipeline:
$(foreach # as in your code... 
  $dt
)) | Export-Csv music.csv -Encoding ascii -NoType

Open in new window

0
 

Author Comment

by:Anisha Singh
ID: 40530011
Hello Qlemo,

Thanks for your inputs.
It's working perfectly. I need one more help. Actually the output that I'm getting is more than 2 Lakhs. I want to open this output in excel for auditing.
Is there anyway I can break the output and create different excel files, when a certain limit is reached.
Kindly guide me in this matter.
Thanks again
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40530022
That needs some effort. We can limit on number of lines.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:Anisha Singh
ID: 40530030
Hello Qlemo,

Thanks for replying again.

No problem, I'll figure out something.

Can you please tell me when to clear the data variable after the execution is done?
Since for some input id's there are no entries in the table I'm querying and I want to make sure the data table is cleared everytime I provide this a fresh set of input id's.

Please look into this.

Thanks and Regards,
Abhinav Singh
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 40530050
$data needs to get cleared before foreach, e.g. by assigning an empty array:
 $data = @()
You do not need that if you call the script again for each set of IDs, because the scope of vars is restricted to the script or function they are first defined in by default.
However, I cannot see how you would use this script for different ID sets. You would have to change the input file. But (#2) that is not part of this question anyway.

Here is how to split output into different files based on lines contained. $maxlines is the limit used, here 100 lines
$maxlines = 100
for ($offs = 0; $offs -lt $data.Count; $offs += $maxlines) {
  $data[$offs..($offs+maxlines-1)] | convertTo-CSV -NoType -Encoding ASCII ('music {0:D2}.csv' -f $offs/$maxlines+1)
}

Open in new window

0
 

Author Closing Comment

by:Anisha Singh
ID: 40538584
Elegant and Perfect Solution
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now