Page 1 of 1

New-Object for Data Collection ?

Posted: Wed Apr 09, 2008 6:29 am
by socaldavel
I'm trying to get my head around the concept of creating a "new-object" and am curious to see if I'm on the right track... or way off base.Similar to the other thread about "asset inventory", I have a script where I'm querying a number of remote systems for information (OS version, BuildDate, ServicePack, etc - all of which I'm querying via WMI). I'd ultimately like to take each of those variables, which I'm querying for each server in a foreach loop and export it into a csv file so that I can pull a report when done.Am I right in thinking that I should be able to create a new-object for each server with properties for each variable I'm creating which can then be displayed as a table (formet-table) or exported to a csv (preferrably appended to an existing csv so I can build upon it)?And if so, how?From what I keep finding about new-objects it seems that I can only select from existing objects (of which I can't seem to find a master list). Am I way off base here? Someone please just point me in the right direction.Thanks much!!
socaldavel2008-04-09 13:31:16

New-Object for Data Collection ?

Posted: Wed Apr 09, 2008 6:44 am
by socaldavel
whew... glad I'm not completely mad, just still wet behind the ears.Thanks Don (I'm assuming the infamous Don Jones who's always mentioned on the Powerscripting Podcast) :)I'll get started on this snippet.... and look for the book online too!!

New-Object for Data Collection ?

Posted: Wed Apr 09, 2008 6:49 am
by donj
This is an example I use in our Advanced training course which does more or less what you're looking for. You can see at the end that it uses the Get-DriveInventory output - which are objects - in three different ways, which is the whole reason to spew objects rather than text. I created a second function which takes those objects and writes them to a database - that keeps each function very single-purposed, so that they can be recombined for more flexibility.

Code: Select all

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

function Get-DriveInventory {
     PROCESS {
          #get drives from WMI
          $drives = gwmi win32_logicaldisk -comp $_ -filter "drivetype=3"
          
          #construct output objects
          foreach ($drive in $drives) {
               $obj = New-Object psobject
               $obj | Add-Member NoteProperty ComputerName $_
               $obj | Add-Member NoteProperty DriveLetter $drive.deviceid
               $free = $drive.freespace/1MB -as [int]
               $obj | Add-Member NoteProperty AvailableSpace $free
               $total = $drive.size/1MB -as [int]
               $obj | Add-Member NoteProperty TotalSpace $total
               
               #write output object
               Write-Output $obj               
          }
     }
}

function Write-DriveInventory {
     BEGIN {
          #open database connection
          $conn = New-Object System.Data.OleDb.OleDbConnection
          #will need to adjust the path
          $connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:DriveInventory.accdb;Persist Security Info=False;"
          $conn.connectionstring = $connstr
          $conn.open()
          
          #create database command
          $cmd = New-Object system.Data.OleDb.OleDbCommand
          $cmd.connection = $conn
     }
     PROCESS {
          #construct SQL statement using input objects' properties
          $now = Get-Date -form g
          $sql = "INSERT INTO DriveInventory (ComputerName,DriveLetter,AvailableMB,TotalMB,LastUpdated) VALUES ("
          $sql += "'" + $_.ComputerName + "',"
          $sql += "'" + $_.DriveLetter + "',"
          $sql += "'" + $_.AvailableSpace + "',"
          $sql += "'" + $_.TotalSpace + "',"
          $sql += "'" + $now + "')"
          $cmd.commandtext = $sql
          $cmd.executenonquery() | Out-Null
     }
     END {
          $conn.close()
     }
}

function Get-FileName {
     $computer = Read-Host "Filename of computer names?"
     return $computer
}

# get the filename
$f = Get-FileName

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

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

# Use this command-line for task #3
Get-Content $f | Get-DriveInventory | Format-Table ComputerName,DriveLetter,AvailableSpace,TotalSpace,@{Label="PercentFree";Expression={$_.AvailableSpace/$_.TotalSpace*100}}

New-Object for Data Collection ?

Posted: Thu Apr 10, 2008 1:06 am
by donj
Ya, I love those guys. I'm so glad someone's doing the podcast - I just couldn't make the time to keep it regular. Hal's the new Community Director at www.PowerShellCommunity.org, too, which is great for him - I hope the podcast'll help bring more folks over there.