2

我有一个函数可以遍历计算机上的所有 HDD,并返回有关这些驱动器及其映射到阵列中物理驱动器的信息。

我希望这个函数在自定义对象中返回信息。

这是功能:

##--------------------------------------------------------------------------
##  FUNCTION.......:  Get-HDDInfo
##  PURPOSE........:  
##  REQUIREMENTS...:  
##  NOTES..........:  
##--------------------------------------------------------------------------
Function Get-HDDInfo {
    [CmdletBinding()]
        Param([Parameter(Mandatory = $True,
            ValueFromPipeLine = $True,
            Position = 0)]
            [String[]]$ComputerName
        )#END: Param
    $W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)
    $Array = @()

    $W32_DD | foreach { 
        $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
        + $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition" 
        $Array += $_.Name
        $Array += $_.Model
        <#
        $obj = New-Object PSObject
        $obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
        $obj | Add-Member -MemberType NoteProperty -Name `
            "PDCaption" -Value $_.Name
        $obj | Add-Member -MemberType NoteProperty -Name `
            "PDModel" -Value $_.Model
        $Array += $obj
        #>
        Get-WmiObject -Query $query | foreach { 

            $Array += $_.Name
            $Array += $_.Description 
            $Array += $_.PrimaryPartition

            #$obj = New-Object PSObject
            <#
            $obj.PSObject.typenames.insert(0,'JoeIT.Custom.SystemInfo')
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPName" -Value $_.Name
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPDescription" -Value $_.Description
            $obj | Add-Member -MemberType NoteProperty -Name `
                        "DPPrimary" -Value $_.PrimaryPartition
            #>
            $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
            + $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk" 

            Get-WmiObject -Query $query2 | ForEach {
            $Array+= $_.Name
            $Used = [math]::round($_.Size/1024/1024/1024,0)
            $Free = [math]::round($_.FreeSpace/1024/1024/1024,0)
            $Array += [String]$Used +"GB"
            $Array += [String]$Free +"GB"
            #Return $Array;
            #$Array = $Null
            }

            <#
            $Array += $obj
            $obj = $Null
            #>
        }#END: Get-WmiObject -Query
    }#END: $W32_DD | foreach
    ##----------------------------------------------------------------------
    ##  Store results in custom Object
    ##----------------------------------------------------------------------
    Return $Array
}#END: Function Get-HDDInfo

被注释掉的东西来自我试图将信息放入自定义对象的尝试。也许我只是有点筋疲力尽,但我似乎无法正确完成这项工作。如您所见,注释掉的代码试图覆盖命名属性 - 我在编写它时就知道,但出于某种原因,我希望它无论如何都能工作;)

也许我不应该在没有休息日的情况下工作三周,但我的大脑就是不让我解决这个问题。

我想要的是能够做这样的事情:

$test = (get-hddinfo $SVR01)
$test.PhysicalDrive1
$test.Partition1
$test.DriveLetter1
$test.TotalSize1
$test.FreeSpace1

这将查询名为 SVR01 的计算机,并写出第一个物理 HDD、该驱动器的第一个逻辑分区、分配的驱动器号、磁盘的总大小以及磁盘上的可用空间。

然后我可以做类似的事情

$test.PhysicalDrive2
$(same code here for the second physical drive)

我到底做错了什么?

4

2 回答 2

2

尝试这个:

[CmdletBinding()]
        Param([Parameter(Mandatory = $True,
            ValueFromPipeLine = $True,
            Position = 0)]
            [String[]]$ComputerName
        )
    $W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName)

    $a = new-object  System.Object
    $sc3 = 1
    $sc2 = 1
    $sc1 = 1
    $W32_DD | foreach { 
        $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" `
        + $_.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition" 

        $a | Add-Member -type NoteProperty -name DiskDriveName$sc1 -value $_.Name
        $a | Add-Member -type NoteProperty -name DiskDriveModel$sc1 -value $_.Model       

        Get-WmiObject -Query $query | foreach { 

            $a | Add-Member -type NoteProperty -name PartitionName$sc2 -value $_.Name
            $a | Add-Member -type NoteProperty -name PartitionDescription$sc2 -value $_.Description
            $a | Add-Member -type NoteProperty -name PrimaryPartition$sc2 -value $_.PrimaryPartition

            $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" `
            + $_.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk" 


            Get-WmiObject -Query $query2 | ForEach {

            $a | Add-Member -type NoteProperty -name LogicalDiskName$sc3 -value $_.Name           

            $Used = [math]::round($_.Size/1024/1024/1024,0)
            $Free = [math]::round($_.FreeSpace/1024/1024/1024,0)

            $a | Add-Member -type NoteProperty -name UsedSpace$sc3 -value $([String]$Used +"GB")
            $a | Add-Member -type NoteProperty -name FreeSpace$sc3 -value $([String]$Free +"GB")

            $sc3++
            }
           $sc2++
          }       
       $sc1++
    }

    Return $a
于 2012-06-27T05:41:15.990 回答
1

这是一种方法,它不完全是您想要的,但它为您提供了一种方法:

##-------------------------------------------------------------------------- 
##  FUNCTION.......:  Get-HDDInfo 
##  PURPOSE........:   
##  REQUIREMENTS...:   
##  NOTES..........:   
##-------------------------------------------------------------------------- 
Function Get-HDDInfo
{ 
  [CmdletBinding()] 
  Param([Parameter(Mandatory = $True, ValueFromPipeLine = $True, Position = 0)] 
        [String[]]$ComputerName)#END: Param 



  $W32_DD = @(gwmi Win32_DiskDrive -ComputerName $ComputerName) 
  $ArrayofPD = @() 

  foreach ($dd in $W32_DD)
  {  
    $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + $dd.DeviceID + "'} WHERE ResultClass=Win32_DiskPartition"  

    # create a new physical disc object
    $PDobj = New-Object PSObject 
    $PDobj | Add-Member -MemberType NoteProperty -Name "PDCaption" -Value $dd.Name 
    $PDobj | Add-Member -MemberType NoteProperty -Name "PDModel" -Value $dd.Model 

    $ArrayofLD = @()
    $diskParts = Get-WmiObject -Query $query
    foreach ($diskPart in $diskParts)
    {  
      # create a new logical disc object
      $LDobj = New-Object PSObject 
      $LDobj | Add-Member -MemberType NoteProperty -Name "DPName" -Value $diskPart.Name 
      $LDobj | Add-Member -MemberType NoteProperty -Name "DPDescription" -Value $diskPart.Description 
      $LDobj | Add-Member -MemberType NoteProperty -Name "DPPrimary" -Value $diskPart.PrimaryPartition 

      $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + $diskPart.DeviceID + "'} WHERE ResultClass=Win32_LogicalDisk"  

      $LogicalDisk = Get-WmiObject -Query $query2
      if ($LogicalDisk -ne $null)
      {
        $LDobj | Add-Member -MemberType NoteProperty -Name "LGName" -Value $LogicalDisk.Name
        $Used = [math]::round($LogicalDisk.Size/1024/1024/1024,0) 
        $Free = [math]::round($LogicalDisk.FreeSpace/1024/1024/1024,0) 
        $LDobj | Add-Member -MemberType NoteProperty -Name "UsedSpace" -Value $([String]$Used +"GB")
        $LDobj | Add-Member -MemberType NoteProperty -Name "FreeSpace" -Value $([String]$Free +"GB")
      } 
      $ArrayofLD += $LDobj
    }
    $PDobj | Add-Member -MemberType NoteProperty -Name "LogicalDisks" -Value $ArrayofLD 

    $ArrayofPD += $PDobj
  }

  ##---------------------------------------------------------------------- 
  ##  Store results in custom Object 
  ##---------------------------------------------------------------------- 
  Return $ArrayofPD 
}#END: Function Get-HDDInfo 

Clear-Host
$a = Get-HDDInfo localhost
$a

点源它为我提供的功能:

PS C:\Users\JPB\Documents> $a = Get-HDDInfo localhost
PS C:\Users\JPB\Documents> $a

PDCaption                                     PDModel                                       LogicalDisks                                 
---------                                     -------                                       ------------                                 
\\.\PHYSICALDRIVE0                            ST9500420AS                                   {@{DPName=Disque n° 0, partition n° 0; DPD...
\\.\PHYSICALDRIVE1                            ST932042 3AS USB Device                       {@{DPName=Disque n° 1, partition n° 0; DPD...

和 :

PS C:\Users\JPB\Documents> $a[0].LogicalDisks

DPName                                        DPDescription                                                                     DPPrimary
------                                        -------------                                                                     ---------
Disque n° 0, partition n° 0                   Système de fichiers installable                                                        True
Disque n° 0, partition n° 1                   Système de fichiers installable                                                        True
于 2012-06-27T05:09:57.847 回答