I am working with Powershell 4 under Windows 7 and I have cmdlet defined like this:
Function Transfer-File {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCustomObject]
$source,
# other parameters removed for brevity
)
# actual logic does not matter
}
I am calling it like this:
$hasError = Transfer-File -source $source -destination $cfg.Destination -logfile $logFile
$source is obtained by parsing a JSON file and looks like this:
@{Path=some path string here; Pack=False}
Write-Host $source.GetType()
outputs:
System.Management.Automation.PSCustomObject
I obtain the following error:
Cannot convert the "@{Path=some path string here; Pack=False}" value of type "System.Management.Automation.PSCustomObject" to type "System.Management.Automation.PSCustomObject".
In a desperate trial to solve it empirically I replace System.Management.Automation.PSCustomObject
with psobject
and it seems to work fine.
Question: Why does my System.Management.Automation.PSCustomObject
does not seem to fit System.Management.Automation.PSCustomObject
from the cmdlet prototype?
Complete code for object creation:
### 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
}