2014-11-21 160 views
0

我是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 

回答

0

使用參數列表參數:

$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

0

您需要使用一個建築工,你會在.NET。在您的示例中,您將-fileName參數傳遞給New-Object cmdlet,而不是FileInfo類。

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

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

$fileInfoData = Get-Item $path 

然後

$fileInfoData.FullName 
相關問題