Tips for output for WMI query

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
fizban2
Posts: 4
Joined: Thu May 08, 2008 10:03 pm

Tips for output for WMI query

Post by fizban2 » Thu May 08, 2008 10:03 pm

hi all,

trying to dive into powershell and make use of all the cool features but running into a snag, trying to figure out a way to output to a text file or HTML, i know there are cmdlets to do this but i cannot figure out how to pipe what i am doing to properly so i can output to the format i want.

Code: Select all

	
$servers=get-content ""
#query each Server in the list
foreach ($s in $servers)
{ 
  
    $computer= $s
    
    $d= get-wmiobject -computer $computer -query "select deviceid,size from win32_logicaldisk where drivetype=3"
	

    
  # Cmdlet to display the size of disk partitions
    
    
        foreach ( $drive in $d ) 
    { 
    $s= $s + "`n" + $drive.DeviceID + [int]($drive.Size/1073741824) + " GB "  
       
    }
    $s= $s + "`n"
    Write-host $s 
    
} 
	
Write-Host "Script Completed"
	

any ideas hit on better formatting or a an easier way output the server name and drives would be appreciated!

User avatar
fizban2
Posts: 4
Joined: Thu May 08, 2008 10:03 pm

Tips for output for WMI query

Post by fizban2 » Thu May 08, 2008 10:03 pm

hi all,

trying to dive into powershell and make use of all the cool features but running into a snag, trying to figure out a way to output to a text file or HTML, i know there are cmdlets to do this but i cannot figure out how to pipe what i am doing to properly so i can output to the format i want.

Code: Select all

	
$servers=get-content ""
#query each Server in the list
foreach ($s in $servers)
{ 
  
    $computer= $s
    
    $d= get-wmiobject -computer $computer -query "select deviceid,size from win32_logicaldisk where drivetype=3"
	

    
  # Cmdlet to display the size of disk partitions
    
    
        foreach ( $drive in $d ) 
    { 
    $s= $s + "`n" + $drive.DeviceID + [int]($drive.Size/1073741824) + " GB "  
       
    }
    $s= $s + "`n"
    Write-host $s 
    
} 
	
Write-Host "Script Completed"
	

any ideas hit on better formatting or a an easier way output the server name and drives would be appreciated!

User avatar
donj
Posts: 416
Joined: Thu Nov 16, 2006 9:02 pm

Tips for output for WMI query

Post by donj » Thu May 08, 2008 11:51 pm

Code: Select all

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               
          }
     }
}
This enables:

Get-Content computers.txt | Get-DriveInventory | Export-CSV filename.csv

For example. The general rule in PowerShell: If you're parsing or creating text, you're working too hard. Work with objects instead. There's a whole chapter on this exact technique in "Windows PowerShell: TFM," if you're interested.

Locked