如何从路径中删除Microsoft.PowerShell.Core \ FileSystem :: \\

时间:2013-02-01 18:02:36

标签: powershell

我正在将所有子文件夹与powershell进行比较,并且它在我的本地计算机上工作正常。但当我在服务器上尝试它时,它给我错误并附加

Microsoft.PowerShell.Core\FileSystem::\\

到所有文件

如果有人提前做过这样的工作请帮助

我的脚本是

$Path = '\\Server1\Folder1'

Get-ChildItem $Path -Recurse | ? { !$_.PSIsContainer } | % { 
        Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.Substring($Path)
        $_
    }

它给我的错误

Cannot convert argument "0", with value: "Microsoft.PowerShell.Core\FileSystem::\\Server1\Folder1\ServerListPowershell", for "Substring" to type "System.Int32": "Cannot convert value "M
icrosoft.PowerShell.Core\FileSystem::\\Server1\Folder1\ServerListPowershell" to type "System.Int32". Error: "Input string was not in a correct format.""
At C:\Users\cwr.satish.kumar\AppData\Local\Temp\898f72f1-a0d3-4003-b268-128c5efc9f2b.ps1:14 char:108
+         Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.Substring <<<< ($Path)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

请帮助

2 个答案:

答案 0 :(得分:24)

尝试Convert-Path cmdlet:

PS> Convert-Path Microsoft.PowerShell.Core\FileSystem::C:\windows\system32
C:\windows\system32

答案 1 :(得分:2)

我不确定为什么FullName属性正在将powershell提供程序添加到名称之前,这通常是在PsPath属性中获得的。

无论如何,您尝试剥离它的原因是失败的原因是因为您在期望整数索引时将String传递给SubString成员函数。要获取路径起点的索引,请使用IndexOf成员函数:

$justThePath = $_.FullName.SubString($_.FullName.IndexOf($Path))