Solved

Powershell Output help

Posted on 2015-01-03
6
134 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 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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