逻辑驱动器信息

时间:2011-06-14 04:44:01

标签: powershell

我有一个脚本,其声明为$ outputs = @()

&安培;添加了一组属性,这里是一个out out,我使用$ output |添加这些属性Add-Member -MemberType Noteproperty -Name“hostname”-Value“$ hostname”,类似于其他属性。我正在做$ outputs + = $ output来添加附加值,

主机名:SEAPS01 日期:06/14/2011 02:06:36 ApplicationsInstalled:Microsoft SQL Server 2008客户端工具

但现在的问题是,当我为此添加硬盘信息时,它会被写入&我收到一个错误,因为note属性已经存在,请你帮我解决一下如何去做&将HDD属性附加到$ outputs对象

$ 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"
}




}
}

3 个答案:

答案 0 :(得分:2)

在这种情况下不需要添加成员,您可以使用Select-Object和自定义属性对输出进行操作

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

您的示例代码也被简化为此(无需重新分配给$ outputs,结果是流式传输,对象在创建后从管道中出来)

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
    }
}

答案 1 :(得分:0)

您是否发现代码错误?

检查一下:

$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
}

答案 2 :(得分:0)

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

哇......这是我见过这个最好的方式。我所看到的所有其他方式都是不必要的复杂。谢谢!