SHARE
    TWEET
    J2897

    USB_Drive_Mapper_CSV

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

    AltStyle によって変換されたページ (->オリジナル) /