15

i am comparing to folders with all their subfolders with powershell and its working fine on my local machine. but when i tried it on server its give me error and append

Microsoft.PowerShell.Core\FileSystem::\\

to all the files

if any one have do such work earlier please help

my script is

$Path = '\\Server1\Folder1'

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

and its give me error

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

please help

4

2 回答 2

29

试试Convert-Pathcmdlet:

PS> Convert-Path Microsoft.PowerShell.Core\FileSystem::C:\windows\system32
C:\windows\system32
于 2013-02-01T21:02:24.810 回答
3

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

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

$justThePath = $_.FullName.SubString($_.FullName.IndexOf($Path))
于 2013-02-01T18:33:01.707 回答