Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell Output help

Posted on 2015-01-03
6
Medium Priority
?
141 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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

Expert Comment

by:Qlemo
ID: 40530022
That needs some effort. We can limit on number of lines.
0
WEBINAR - Latest Cyber Tips for Defense

Join the WatchGuard Threat Research Team on October 26th for an informative webinar featuring expert tips and tricks for defending your organization from today's latest cyber threats. Don't leave yourself vulnerable to attack. Register for the webinar today!

 

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 71

Assisted Solution

by:Qlemo
Qlemo earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Screencast - Getting to Know the Pipeline

610 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