Advertisement
Guest User

test3.ps1 powershell code view linux drive

a guest
Oct 27th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.18 KB | Source Code | 0 0
  1. # Function to check if a partition is ext4
  2. function Check-Ext4 {
  3.     param (
  4.         [string]$Device
  5.     )
  6.     # Try to read the superblock to determine if it's ext4
  7.     try {
  8.         $output = wsl -e sh -c "file -s $Device"
  9.         if ($output -match "ext4") {
  10.             return $true
  11.         }
  12.     } catch {
  13.         # Handle any errors silently
  14.     }
  15.     return $false
  16. }
  17.  
  18. # Get available drives
  19. $drives = Get-CimInstance -Query "SELECT * FROM Win32_DiskDrive"
  20.  
  21. # Display drive information with index numbers
  22. $driveInfo = @()
  23. for ($i = 0; $i -lt $drives.Count; $i++) {
  24.     $drive = $drives[$i]
  25.     $partitions = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($drive.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
  26.     $partitionCount = $partitions.Count
  27.     $partitionTypes = @()
  28.  
  29.     foreach ($partition in $partitions) {
  30.         $logicalDisks = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($partition.DeviceID)'} WHERE AssocClass=Win32_LogicalDiskToPartition"
  31.        
  32.         if ($logicalDisks) {
  33.             foreach ($logicalDisk in $logicalDisks) {
  34.                 $fsType = $logicalDisk.FileSystem
  35.                 $driveLetter = $logicalDisk.DeviceID
  36.                 $partitionTypes += "$driveLetter ($fsType)"
  37.             }
  38.         } else {
  39.             $partitionTypes += "Unknown"
  40.         }
  41.        
  42.         # Check if the partition is ext4
  43.         $devicePath = "\\.\PHYSICALDRIVE$($partition.DeviceID -replace 'Disk #', '')"
  44.         if (Check-Ext4 $devicePath) {
  45.             $partitionTypes[-1] = "ext4 (Linux)"
  46.         }
  47.     }
  48.  
  49.     # Create a string to show partition types
  50.     $fsTypeString = ($partitionTypes -join ", ")
  51.  
  52.     # Append drive info to the list with index number
  53.     $driveInfo += "$($i): Drive \\.\PHYSICALDRIVE$($drive.DeviceID -replace 'Disk #', ''): $($drive.Model), Partitions: $partitionCount, Filesystem Types: $fsTypeString"
  54. }
  55.  
  56. # Display all drives with index numbers and their partition types
  57. foreach ($info in $driveInfo) {
  58.     if ($info -match "ext4") {
  59.         Write-Host $info -ForegroundColor Red  # Highlight ext4 drives in red
  60.     } else {
  61.         Write-Host $info  # Default color for other drives
  62.     }
  63. }
  64.  
  65. # Prompt user to select a drive
  66. $driveIndex = Read-Host "Enter the drive index of the disk you want to mount (e.g., 0)"
  67. $driveToMount = $drives[$driveIndex]
  68.  
  69. # Mount the selected drive
  70. if ($driveToMount) {
  71.     $drivePath = "\\.\PHYSICALDRIVE$($driveIndex)"
  72.     wsl.exe --mount $drivePath --bare
  73.     Write-Host "Mounted $drivePath in WSL. Check with 'lsblk' in WSL."
  74.  
  75.     # Check for additional partitions
  76.     $partitions = Get-CimInstance -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($driveToMount.DeviceID)'} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
  77.     if ($partitions.Count -gt 1) {
  78.         for ($j = 1; $j -lt $partitions.Count; $j++) {
  79.             $partitionDevice = "\\.\PHYSICALDRIVE$($driveIndex)$j"
  80.             wsl.exe --mount $partitionDevice --bare
  81.             Write-Host "Mounted additional partition $partitionDevice in WSL."
  82.         }
  83.     }
  84.  
  85.     # Note for unmounting
  86.     Write-Host "To unmount, run: wsl --unmount $drivePath"
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement