SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

Custom Objects Example

We’ve long been advocates of outputting objects, rather than text, and of keeping individual functions very single-task-specific. Here’s an ultimate example of these two philosophies:

The first function, Get-DriveInventory, emits custom "drive inventory objects" rather than text. The second, Write-DriveInventory, takes these objects and writes their properties to a pre-designed Access database. However, the three commands at the end show the flexibility: The output of Get-DriveInventory is used to populate a database, exported to a CSV file, and displayed on-screen in table format, along with calculated table columns. By keeping Get-DriveInventory (a) task-focused, and (b) object-oriented, the function can be reused for a number of different end purposes, all with no additional programming. 

 


  1. [reflection.assembly]::loadwithpartialname("System.Data") | Out-Null

  2. function Get-DriveInventory {
  3. PROCESS {
  4. #get drives from WMI
  5. $drives = gwmi win32_logicaldisk -comp $_ -filter "drivetype=3"

  6. #construct output objects
  7. foreach ($drive in $drives) {
  8. $obj = New-Object psobject
  9. $obj | Add-Member NoteProperty ComputerName $_
  10. $obj | Add-Member NoteProperty DriveLetter $drive.deviceid
  11. $free = $drive.freespace/1MB -as [int]
  12. $obj | Add-Member NoteProperty AvailableSpace $free
  13. $total = $drive.size/1MB -as [int]
  14. $obj | Add-Member NoteProperty TotalSpace $total

  15. #write output object
  16. Write-Output $obj
  17. }
  18. }
  19. }

  20. function Write-DriveInventory {
  21. BEGIN {
  22. #open database connection
  23. $conn = New-Object System.Data.OleDb.OleDbConnection
  24. #will need to adjust the path
  25. $connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DriveInventory.accdb;Persist Security Info=False;"
  26. $conn.connectionstring = $connstr
  27. $conn.open()

  28. #create database command
  29. $cmd = New-Object system.Data.OleDb.OleDbCommand
  30. $cmd.connection = $conn
  31. }
  32. PROCESS {
  33. #construct SQL statement using input objects' properties
  34. $now = Get-Date -form g
  35. $sql = "INSERT INTO DriveInventory (ComputerName,DriveLetter,AvailableMB,TotalMB,LastUpdated) VALUES ("
  36. $sql += "'" + $_.ComputerName + "',"
  37. $sql += "'" + $_.DriveLetter + "',"
  38. $sql += "'" + $_.AvailableSpace + "',"
  39. $sql += "'" + $_.TotalSpace + "',"
  40. $sql += "'" + $now + "')"
  41. $cmd.commandtext = $sql
  42. $cmd.executenonquery() | Out-Null
  43. }
  44. END {
  45. $conn.close()
  46. }
  47. }

  48. function Get-FileName {
  49. $computer = Read-Host "Filename of computer names?"
  50. return $computer
  51. }

  52. # get the filename
  53. $f = Get-FileName

  54. # Use this command-line for task #1
  55. Get-Content $f | Get-DriveInventory | Write-DriveInventory

  56. # Use this command-line for task #2
  57. Get-Content $f | Get-DriveInventory | Export-Csv C:\DriveInventory.csv

  58. # Use this command-line for task #3
  59. Get-Content $f | Get-DriveInventory | Format-Table ComputerName,DriveLetter,AvailableSpace,TotalSpace,@{Label="PercentFree";Expression={$_.AvailableSpace/$_.TotalSpace*100}}
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , ,

Leave a Reply


Entries (RSS) and Comments (RSS).