2014-01-18 84 views
0

我有一个函数,用于测试一个或多个路径是否有效以及它们是否存在。现在我创建了一个包含多个数组的自定义PSObject,并希望函数返回该对象。但它似乎只返回一个数组而不是原始的自定义对象。 Powershell ISE的调试器显示在对象存在的函数中。我做错了什么?Powershell v2不返回函数的对象

这是我的代码

function Validate-Path 
{ 
    [CmdletBinding(SupportsShouldProcess=$true)] 
    Param 
    (
    [Parameter(Mandatory = $true)] 
    [Array]$Inputpaths 
    ) 
     $Ispathvalid=test-path $Inputpaths -IsValid 
     [email protected]() 
     [email protected]() 

     $counter=0 
     foreach ($item in $Ispathvalid) 
     { 
      if($Ispathvalid[$counter] -eq $true) 
      { 
       $Validpaths+=$Inputpaths[$counter] 
      } 
      else 
      { 
       $Invalidpaths+=$Inputpaths[$counter] 
      } 
      $counter++ 

     } 

     $Doespathexist=test-path $Validpaths 
     $Testvalue=$true 
     [email protected]() 
     [email protected]() 
     $counter=0 
     foreach ($item in $Validpaths) 
     { 
      if($Doespathexist[$counter] -eq $true) 
      { 
       $Existingpaths+=$Validpaths[$counter] 
      } 
      else 
      { 
       $NonExistingpaths+=$Validpaths[$counter] 
      } 
      $counter++ 

     } 

     $object = New-Object PSObject 
     $object | Add-Member -MemberType NoteProperty -Name Existingpaths -Value $Existingpaths 
     $object | Add-Member -MemberType NoteProperty -Name NonExistingpaths -Value $NonExistingpaths 
     $object | Add-Member -MemberType NoteProperty -Name Validpaths -Value $Validpaths 
     $object | Add-Member -MemberType NoteProperty -Name Invalidpaths -Value $Invalidpaths 

     return $object 
} 
+0

代码为我工作。你的系统上'$ PSVersionTable'的输出是什么。 –

+0

也适用于我。你能告诉你如何调用它,并产生输出吗? –

回答

0

使用PSCustomObject,而不是PSObject

改变这一行:

$object = New-Object PSObject 

要这样:

$object = New-Object -TypeName PSCustomObject; 
+0

这应该没什么区别,因为'PSObject'只是'PSCustomObject'的缩写。 –