Transpose in powershell

ACDBA 421 Reputation points
2022-12-20T10:56:07.39+00:00

Hi All,

I am using the below script to obtain LUN ID and Disk letter.`

function Get-DiskScsiLun {

[CmdletBinding()]
param([Parameter(Mandatory = $false,
Position = 0)]
[alias("Disk")]
[string] $DeviceID = '*',
[Parameter(Mandatory = $false,
Position = 1,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[alias("CN")]
[String[]] $ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false,
Position = 2)]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty
)

process {
if ($ComputerName)
{
# Loop through all computers in the parameter list
foreach ($Computer in $ComputerName) {
try {
if ($Computer -eq "$($env:COMPUTERNAME)" -or $Computer -eq "." -or $Computer -eq "localhost")
{
# Define the Get-WmiObject parameter set for the local computer
$Parameters = @{
Impersonation = 3
ErrorAction = 'Stop'
}
}
else
{
# Define the Get-WmiObject parameter set for remote computers
$Parameters = @{
ComputerName = $Computer
Credential = $Credential
ErrorAction = 'Stop'
}
}

    # Test if the computer can be connected  
    if (Test-Connection -ComputerName $Computer -Count 1 -Quiet)       
    {           
      # Get the  WMI objects  
      $Win32_LogicalDisk = Get-WmiObject -Class Win32_LogicalDisk @Parameters |  
        Where-Object {$_.DeviceID -like $DeviceID}  
      $Win32_LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition @Parameters  
      $Win32_DiskDriveToDiskPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition @Parameters  
      $Win32_DiskDrive = Get-WmiObject -Class Win32_DiskDrive @Parameters  
 
      # Search the SCSI Lun Unit for the disk  
      $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)  
                {  
                  # Return the results  
                  New-Object -TypeName PSObject -Property @{  
                    Computer = $Computer  
                    DeviceID = $LogicalDisk.DeviceID  
                    SCSIBus = $DiskDrive.SCSIBus  
                    SCSIPort = $DiskDrive.SCSIPort  
                    SCSITargetId = $DiskDrive.SCSITargetId  
                    SCSILogicalUnit = $DiskDrive.SCSILogicalUnit  
                    Size=[Math]::Round($DiskDrive.size / 1GB, 2)  
                    FreeSpace=[Math]::Round($LogicalDisk.FreeSpace / 1GB, 2)  
                      
                  }  
                }  
              }  
            }  
          }  
        }  
      }  
      else  
      {  
        Write-Warning "Unable to connect to computer $Computer."  
      }  
    }  
    catch {  
      Write-Warning "Unable to get disk information for computer $Computer.`n$($_.Exception.Message)"  
    }  
  }  
}  

}
}`

My output will come in below format.

272409-capture.jpg

How do i obtain the results in below format..I think its called transpose.

272456-capture.jpg

Thanks,
ACDBA

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. DaveK 1,871 Reputation points
    2022-12-20T12:41:01.393+00:00

    Hi,

    Can you just format table on your results and if only the 3 headings are needed select them? something like:

    Get-DiskScsiLun | Select Computer,SCSIBus,DeviceID | ft  
    

    or

    $Results = Get-DiskScsiLun  
    $Results | Select Computer,SCSIBus,DeviceID | ft  
    

    I've tried this on my device with a C: and a USB stick (just to give a 2nd drive) and it produces the output format you want. You can export to CSV or GridView if you need them in a spreadsheet - I'm presuming that was to show the format your after rather then needed to export so I didn't include any export in the above

    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.