formatting - Create Table with Variables in PowerShell -


I still am learning my way around PowerShell scripting and I am working on my script so that my free place percentage Send an e-mail notification to the file server and when a drive is 10% empty or less (this happens once in a month, and I should know before receiving e-mail from the customer that there is no other place) . So far the script works great and is set to run every morning through Windows Tasks. But the current formatting for my output is done manually. I was wondering if there was a way to collect the information from the calculation made with the collected and received-WMIObject function, I have tried the format table and tried to mess with the hash tables, but no one Not benefited Any ideas would be helpful. Thank you.

  Set # parameter $ file = "c: \ location \ lowerdisk.txt" explicit-content $ file $ emailTO = "Someone@omome.com.com" $ emailFrom = "any @ SomeDomain .com "$ smtpServer =" smtpServer "$ diskspace =" 3 "$ computer = (" FSCN01 "," FSCN02 "," FSCN03 "," FSCN04 ")" Server name drive drive size free space is free " Gt; Echo; $ File $ i = 0 # Get the drive data foreach ($ computer $ computer) {$ drives = Get-WMIObject -ComputerName $ computer Win32_LogicalDisk | Where the object {$ _ DriveType -q3} foreach ($ drive in drive $) {$ ID = $ drive.DeviceID $ size1 = $ drive.size / 1GB $ size = "{0: N1}" -f $ size 1 $ free 1 = $ Drive.freespace / 1GB $ free = "{0: N1}" -f $ free 1 $ a = $ free1 / $ size1 * 100 $ b = "{0: N1}" -f $ a # Monitor less than 10% For drive free space% ($ b -lt 10) {echo "$ computer $ id $ size $ free $ b" & gt; & Gt; $ File $ i ++}}} Notice if the script finds more than 0 drives with less than 35% of space ($ i -gt 0) {foreach ($ user $ in $ email $) {echo "email Notification $ user is sending "$ Smtp = New Object Net.mail SMTP Client ($ smtpServer) $ theme =" server with low disk space "Forex currency ($ line in $ Get-content $ file) {$ body + = "$ Line` n "} Send -mail message- $ for user- $ email-attachments $ file - SMTP server $ smtp all - subject $ theme - body $ body $ body = ""}}  

Format-table works best when you have all the data of interest in the same type of object, I would recommend creating custom objects for it eg .:

  Foreach ($ computer in $ computer) {$ drives = Get-WMIObject -ComputerName $ computer Win32_LogicalDisk | Where the object {$ _ DriveType -q3} foreach ($ drive in drive $) {$ obj = new-object psobject -Property @ {ComputerName = $ computer drive = $ drive.DeviceID size = $ drive.size / 1GB free = $ drive Freespace / 1GB percent free = $ drive.freespace / $ drive.size * 100} if ($ obj.PercentFree -lt 10) {$ obj | Format-table computername, drive, size, free, percent-free $ i ++}}}  

If you want to change the display format, then in the format table command, you can:

$ obj | Format-table computer name, drive, @ {n = 'size'; E = {'{0: N1}' -f $ _. Size}}, free, percent free

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -