我正在使用Windows 7下的Powershell 4,我的cmdlet定义如下:
Function Transfer-File {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[System.Management.Automation.PSCustomObject]
$source,
# other parameters removed for brevity
)
# actual logic does not matter
}
我这样称呼它:
$hasError = Transfer-File -source $source -destination $cfg.Destination -logfile $logFile
$ source是通过解析JSON文件获得的,如下所示:
@{Path=some path string here; Pack=False}
Write-Host $source.GetType()
输出:
System.Management.Automation.PSCustomObject
我收到以下错误:
无法在此处转换" @ {Path =某些路径字符串;包= FALSE}" 类型的值" System.Management.Automation.PSCustomObject"输入 " System.Management.Automation.PSCustomObject"
在绝望的试验中,凭经验解决它,我将System.Management.Automation.PSCustomObject
替换为psobject
,它似乎工作正常。
问题:为什么我的System.Management.Automation.PSCustomObject
似乎不适合cmdlet原型中的System.Management.Automation.PSCustomObject
?
完整的对象创建代码:
### 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
}
答案 0 :(得分:3)
正如@ VivekKumarSingh的评论所暗示的,这是一个与加速器相关的错误。
这不起作用:
# Error
$y = [System.Management.Automation.PSCustomObject]@{Path='some path string here'; Pack=$False}
这不起作用:
$y = [PSCustomObject]@{Path='some path string here'; Pack=$False}
function t ([System.Management.Automation.PSCustomObject]$x) { $x }
# Error
t $y
这将有效:
$y = [PSCustomObject]@{Path='some path string here'; Pack=$False}
function t ([PSCustomObject]$x) { $x }
t $y
但是,上面的不类型会将任何内容投射到PSCustomObject
,因为该类型继承自Object
:
PS> function t ([PSCustomObject]$x) { $x.GetType().FullName }
PS> t 5
System.Int32
PS> t 'asdf'
System.String
PS> t $(Get-ChildItem)
System.Object[]
PS> t $(Get-Item .)
System.IO.DirectoryInfo
这也可行,但我不完全确定它是否100%相同。
function t ([System.Management.Automation.PSObject]$x) { $x }
我不确定的原因是因为这种奇怪:
PS> $a = New-Object -TypeName System.Management.Automation.PSObject -Property @{Path='some path string here'; Pack=$False}
PS> $a.GetType().FullName
System.Management.Automation.PSCustomObject
PS> $a = New-Object -TypeName System.Management.Automation.PSCustomObject -Property @{Path='some path string here'; Pack=$False}
New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Management.Automation.PSCustomObject.
PS> $a = [PSCustomObject]@{Path='some path string here'; Pack=$False}
PS> $a.GetType().FullName
System.Management.Automation.PSCustomObject
PS> $a = [System.Management.Automation.PSObject]@{Path='some path string here'; Pack=$False}
PS> $a.GetType().FullName
System.Collections.Hashtable
PS> $a = [PSObject]@{Path='some path string here'; Pack=$False}
PS> $a.GetType().FullName
System.Collections.Hashtable
据我所知,[PSCustomObject]
和[PSObject]
都是System.Management.Automation.PSObject
的加速器。 System.Management.Automation.PSCustomObject
只是他们为使[PSCustomObject]
成为可能而添加的实现细节,但不幸的是,所有工作的方式都有点不一致。但是,我并不完全清楚[PSObject]
型加速器的用途。它似乎什么都不做,但它也确实存在:
PS> ([System.Management.Automation.Cmdlet]).Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get.ContainsKey('psobject')
True