Powershell append to array

ACDBA 416 Reputation points
2022-12-26T01:23:29.3+00:00

Hi All,

Hope everyone is having holiday time.

I am trying to get the disk and lun details of two serves using the script.

Server A has 5 drives and server B has 4. So i expect an output of 9 rows. 5 disks from Server A and 4 from B.

But my output is showing wrong results. Eg: There is no W drive for Server B. But its present in output. Can you please advise where did my append in array for wrong.

273956-capture.jpg

   $vm="server1","server2"  
   $array1 =@()  
     
foreach ($v in $vm)  
{  
        
          $Win32_LogicalDisk = Get-WmiObject -Class Win32_LogicalDisk -computername $vm | Where-Object {($_.DeviceID -ne 'C:') -and ($_.DeviceID -ne 'T:') }  
           
          $Win32_LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition  -computername $vm  
          $Win32_DiskDriveToDiskPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -computername $vm  
          $Win32_DiskDrive = Get-WmiObject -Class Win32_DiskDrive -computername $vm  
     
          # Search the SCSI Lun Unit for the disk  
           $p= @{  
                        Computer = ""  
                        DeviceID = ""  
                        SCSIBus = ""  
                        SCSIPort = ""  
                        SCSITargetId = ""  
                        SCSILogicalUnit = ""  
                        Size=""  
                        FreeSpace=""}  
     
           
                        
          $Win32_LogicalDisk |  
            ForEach-Object {  
              if ($_)  
              {  
                $LogicalDisk = $_  
                $LogicalDiskToPartition = $Win32_LogicalDiskToPartition |  
                  Where-Object {$_.Dependent -eq $LogicalDisk.Path}  
                if ($LogicalDiskToPartition)  
                {  
                  $DiskDriveToDiskPartition = $Win32_DiskDriveToDiskPartition |  
                    Where-Object {$_.Dependent -eq $LogicalDiskToPartition.Antecedent}  
                  if ($DiskDriveToDiskPartition)  
                  {  
                    $DiskDrive = $Win32_DiskDrive |  
                      Where-Object {$_.__Path -eq $DiskDriveToDiskPartition.Antecedent}  
                    if ($DiskDrive)  
                    {  
                        
                        
                       
                        $p.Computer = $v  
                        $p.DeviceID = $LogicalDisk.DeviceID  
                        $p.SCSIBus = $DiskDrive.SCSIBus  
                        $p.SCSIPort = $DiskDrive.SCSIsPort  
                        $p.SCSITargetId = $DiskDrive.SCSITargetId  
                        $p.SCSILogicalUnit = $DiskDrive.SCSILogicalUnit  
                        $p.Size=[Math]::Round($DiskDrive.size / 1GB, 2)  
                        $p.FreeSpace=[Math]::Round($LogicalDisk.FreeSpace / 1GB, 2)  
                       
                        $array1 += [PSCustomObject]$p  
      
                            
                    }  
                       
                  }  
                }  
              }  
                
            }  
              
                 
  
    
  }  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,766 Reputation points
    2022-12-26T02:54:54.263+00:00

    It looks to me that these are your problem:

    $Win32_LogicalDisk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $vm | Where-Object { ($_.DeviceID -ne 'C:') -and ($_.DeviceID -ne 'T:') }  
    $Win32_LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition  -ComputerName $vm  
    $Win32_DiskDriveToDiskPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $vm  
    $Win32_DiskDrive = Get-WmiObject -Class Win32_DiskDrive -ComputerName $vm  
    

    You should be using $V, not $VM as the value of the -ComputerName parameter.

    You're running the outer "ForEach" twice (once for each machine name in the $vm list), but each time you're getting the information from both machines.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.