Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

SQL Query results to PowerShell Array.

$
0
0

Hi,

First time I've posted on here so hi. I'm pretty new to SQL so sorry if this looks a little naff.

I'm trying to query two tables. One has a username, associated with an ID, the other has a list of files found for that user, with information about those files, and it uses the ID in the other table to associate it to that user.

So what I need to do, is get the sum of sizes of each file, then use the ID's to associate them, and output the Alias and SUM. I've pretty much got that working.

$importedusers = Import-CSV c:\temp\ListOfUsers.csv              

$pausers = @()

foreach ($importeduser in $importedusers)

{

$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=ServerName\Database;Integrated Security=SSPI;Initial Catalog=Database");

$conn.Open()

#This get's both tables and puts the sum of the InitialDataSize into a Size variable

$sql = "select SUM(InitialDataSize)/1024/1024 as Size FROM [PSTEnterprise].[dbo].[PSTFile],[PSTEnterprise].[dbo].[PSTMailbox] where [PSTEnterprise].[dbo].[PSTMailbox].ID = [PSTEnterprise].[dbo].[PSTFile].PSTMailbox_id and Attribute1 = '"+$importeduser.Alias+"'"

$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)

$rdr = $cmd.ExecuteReader()

while($rdr.Read())

{

Write-Output $importedUser.Alias,$rdr["Size"]

}

$conn.Close()

}

 

This basically outputs the correct data like this

Alias

Size

Alias

Sum

 

What I need to do is get the results into an array, and add headings to the array so I can then do what I want with it in Powershell. I.e. write to CSV, or use it to allocate space based on the figures.

Two questions are, have I done the query in the right way or is there a better way, and any idea how to write out to an array instead of host.

Thanks in advance for any assistance.


Viewing all articles
Browse latest Browse all 6937

Trending Articles