Powershell使用参数实例化类构造函数

Powershell使用参数实例化类构造函数

问题描述:

我是Powerhell的新手。我试图写一个简单的例程,它失败了。代码和错误消息如下。我尝试了很多种组合。我究竟做错了什么?Powershell使用参数实例化类构造函数

$fileContent = Get-Content "ExePaths.txt" 
foreach ($file in $fileContent) 
{ 
    if($file.Contains("Executable")) 
     { 
      $path = $file.Replace("""Executable""=","") 
      $path = $path.Replace("\\","\") 
      echo $path 
      $fileInfoData = New-Object -TypeName System.IO.FileInfo -fileName $path 
      echo $fileInfoData.FullName 
      break; 

      ##echo $fileInfoData.FullName 
     } 
} 

错误消息

New-Object : A parameter cannot be found that matches parameter name 'fileName'. 
At C:\Users\siluvaie\Desktop\Docs\Barcap\Projects\FV8\reg from prod servers\ReadFile.ps1:9 char:69 
+    $fileInfoData = New-Object -TypeName System.IO.FileInfo -fileName <<<< $path 
    + CategoryInfo   : InvalidArgument: (:) [New-Object], ParameterBindingException 
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand 

使用参数列表参数:

$fileInfoData = New-Object -TypeName System.IO.FileInfo -ArgumentList $path 
+0

我试过了。它不适合我。你确定? – Hunter 2014-11-21 10:26:43

+0

只要$ Path包含它应该工作的有效路径。在我的最后尝试它,它的工作原理应该如此。 – ojk 2014-11-21 10:28:16

您需要使用一个建筑工,你会在.NET。在您的示例中,您将-fileName参数传递给New-Object cmdlet,而不是FileInfo类。

$fileInfoData = New-Object -TypeName System.IO.FileInfo($path) 

我会用Get-Item而不是System.IO.FileInfo

$fileInfoData = Get-Item $path 

然后

$fileInfoData.FullName