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