用于返回自定义对象中的信息的函数

时间:2012-06-26 16:53:42

标签: powershell powershell-v2.0

我有一个函数可以遍历计算机上的所有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)

我到底做错了什么?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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