Solved

Powershell Output help

Posted on 2015-01-03
6
131 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

15 Experts available now in Live!

Get 1:1 Help Now