Advertisement
Kevinator

Untitled

Jan 20th, 2024
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 1.60 KB | Source Code | 0 0
  1. ##########################
  2. # NEW ADDITION TO SCRIPT #
  3. ##########################
  4.  
  5. # TreeSize File Path
  6. $excelFilePath = "V:\Data\MediaFileDataFromTreeSize.xlsx"
  7.  
  8. # Import the Excel module
  9. Import-Module ImportExcel
  10.  
  11. # Import data from each sheet in the Excel file
  12. $sheets = Get-ExcelSheetInfo -Path $excelFilePath
  13.  
  14. foreach ($sheet in $sheets) {
  15.  
  16.     # Import data from Excel sheet to a PowerShell variable and skip first 4 rows
  17.     $data = Import-Excel -Path $excelFilePath -WorksheetName $sheet.Name | Select-Object -Skip 4
  18.  
  19.     # Map Excel column names to SQL Server column names
  20.     $columnMapping = @{
  21.         'Name' = 'full_path';
  22.         'Folder Path' = 'folder_path';
  23.         'Folders' = 'folders';
  24.         'Files' = 'files';
  25.         'Size' = 'size';
  26.         'Avg. File Size' = 'avg_size';
  27.         'Space free' = 'free_space';
  28.         'Dir Level (Relative)' = 'directory_level';
  29.         'File Extension' = 'file_extension';
  30.         'Last Modified' = 'date_modified';
  31.         'Creation Date' = 'date_created';
  32.         'Current Date' = 'date_imported';
  33.     }
  34.  
  35.     # Iterate through each row and generate SQL Server INSERT statement with correct column names
  36.     foreach ($row in $data) {
  37.    
  38.         # Make Sure All Row Data Has a Closing Quotation Mark"
  39.         $quotedValues = $columnMapping.Keys | ForEach-Object { "'$($row.$_ -replace "'", "''")'" }
  40.        $sqlRows = $quotedValues -join ', '
  41.        $insertQuery = "INSERT INTO $tableName ($($columnMapping.Values -join ', ')) VALUES ($sqlRows)"
  42.        Invoke-Sqlcmd -Query $insertQuery -ConnectionString $connectionString
  43.    }
  44. }
  45.  
  46.  
Tags: Import-Excel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement