Advertisement
J2897

USB_Drive_Mapper_CSV

Mar 29th, 2025 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Maintains a CSV database of USB storage devices and their sync directories, preserving manual edits.
  4.  
  5. .DESCRIPTION
  6.     This script:
  7.     1. Detects currently connected USB storage devices using PnP.
  8.     2. Matches devices against a predefined mapping table ($deviceMappings) to assign source/target sync directories.
  9.     3. Updates a CSV file (DeviceMapping.csv) with new devices while preserving existing directory mappings.
  10.     4. Only populates empty Model/SourceDirectory/TargetDirectory fields - never overwrites user-defined values.
  11.  
  12. .KEY BEHAVIORS
  13.     - New devices: Added to CSV with "NotMapped" if no predefined mapping exists
  14.     - Existing devices: Blank fields are auto-filled, populated fields are preserved
  15.     - Model names: Extracted from DeviceID (PROD_* segment)
  16.  
  17. .USAGE
  18.     Run periodically or on USB insertion to keep CSV updated. Another script can use this CSV
  19.     to perform actual file synchronization.
  20.  
  21. .NOTES
  22.     CSV format:
  23.     Model,DeviceID,FriendlyName,SourceDirectory,TargetDirectory
  24. #>
  25.  
  26. # Define the mappings for DeviceID to SourceDirectory and TargetDirectory
  27. $deviceMappings = @{
  28.     "USBSTOR\DISK&VEN_FAKE&PROD_FLASH_DRIVE&REV_2.10\SN1234567890&0" = @{
  29.         SourceDirectory = "DataToSync"
  30.         TargetDirectory = "C:\Backup\USBFlashDrive"
  31.     }
  32.     "USBSTOR\DISK&VEN_GENERIC&PROD_USB_DISK&REV_6.50\4&ABCDEF12&0&_&0" = @{
  33.         SourceDirectory = "Test"
  34.         TargetDirectory = "C:\Flash\GeneralUDisk\Test"
  35.     }
  36.     "USBSTOR\DISK&VEN_VIRTUAL&PROD_STORAGE&REV_0001\ZZ9012345678&0" = @{
  37.         SourceDirectory = "ImportantDocs"
  38.         TargetDirectory = "C:\Archive\ST8GBDrive"
  39.     }
  40. }
  41.  
  42. # Function to extract the model from the DeviceID
  43. function Get-ModelFromDeviceID {
  44.     param (
  45.         [string]$DeviceID
  46.     )
  47.  
  48.     # Example regex to extract the model from DeviceID
  49.     # This regex is a placeholder and may need to be adjusted based on the actual DeviceID format
  50.     $match = [regex]::Match($DeviceID, 'PROD_(?<Model>[^&]+)')
  51.     if ($match.Success) {
  52.         return $match.Groups['Model'].Value
  53.     } else {
  54.         return "UnknownModel"
  55.     }
  56. }
  57.  
  58. # Get the USB devices and create custom objects with the required properties
  59. $usbDevices = Get-PnpDevice | Where-Object { $_.DeviceID -like "USBSTOR*" }
  60.  
  61. $newData = foreach ($device in $usbDevices) {
  62.     $friendlyName = $device.FriendlyName
  63.     $deviceId = $device.DeviceID
  64.     $model = Get-ModelFromDeviceID -DeviceID $deviceId
  65.  
  66.     if ($deviceMappings.ContainsKey($deviceId)) {
  67.         [PSCustomObject]@{
  68.             Model           = $model
  69.             FriendlyName    = $friendlyName
  70.             DeviceID        = $deviceId
  71.             SourceDirectory = $deviceMappings[$deviceId].SourceDirectory
  72.             TargetDirectory = $deviceMappings[$deviceId].TargetDirectory
  73.         }
  74.     } else {
  75.         [PSCustomObject]@{
  76.             Model           = $model
  77.             FriendlyName    = $friendlyName
  78.             DeviceID        = $deviceId
  79.             SourceDirectory = "NotMapped"
  80.             TargetDirectory = "NotMapped"
  81.         }
  82.     }
  83. }
  84.  
  85. # Path to the CSV file
  86. $csvPath = "C:\Users\J2897\Code\PowerShell\DriveSync\DeviceMapping.csv"
  87.  
  88. # Load the existing CSV file if it exists
  89. if (Test-Path $csvPath) {
  90.     $existingData = Import-Csv -Path $csvPath
  91. } else {
  92.     $existingData = @()
  93. }
  94.  
  95. # Create a hashtable to map DeviceID to existing data
  96. $existingDataHashtable = @{}
  97. foreach ($item in $existingData) {
  98.     $existingDataHashtable[$item.DeviceID] = $item
  99. }
  100.  
  101. # Update the existing data with new data
  102. foreach ($newItem in $newData) {
  103.     $deviceId = $newItem.DeviceID
  104.  
  105.     if ($existingDataHashtable.ContainsKey($deviceId)) {
  106.         $existingItem = $existingDataHashtable[$deviceId]
  107.  
  108.         # Update only if the existing cells are missing or blank
  109.         if ([string]::IsNullOrEmpty($existingItem.Model)) {
  110.             $existingItem.Model = $newItem.Model
  111.         }
  112.         if ([string]::IsNullOrEmpty($existingItem.SourceDirectory)) {
  113.             $existingItem.SourceDirectory = $newItem.SourceDirectory
  114.         }
  115.         if ([string]::IsNullOrEmpty($existingItem.TargetDirectory)) {
  116.             $existingItem.TargetDirectory = $newItem.TargetDirectory
  117.         }
  118.     } else {
  119.         # Add new item if it doesn't exist
  120.         $existingData += $newItem
  121.     }
  122. }
  123.  
  124. # Export the updated data to the CSV file
  125. $existingData | Export-Csv -Path $csvPath -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement