How to remove Microsoft.PowerShell.Core\FileSystem::\\ from path

不羁的心 提交于 2019-12-08 15:11:18

问题


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


回答1:


Try the Convert-Path cmdlet:

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



回答2:


I am not sure why the FullName property is prepending the powershell provider to the name, that's usually what you get when in the PsPath property.

At any rate, the reason why your attempt to strip it is failing is because your are passing a String to the SubString member function when it expects an integer index. To get the index of the start of your path, use the IndexOf member function:

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


来源:https://stackoverflow.com/questions/14652333/how-to-remove-microsoft-powershell-core-filesystem-from-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!