Tips for output for WMI query

Ask your 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.
This topic is 15 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
fizban2
Posts: 4
Last visit: Fri May 03, 2013 2:29 pm

Tips for output for WMI query

Post 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!
User avatar
fizban2
Posts: 4
Last visit: Fri May 03, 2013 2:29 pm

Tips for output for WMI query

Post 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!
User avatar
donj
Posts: 416
Last visit: Thu May 29, 2008 5:08 am

Tips for output for WMI query

Post 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.
This topic is 15 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked