Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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使用参数实例化类构造函数_Powershell - Fatal编程技术网

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

Powershell使用参数实例化类构造函数,powershell,Powershell,我对powershell是新手。我试图编写一个简单的例程,但失败了。下面给出了代码和错误消息。我试了不少组合。我做错了什么 $fileContent = Get-Content "ExePaths.txt" foreach ($file in $fileContent) { if($file.Contains("Executable")) { $path = $file.Replace("""Executable""=","")

我对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
新对象:找不到与参数名称“fileName”匹配的参数。
位于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使用ArgumentList参数:

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

您需要像在.NET中一样使用构造函数。在您的示例中,您将
-fileName
参数传递给
New Object
cmdlet,而不是
FileInfo

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

我将使用
Get Item
而不是
System.IO.FileInfo

$fileInfoData = Get-Item $path
然后


只要$Path包含有效路径,它就应该工作。在我这边试过了,而且效果很好。
$fileInfoData.FullName