开发者

Logical drive info

开发者 https://www.devze.com 2023-03-12 12:20 出处:网络
I have a script which has a declaration of $outputs=@() & into which a set of properties get added, here is an out out for the same, im getting these added using $output | Add-Member -MemberType

I have a script which has a declaration of $outputs=@()

& into which a set of properties get added, here is an out out for the same, im getting these added using $output | Add-Member -MemberType Noteproperty -Name "hostname" -Value "$hostname", similarly for others properties too. im doing an $outputs+=$output to add the appended values,

hostname : SEA开发者_开发知识库PS01 date : 06/14/2011 02:06:36 ApplicationsInstalled : Microsoft SQL Server 2008 Client Tools

but now the problem is that when i add hard disk info to this it gets owerwritten & i recieve an error as the note property already exists, could you please help me out on how to go about & append the HDD properties to the $outputs object

$colItems = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computername $compname

$drivecount=(get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" | Select-Object drivetype | Measure-Object).count




foreach ($objItem in $colItems){
for($j=0;$j-lt$drivecount;$j++){
if ($objItem.DriveType -eq 3){
    # Write to screen
    #write-host "Drive Type: " $objItem.DriveType
    $drivename=$objItem.Name



    # Improve the display of the higher order values of MB and GB 
    $displayMB = [math]::round($objItem.Size/1024/1024, 2)
    $displayGB = [math]::round($objItem.Size/1024/1024/1024, 2)

    $strOutPut57 = $displayGB

    # Improve the display of the higher order values of MB and GB 
    $displayMB = [math]::round($objItem.FreeSpace/1024/1024, 2)
    $displayGB = [math]::round($objItem.FreeSpace/1024/1024/1024, 2)

    $strOutPut58 = $objItem.Name +"\ "+ $displayGB
    $strOutPut59 = $objItem.FileSystem

$output | Add-Member -MemberType Noteproperty -Name DriveName[$j] -Value "$drivename"
$output | Add-Member -MemberType Noteproperty -Name FilesystemType[$j] -Value "$strOutPut59"
$output | Add-Member -MemberType Noteproperty -Name Size[$j] -Value "$strOutPut57"
$output | Add-Member -MemberType Noteproperty -Name FreeSpace[$j] -Value "$strOutPut58"
}




}
}


Add-member is not necessary in this case, you can manupulate the output with Select-Object and custom properties

Get-WmiObject Win32_LogicalDisk -Filter 'drivetype=3' | Select-Object Name,FileSystem,VolumeName,@{Name='FreeSpace(GB)';Expression={$_.FreeSpace/1GB}},@{Name='Size(GB)';Expression={$_.Size/1GB}}

Your example code also be simplified to this (no need to reassign to $outputs and the result is streaming, objects come out of the pipeline once it's created)

Get-WmiObject Win32_LogicalDisk -Filter 'drivetype=3' | Foreach-Object{
    New-Object PSObject -Property @{
         Name         = $_.caption
         FileSystem   = $_.filesystem
         FreeSpace    = $_.freespace/1GB
         Size         = $_.size/1GB
         VolumeName   = $_.volumename
    }
}


Are you not seeing errors with your code?

Check this:

$outputs = @()
$csvs = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -Filter 'drivetype=3'
foreach ( $csv in $csvs ) {
 $output1 = New-Object PSObject -Property @{
     Name         = $csv.caption
     FileSystem   = $csv.filesystem
     FreeSpace    = ($csv.freespace)/1GB
     Size         = ($csv.size)/1GB
     VolumeName   = $csv.volumename

  }
  $outputs+=$output1
}


Get-WmiObject Win32_LogicalDisk -ComputerName s856ab3 -Filter 'drivetype=3' | Select-Object Name,FileSystem,VolumeName,@{Name='FreeSpace(GB)';Expression={$.FreeSpace/1GB}},@{Name='Size(GB)';Expression={$.Size/1GB}}

Wow... this is the nicest way I've seen this done. All other ways I've seen it done are unnecessarily complex. Thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消