Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么powershell有时会将PSObject(而不是基本对象)传递给.Net代码?_.net_Powershell_Powershell 2.0 - Fatal编程技术网

为什么powershell有时会将PSObject(而不是基本对象)传递给.Net代码?

为什么powershell有时会将PSObject(而不是基本对象)传递给.Net代码?,.net,powershell,powershell-2.0,.net,Powershell,Powershell 2.0,我发现,在某些情况下,powershell 2.0在作为参数传递到.Net代码时,会传递包含PSObject的Object[]数组,而不是真正包含的类型。我认为这是一个powershell 2.0错误,但我找不到任何提及它的地方 $Source = @" public static class DotNetTypeInfo { public static string TypeOfNthElement(object[] oa, int n) { return

我发现,在某些情况下,powershell 2.0在作为参数传递到.Net代码时,会传递包含PSObject的Object[]数组,而不是真正包含的类型。我认为这是一个powershell 2.0错误,但我找不到任何提及它的地方

$Source = @" 
public static class DotNetTypeInfo
{ 
    public static string TypeOfNthElement(object[] oa, int n)
    {
        return oa[n].GetType().ToString();
    }    
} 
"@ 
Add-Type -TypeDefinition $Source -Language CSharp  

$h = @{}
$h.foo = "bar"
#$h["foo"] = "bar" # uncomment this to fix! (Why?)

$testdata = @($h) # To .net, passed Object[] containing PSObject, not Hashtable!

# other examples that behave similarly badly - uncomment to try
$a = (1,2) # should result in an arry of System.Int32
function getA() { $a } # uncaptured value returned
function getB() { return $a } # explicit return
# $testdata = $a    # this works fine
# $testdata  = getA # also fine
# $testdata  = getB # To .net, passed Object[] containing PSObject, not Int32!

Write-Host "From powershell"
'  $testdata[0].GetType()         ' + $testdata[0].GetType().ToString()
Write-Host "From .Net"
'  TypeOfNthElement($testdata,0)  ' + [DotNetTypeInfo]::TypeOfNthElement($testdata,0)
这张照片是:

From powershell
  $testdata[0].GetType()         System.Collections.Hashtable
From .Net
  TypeOfNthElement($testdata,0)  System.Management.Automation.PSObject
最后一行非常令人惊讶;C#code正在接收一个包含
PSObject
对象[]
,而不是包含
哈希表的
对象[]
。这使得无法与不需要PSObject的现有.Net库一起使用

奇怪的是,如果我使用
$h[“foo”]=“bar”
而不是
$h.foo=“bar”
,我就可以解决这个问题

我还看到了类似的效果,这取决于您从函数返回值的方式-取消注释
getA
getB
示例以进行尝试

按要求-从
$pVersionTable
中选择的突出显示,为简洁起见进行了编辑:

Key   : CLRVersion, Value : 2.0.50727.5472
Key   : BuildVersion, Value : 6.1.7601.17514
Key   : PSVersion, Value : 2.0
Key   : PSCompatibleVersions, Value : {1.0, 2.0}
Key   : SerializationVersion, Value : 1.1.0.1

我可以在XP、Win7和Windows Server 2008 R2上使用powershell 2.0复制此问题

我使用powershell V2.0在W2K3框上尝试了您的代码,第三次测试返回了
[“hello”,“world”]

如果
getdata3
看起来像这样,我可以接收到您的错误:

function getdata3()
{
  $a
  return " "
}
因此,我的结论是,在脚本代码中有一些“隐藏”字符,这些字符在这个问题的副本/过去丢失了


还有一件事:你能在你的PowerShell上给出$psversiontable的值吗?

你在源代码中寻找隐藏字符吗?我已经将它按原样粘贴到脚本中,同样的问题。所以我没有任何你在这里没有的东西。你的函数也不应该引起这个错误(没有循环引用)。这个函数与您遇到的问题相同,如果我删除
return
关键字,它就会消失。所以我想你可能已经复制了这个问题。用很多新的信息修改了Q。如果你还在看-我能得到一个关于否决票的解释吗?我找不到PS 2是否仍然被修补/维护,但我已经