title: Get-Content works when run locally; returns nothing when run on file with length reported on deployment server
body: <p>I've inherited a deployment script that reads through files with specific names and extensions deployed to our on-prem web servers, looks line-by-line in those text files for string matches that signify they need replacing, and then inserts, in this case, the correct database and catalog names for the specific server being deployed to.</p>
  <p>Unfortunately, though the script works fine On My Computer (c)1984, it's not working when run by TFS as part of the deploy process.</p>
  <p>I've added an embarrassing amount of debug info, but haven't been able to track down the issue.</p>
  <h2>Code</h2>
  <p>Here's the pertinent code:<sup>1</sup></p>
  <pre><code>$getFilesParams = @{
      Path = $SearchDirectory
      Include = @(
        &quot;*.asa&quot;, &quot;*.asp&quot;,
        &quot;*.aspx&quot;, &quot;*.config&quot;,
        &quot;*.inc&quot;, &quot;*.ini&quot;,
        &quot;appsettings.json&quot;
      )
      Exclude = &quot;Web.*.config&quot;
      Recurse = $True
  }
  
  $files = @(Get-ChildItem @getFilesParams)
  foreach ($f in $files) {
  
  
  # DEBUG STARTS HERE
    Write-Feedback &quot;`tFile to search for Data Source: $f&quot; -LogFile &quot;$LogFile&quot;
  
    if ($f.Name -eq &quot;appsettings.json&quot;) {
      try {
        Write-Feedback &quot;`t`tFound the appsettings.json file: $($f.Length) $($f.LastWriteTime) $($f.Name)&quot; -LogFile $LogFile
  
        Get-Content $f | % {
          Write-Feedback &quot;!!!! Line: $_&quot;
        }
  
        Select-String $f -Pattern &quot;Data Source&quot; | % { Write-Feedback &quot;`t`t`tFound data source: $_&quot; }
  
        # was suspicious it'd work the first but not the second time. No, it fails each time I Get-Content.
        Get-Content $f | % {
          Write-Feedback &quot;@@@@ Line: $_&quot;
        }
      }
      catch {
        Write-Feedback &quot;An error occurred with appsettings.json:&quot; -LogFile $LogFile
        Write-Feedback $_ -LogFile $LogFile
      }
    }
  # DEBUG ENDS
  
  
  }
  
  $files = $files |
    Select-String -Pattern &quot;Data Source&quot; |
    Group-Object path |
    Select-Object name
  
  $count = $files.count
  if ($count)
  {
    @&quot;
  Found $count files....
  Processing:
  &quot;@ | Write-Feedback -LogFile $LogFile
  
    # etc etc he fixes the cable
  }
  else
  {
    '  Did not find any databases catalogs!' | Write-Feedback -LogFile $LogFile
  }
  </code></pre>
  <p>Then we go line by line through the files in <code>$files</code>. The problem is that my <code>appsettings.json</code> file, which <em>does</em> contain <code>Data Source</code> (okay, it's lowercase right now -- <code>data source</code>), doesn't get captured and no replacements are made.</p>
  <blockquote>
  <p>Note: <code>Write-Feedback</code> is a convenience function that writes to the console (<code>Write-Host</code>) <em>and</em> to a file, if one is given.</p>
  </blockquote>
  <h2>Local output</h2>
  <p>When I run locally, I get what I'm expecting (edited a bit for brevity):</p>
  <pre><code>File to search for Data Source: C:\path\appsettings.json
      Found the appsettings.json file: 993 01/12/2022 13:04:52 appsettings.json
  !!!! Line:  &quot;SomeJsonKey&quot;: &quot;data source=localhost;initial catalog=SomeDb;integrated security=True&quot;,
          Found datasource: C:\path\appsettings.json:9:  &quot;SomeJsonKey&quot;: &quot;data source=localhost;initial catalog=SomeDb;integrated security=True&quot;,
  @@@@ Line:  &quot;SomeJsonKey&quot;: &quot;data source=localhost;initial catalog=SomeDb;integrated security=True&quot;,
  Found 1 files....
  Processing:
    C:\path\appsettings.json....
  </code></pre>
  <h2>Production output</h2>
  <p>But when it's run as part of the deployment, I get...</p>
  <pre><code>File to search for Data Source: E:\path\appsettings.json
      Found the appsettings.json file: 762 01/14/2022 15:15:02 appsettings.json
  Did not find any databases catalogs!
  </code></pre>
  <p>So <em>it sees <code>appsettings.json</code></em> , it even <em>knows appsettings.json has a length</em> (they are different files, so the two lengths here aren't an issue), but it won't <code>Get-Content</code>, much less find the line with <code>Data Source</code> in it.</p>
  <p>Notepad++ says the file is ANSI, which is fine, I think. There're no extended characters, so that's the same as UTF8, which is what I expected. Don't think that'd break <code>Get-Content</code>.</p>
  <p>To be clear, Not necessarily looking for an answer. Just wondering what my next debug(s) step would be.</p>
  <p>I've looked up a little about <a href="https://stackoverflow.com/questions/22943289/powershell-what-is-the-best-way-to-check-whether-the-current-user-has-permissio">permissions</a>, though I'm not sure how I'd tell which entry from <code>(Get-Acl $Outfile).Access</code> represents the process that's currently running. But I also would expect an exception of some kind if it can't read the file.</p>
  <hr />
  <p>Just for fun, here's a picture of the release UI for the TFS server this is running on. If you've seen it before, you'll get it.</p>
  <p>I was hoping I could figure this out through straight PowerShell, but
  I can check things out there to a degree if that's useful. That said, I don't have full perms on the TFS site. I probably have more running as... whoever I'm running as when the PowerShell (third step) is executed.</p>
  <p><a href="https://i.stack.imgur.com/J9DsC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J9DsC.png" alt="the current release UI from TFS with three steps pictured: grab the zip with the PowerShell, unzip the PowerShell, run it." /></a></p>
  <p>(sorry for all the redaction; almost certainly not necessary, but better safe than sorry (sorrier?), etc?)</p>
  <hr />
  <p><sup>1</sup> I realize this code can be optimized. Please ignore that; it's, again, inherited, and I'm trying to get it to work with a minimum of churn.</p>