title: Pester Mock does not work for Invoke-Command using script block
body: <p>I have a console logger</p>
  
  <pre><code>function Common-Write-Log-Console
  {
      param (
          [Parameter(Mandatory=$true)]
          [string] $logText
          )
  
          $textToOutput = [String]::Format("{0}:{1}", [System.DateTime]::Now.ToString(), $logText)
          Write-Output ($textToOutput)
  }
  </code></pre>
  
  <p>Then I have wrapper function which calls it by dynamically loading it</p>
  
  <pre><code>function Common-Write-Log-WithInvoke
  {
      param (
          [Parameter(Mandatory=$true)]
          [string] $logText
  
          )
  
      foreach($logger in $loggers.Values)
      {
          Invoke-Command $logger -ArgumentList $logText,$verbosityLevel,$logType
      }
  
  }
  </code></pre>
  
  <p>Another wrapper function which calls it directly</p>
  
  <pre><code>function Common-Write-Log-WithoutInvoke
  {
      param (
          [Parameter(Mandatory=$true)]
          [string] $logText, 
          [string] $verbosityLevel = "Normal",
          [string] $logType = "Info"
          )
  
      Common-Write-Log-Console $logText
  
  }
  </code></pre>
  
  <p>Add Loggers for dynamic calling</p>
  
  <pre><code> $loggers = @{}
   $loggers.Add("Console_Logger", ${function:Common-Write-Log-Console})
  </code></pre>
  
  <p>Now I have couple of Pester tests</p>
  
  <pre><code> # pester tests
  Describe "Common-Write-Log" {
      It "Test 1. Calls all log sources when log sources are called directly - **this test passes**" {
  
  
          # Arrange
          $expectedLogText  = "test message" 
          Mock Common-Write-Log-Console -Verifiable -ParameterFilter { $logText -eq  $expectedLogText}
  
          # Act
          Common-Write-Log-WithoutInvoke "test message"
  
          # Assert
          Assert-VerifiableMocks
      }
  
      It "Test 2. Calls all log sources when log sources are called through Invoke-Command - **this test fails**" {
  
  
          # Arrange
          $expectedLogText  = "test message" 
          Mock Common-Write-Log-Console -Verifiable -ParameterFilter { $logText -eq  $expectedLogText}
  
          # Act
          Common-Write-Log-WithInvoke "test message"
  
          # Assert
          Assert-VerifiableMocks # This statement fails as actual function "Common-Write-Log-Console" is called instead of the mocked one
      }
  }
  </code></pre>
  
  <p>Test 2. always fails. I have worked around by creating a fake logger function, instead of using mock and setting some global variables to verify/assert in my test that dynamic loading and calling of intended function works. It would be nice to get the Mock working in such scenario , rather then writing those dumb fakes! </p>
  
  <p>Any ideas how would it work or is it not supported by pester at all?</p>
  
  <p>PS: All code works if copied in order</p>