2012-06-26 30 views
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) 
    $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電腦,寫出來的第一個物理硬盤,該硬盤的第一個邏輯分區,分配的驅動器號,磁盤總大小以及磁盤上的可用空間。

然後我可以做這樣的事情

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

到底什麼是我做錯了什麼?

回答

1

試試這個:

[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 
+0

真棒,謝謝你! – JoeG

+0

好吧,現在我已經測試了它,這不會返回對象中遠程計算機上的正確信息。 奇怪的是,我不知道它從哪裏獲取信息。它爲DiskDriveName,DiskDriveModel返回正確的信息,但在此之後崩潰,它看起來像從本地機器獲取信息。 我的GWMI查詢語法是錯誤的還是什麼? – JoeG

+0

@JoeG是的,它可以,我沒有測試它,但也嵌套'get-wmiobject'需要'-computername'參數... –

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 
+0

謝謝,這可以讓你的工作輕鬆完成。 – JoeG

相關問題