test3.ps1 powershell code view linux drive

test3.ps1 powershell code view linux drive


# Function to check if a partition is ext4

function Check-Ext4 {

   param (

       [string]$Device

   )

   # Try to read the superblock to determine if it's ext4

   try {

       $output = wsl -e sh -c "file -s $Device"

       if ($output -match "ext4") {

           return $true

       }

   } catch {

       # Handle any errors silently

   }

   return $false

}


# Get available drives

$drives = Get-CimInstance -Query "SELECT * FROM Win32_DiskDrive"


# Display drive information with index numbers

$driveInfo = @()

for ($i = 0; $i -lt $drives.Count; $i++) {

   $drive = $drives[$i]

   $partitions = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($drive.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition"

   $partitionCount = $partitions.Count

   $partitionTypes = @()


   foreach ($partition in $partitions) {

       $logicalDisks = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass=Win32_LogicalDiskToPartition"

       

       if ($logicalDisks) {

           foreach ($logicalDisk in $logicalDisks) {

               $fsType = $logicalDisk.FileSystem

               $driveLetter = $logicalDisk.DeviceID

               $partitionTypes += "$driveLetter ($fsType)"

           }

       } else {

           $partitionTypes += "Unknown"

       }

       

       # Check if the partition is ext4

       $devicePath = "\\.\PHYSICALDRIVE$($partition.DeviceID -replace 'Disk #', '')"

       if (Check-Ext4 $devicePath) {

           $partitionTypes[-1] = "ext4 (Linux)"

       }

   }


   # Create a string to show partition types

   $fsTypeString = ($partitionTypes -join ", ")


   # Append drive info to the list with index number

   $driveInfo += "$($i): Drive \\.\PHYSICALDRIVE$($drive.DeviceID -replace 'Disk #', ''): $($drive.Model), Partitions: $partitionCount, Filesystem Types: $fsTypeString"

}


# Display all drives with index numbers and their partition types

foreach ($info in $driveInfo) {

   if ($info -match "ext4") {

       Write-Host $info -ForegroundColor Red # Highlight ext4 drives in red

   } else {

       Write-Host $info # Default color for other drives

   }

}


# Prompt user to select a drive

$driveIndex = Read-Host "Enter the drive index of the disk you want to mount (e.g., 0)"

$driveToMount = $drives[$driveIndex]


# Mount the selected drive

if ($driveToMount) {

   $drivePath = "\\.\PHYSICALDRIVE$($driveIndex)"

   wsl.exe --mount $drivePath --bare

   Write-Host "Mounted $drivePath in WSL. Check with 'lsblk' in WSL."


   # Check for additional partitions

   $partitions = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($driveToMount.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition"

   if ($partitions.Count -gt 1) {

       for ($j = 1; $j -lt $partitions.Count; $j++) {

           $partitionDevice = "\\.\PHYSICALDRIVE$($driveIndex)$j"

           wsl.exe --mount $partitionDevice --bare

           Write-Host "Mounted additional partition $partitionDevice in WSL."

       }

   }


   # Note for unmounting

   Write-Host "To unmount, run: wsl --unmount $drivePath"

}


Report Page