Advertisement
J2897

Generate Data Tables

May 6th, 2016
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <# Function for generating data tables. Leave the New-Table function as it is and play with the stuff below it.
  2. AMIABTY...
  3. All my ideas are belong to you. #>
  4.  
  5. cls
  6. function New-Table {
  7.     param([string]$TableName, [hashtable]$Columns)
  8.     Write-Host 'Table:' $TableName
  9.     $Table = New-Object system.Data.DataTable $TableName
  10.     foreach ($Column in $Columns.GetEnumerator()) {
  11.         Write-Host "$($Column.Name): $($Column.Value)"
  12.         $NewColumn = New-Object system.Data.DataColumn $($Column.Name),($($Column.Value))
  13.         $Table.columns.add($NewColumn)
  14.     }
  15.     return, $Table
  16. }
  17.  
  18. # Specify the Columns.
  19. $GenerateTheseColumns = @{
  20.     'SHA1' = [string];
  21.     'DateTime' = [DateTime];
  22.     'FileName' = [string]
  23. }
  24.  
  25. # Create the table.
  26. $SHA1Table = New-Table -TableName 'SHA1Table' -Columns $GenerateTheseColumns
  27.  
  28. # Create rows.
  29. $Row1 = $SHA1Table.NewRow()
  30. $Row2 = $SHA1Table.NewRow()
  31.  
  32. # Enter data in the rows.
  33. $Row1.SHA1 = "A"
  34. $Row1.FileName = "1"
  35. $Row2.SHA1 = "B"
  36. $Row2.FileName = "2"
  37.  
  38. # Add the rows to the table.
  39. $SHA1Table.Rows.Add($Row1)
  40. $SHA1Table.Rows.Add($Row2)
  41.  
  42. # Display the table.
  43. $SHA1Table # | format-table -AutoSize
  44.  
  45. # http://stackoverflow.com/questions/9015138/powershell-looping-through-a-hash-or-using-an-array
  46. # http://stackoverflow.com/questions/11562634/powershell-returning-data-tables-without-rows-in-functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement