Advertisement
J2897

Map Devices to CSV

Mar 29th, 2025 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define the mappings for DeviceID to SourceDirectory and TargetDirectory
  2. $deviceMappings = @{
  3.     "USBSTOR\DISK&VEN_FAKE&PROD_FLASH_DRIVE&REV_2.10\SN1234567890&0" = @{
  4.         SourceDirectory = "DataToSync"
  5.         TargetDirectory = "C:\Backup\USBFlashDrive"
  6.     }
  7.     "USBSTOR\DISK&VEN_GENERIC&PROD_USB_DISK&REV_6.50\4&ABCDEF12&0&_&0" = @{
  8.         SourceDirectory = "Test"
  9.         TargetDirectory = "C:\Flash\GeneralUDisk\Test"
  10.     }
  11.     "USBSTOR\DISK&VEN_VIRTUAL&PROD_STORAGE&REV_0001\ZZ9012345678&0" = @{
  12.         SourceDirectory = "ImportantDocs"
  13.         TargetDirectory = "C:\Archive\ST8GBDrive"
  14.     }
  15. }
  16.  
  17. # Get the USB devices and create custom objects with the required properties
  18. $usbDevices = Get-PnpDevice | Where-Object { $_.DeviceID -like "USBSTOR*" }
  19.  
  20. $customObjects = foreach ($device in $usbDevices) {
  21.     $deviceId = $device.DeviceID
  22.     $status = $device.Status
  23.  
  24.     if ($deviceMappings.ContainsKey($deviceId)) {
  25.         [PSCustomObject]@{
  26.             DeviceID        = $deviceId
  27.             SourceDirectory = $deviceMappings[$deviceId].SourceDirectory
  28.             TargetDirectory = $deviceMappings[$deviceId].TargetDirectory
  29.         }
  30.     } else {
  31.         [PSCustomObject]@{
  32.             DeviceID        = $deviceId
  33.             SourceDirectory = "NotMapped"
  34.             TargetDirectory = "NotMapped"
  35.         }
  36.     }
  37. }
  38.  
  39. # Export the custom objects to a CSV file
  40. $customObjects | Export-Csv -Path "C:\Users\J2897\DeviceMapping.csv" -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement