title: Powershell - Cannot convert value of type "System.Management.Automation.PSCustomObject" to type "System.Management.Automation.PSCustomObject"
body: <p>I am working with Powershell 4 under Windows 7 and I have cmdlet defined like this:</p>
  
  <pre><code> Function Transfer-File {
      [CmdletBinding()]
      Param(
          [Parameter(Mandatory=$true)]
          [System.Management.Automation.PSCustomObject]
          $source,
  
          # other parameters removed for brevity
      )
  
      # actual logic does not matter 
  
    }
  </code></pre>
  
  <p>I am calling it like this:</p>
  
  <pre><code>$hasError = Transfer-File -source $source -destination $cfg.Destination -logfile $logFile
  </code></pre>
  
  <p>$source is obtained by parsing a JSON file and looks like this:</p>
  
  <pre><code>@{Path=some path string here; Pack=False}
  </code></pre>
  
  <p><code>Write-Host $source.GetType()</code> outputs:</p>
  
  <pre><code>System.Management.Automation.PSCustomObject
  </code></pre>
  
  <p>I obtain the following error:</p>
  
  <blockquote>
    <p>Cannot convert the "@{Path=some path string here; Pack=False}"
    value of type "System.Management.Automation.PSCustomObject" to type
    "System.Management.Automation.PSCustomObject".</p>
  </blockquote>
  
  <p>In a desperate trial to solve it empirically I replace <code>System.Management.Automation.PSCustomObject</code> with <code>psobject</code> and it seems to work fine.</p>
  
  <p><strong>Question:</strong> Why does my <code>System.Management.Automation.PSCustomObject</code> does not seem to fit <code>System.Management.Automation.PSCustomObject</code> from the cmdlet prototype?</p>
  
  <hr>
  
  <p>Complete code for object creation:</p>
  
  <pre><code>### Configuration ###
  $cfg = New-Object –TypeName PSObject
  $cfg | Add-Member -MemberType NoteProperty –Name Sources –Value @()
  
  # get configuration from json file
  $jsonContent = Get-Content .\config.json |  Out-String
  $jsonObj = ConvertFrom-Json $jsonContent
  $cfg.Sources = $jsonObj.Sources
  
  Foreach ($source in $cfg.Sources)
  {
      # Write-Host $source.GetType()
      $hasError = Transfer-File -source $source -destination $cfg.Destination -logfile $logFile
  }
  </code></pre>