title: Powershell hash table call external command
body: <p>I have a powershell script that I schedule to check the previous ten days's dated "transaction" files and sorts by LastWriteTime because the year-to-date data is always growing. There are no changes over the weekend, and if there are none during the week I know the process on the server didn't run. </p>
  
  <p>The resulting table has columns "Name", "Size (KB)" (converted "Length" from bytes to KB), and LastWriteTime. I'd like to add an "MD5" column. I have an md5sum.exe command on my computer. I'd like to construct the hash table such that it calls the external command within the <code>expression</code> field using the <code>$_.FullName</code> property. </p>
  
  <p>I found how to calculate the MD5 checksum using PS natively from <a href="https://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell">this post</a> and I've integrated it as a one-liner into the <code>expression</code> value for the new column. I'd still like to know if it's possible to call an external command inside a hash table though.</p>
  
  <p>Here's the original code:</p>
  
  <pre><code>$LOOKBACKXDAYS=( Get-Date ).AddDays(-10)
  $SrcMachine='TheServerInQuestion'
  
  if( -not ( Test-Path -Path \\$SrcMachine\C$ ) ) { net use \\$SrcMachine\C$ /user:mydomain\administrator }
  
  Get-ChildItem -Path \\$SrcMachine\C$\temp transactions*.tab | `
      Where-Object {$_.LastWriteTime -ge $LOOKBACKXDAYS } | `
          Select-Object `
              -Property `
                  Name , `
                  @{name='Size (KB)'; expression={[string]([math]::ceiling($_.Length / [math]::pow(2, 10)))}} , `
                  LastWriteTime | `
              Sort-Object `
                  -Property `
                      'Size (KB)', `
                      LastWriteTime, `
                      Name | `
                  Out-Host
  Write-Host -ForegroundColor Green "`n`nAll Done!`n`n"
  pause
  </code></pre>
  
  <p>and it produces output that looks like this:</p>
  
  <pre><code>Name                          Size (KB) LastWriteTime
  ----                          --------- -------------
  transactions for 12172018.tab 13783     2018-12-17 12:05:04
  transactions for 12182018.tab 13824     2018-12-18 12:05:06
  transactions for 12192018.tab 13869     2018-12-19 12:05:16
  transactions for 12202018.tab 13901     2018-12-20 12:05:14
  transactions for 12212018.tab 13901     2018-12-21 12:05:12
  transactions for 12222018.tab 13931     2018-12-22 12:05:16
  transactions for 12232018.tab 13931     2018-12-23 12:05:12
  transactions for 12242018.tab 13954     2018-12-24 12:05:14
  transactions for 12252018.tab 13954     2018-12-25 12:05:16
  transactions for 12262018.tab 14001     2018-12-26 12:05:26
  </code></pre>
  
  <p>This is the modified code that produces lower case, hyphen removed MD5 checksums, and I added a column that shows a 'Y' if the day of the week is a weekend so I can ignore an identical file. I changed some other stuff too.</p>
  
  <pre><code>$LOOKBACKXDAYS=( Get-Date ).AddDays(-10)
  $SrcMachine='TheServerInQuestion'
  
  if( -not ( Test-Path -Path TabFiles:\temp ) ) {
      New-PSDrive `
          -PSProvider FileSystem `
          -Name TabFiles `
          -Root \\TheServerInQuestion\C$ `
          -Credential mydomain\administrator
  }
  
  Get-ChildItem -Path TabFiles:\temp transactions*.tab | `
      Where-Object {$_.LastWriteTime -ge $LOOKBACKXDAYS } | `
          Select-Object `
              -Property `
                  Name , `
                  @{name='Size (KB)'; expression={[string]([math]::ceiling($_.Length / [math]::pow(2, 10)))}} , `
                  @{name='MD5'; expression={ [System.BitConverter]::ToString($(New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider).ComputeHash([System.IO.File]::ReadAllBytes($_.FullName))).replace('-','').ToLower() }}, `
                  @{name='Is Weekend'; expression={ $( if ($_.LastWriteTime.dayofweek -match 'Saturday|Sunday'){return 'Y'} else{return ''} ) } }, `
                  LastWriteTime | `
              Sort-Object `
                  -Property `
                      'Size (KB)', `
                      LastWriteTime, `
                      Name | `
                  Format-Table
  Write-Host -ForegroundColor Green "`n`nAll Done!`n`n"
  pause
  </code></pre>
  
  <p>This is the output for the script above:</p>
  
  <pre><code>Name                          Size (KB) MD5                              Is Weekend LastWriteTime
  ----                          --------- ---                              ---------- -------------
  transactions for 12172018.tab 13783     e825b1a58954203da9f8a16c454d9441            2018-12-17 12:05:04
  transactions for 12182018.tab 13824     4de56734ceacd02db11bdd8a3dcdc628            2018-12-18 12:05:06
  transactions for 12192018.tab 13869     1e4401afd2734004da372bfcf1e90395            2018-12-19 12:05:16
  transactions for 12202018.tab 13901     49b1c5e29ea4da7e8a7a72bf610cecd6            2018-12-20 12:05:14
  transactions for 12212018.tab 13901     49b1c5e29ea4da7e8a7a72bf610cecd6            2018-12-21 12:05:12
  transactions for 12222018.tab 13931     36e40a546c6049e550b0feac9aa7adc7 Y          2018-12-22 12:05:16
  transactions for 12232018.tab 13931     36e40a546c6049e550b0feac9aa7adc7 Y          2018-12-23 12:05:12
  transactions for 12242018.tab 13954     9c97ccf81ce4cbb583fff348a739cc66            2018-12-24 12:05:14
  transactions for 12252018.tab 13954     9c97ccf81ce4cbb583fff348a739cc66            2018-12-25 12:05:16
  transactions for 12262018.tab 14001     ad18d544c8dea1d3d9cf1512c4a772e4            2018-12-26 12:05:26
  </code></pre>
  
  <p>There's ignorable duplicate data over the weekend and from Christmas Eve to Christmas, but there was inexcusable duplicate data from the 20th and 21st.</p>