Solved

Powershell Output help

Posted on 2015-01-03
6
135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 69

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 69

Expert Comment

by:Qlemo
ID: 40530022
That needs some effort. We can limit on number of lines.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 69

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linked Server - SP with Param to VIew 7 25
query all mailbox rules 5 33
Powershell PGP and SFTP 1 18
T-SQL: I need to add an index on a field 5 27
Synchronize a new Active Directory domain with an existing Office 365 tenant
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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