HTML report for File Share info - against several servers?

Hello,

I continue learning a great deal on here, I am trying to create a report I thought would be easier. But I’m getting stuck trying to figure out how to write a report that would:

A. Gather the quotas setup on file servers that run Windows 2003 and 2008R2

B. Provide an HTML or Excel report with the data. Given it’s multiple servers, I’m thinking excel might be better, perhaps 1 sheet per server?

C. Email the sheet when a scheduled task runs weekly.

I’ve been working on this for a little while now and can’t seem to get to where I need at all, which is frustrating. I know the command I need to run is: dirquota quota list

I think I need to start with a foreach loop that will accept either 1 server name or get-content where I put in the path of a text file with the server names. Then store the properties I need in an array or variable? After that I think it’s exporting the data to excel, and ideally the “sheet” in excel would be named the name of the server, and then the email portion would come in last?

Then I need these properties:

Quota Path: D:\Share\Marketing
Source Template: 300 MB Limit (Does not match template)
Quota Status: Enabled
Limit: 50.00 MB (Hard)
Used: 1.00 KB (0%)
Available: 50.00 MB

If anyone can help it would be great!

Have you considered looking at powershell.org/wp/ebooks, where we have a free book on creating HTML reports? I don’t believe Excel is a good reporting tool, personally, but HTML can work well. Excel is also a pain to automate from within PowerShell (in my opinion), whereas HTML is very easy to produce. The free ebook has a number of examples, and you can start very simple and work up to quite complex formatting and display.

Thanks Don! I have that Ebook and I’m trying to get this working. I also bought your last book series and dvds but it’s trying to learn while working as I’ve never been a programmer/scripter. of course trying to learn while meeting the requests that are non-stop makes it harder as I’m going through the materials on my time and learning the right way, in the meantime I’m using the online resources to try to get these things done.

Right now I’m stuck on just running the for-each loop for the command. Then when I get that working I’m not sure how to convert it to the html format, and finally email it.

If I can do the for-each loop, then how would you handle sending out a report if it’s for multiple file servers? Or should I just create the script and schedule it to run against a single server as a scheduled task, then schedule it to run for all of the others? That doesn’t seem right, but otherwise the html report would be quite long as it’s for 10 servers and many quotas are enabled per server.

How would you handle it?
I read a post about creating HTML reports here:

The part I get stuck on is how to get the data into the format, then send the email. I can figure out the loop I think, but it’s the rest that I get lost on.

Thanks as always,
Jake

In pseudo code, assuming $computers contained one or more computer names, I’d so something like this:

foreach ($computer in $computers) {

make up a filename based on the computer name

retrieve whatever information from the computer

produce the HTML report for the computer

}

In terms of “getting the data in the format,” you have to get the data into an object. The HTML book offers several examples. I usually do this by writing a short function:

Get-Data { param($computername) $rawdata = Get-Something -Computername $computername $raw2 = Get-SomethingElse -Computername $computername $properties = @{'ComputerName'=$computername; 'OneThing'=$rawdata.someproperty; 'Another'=$raw2.anotherproperty} New-Object -Type PSObject -Prop $properties }

That combines information from two commands into a single output object. You then convert that to an HTML fragment:

foreach ($computer in $computers) { $filename = "report-$($computer).html" $frag1 = Get-Data -Computername $computer | ConvertTo-HTML -Fragment -As Table -Pre "

Data 1

" | Out-String $frag2 = Get-OtherData -Computername $computer | ConvertTo-HTML -Fragment -As List -Pre "

More Info

" | Out-String ConvertTo-HTML -Title "For $computer" -Body "

Report for $computer

",$frag1,$frag2 | Out-File $filename } </code

That assumes I want two sections in my report, one created by running the Get-Data function and another the Get-Otherdata function. Both functions might simply be written at the start of the same script file.

So:

  1. One short function per section of the report
  2. Each function outputs a PSObject having the properties you want in the report
  3. Each function is converted to an HTML fragment
  4. The fragments are combined into a single HTML file
  5. You’d put Send-MailMessage INSIDE the ForEach loop to send one email per computer; AFTER the loop if you want to attach all the files to a single email.

That’s the same basic approach the HTML book takes. But in terms of “should I do one report per server or one report with all the servers,” I can’t really tell you that. It’s whichever you want. If you want one report for all servers, then you’d probably have to save up the fragments in an array, and combine them AFTER the ForEach.

Just think about it - whatever is INSIDE the ForEach will happen FOR EACH computer. If you don’t want a report FOR EACH computer, then that doesn’t go inside the loop.