Page 1 of 1

Tips for output for WMI query

Posted: Thu May 08, 2008 10:03 pm
by fizban2
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!

Tips for output for WMI query

Posted: Thu May 08, 2008 10:03 pm
by fizban2
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!

Tips for output for WMI query

Posted: Thu May 08, 2008 11:51 pm
by donj

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.