如何从路径中删除Microsoft.PowerShell.Core\文件系统::\\

k10s72fa  于 2023-02-23  发布在  Shell
关注(0)|答案(3)|浏览(124)

我正在比较文件夹与其所有子文件夹与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

请帮帮忙

c2e8gylq

c2e8gylq1#

尝试Convert-Path cmdlet:

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

gv8xihay2#

我不知道为什么FullName属性将powershell提供程序放在名称前面,这通常是在PsPath属性中得到的。
无论如何,尝试剥离它失败的原因是,当SubString成员函数期望整数索引时,您将String传递给它。要获取路径开始的索引,请使用IndexOf成员函数:

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

kd3sttzy3#

您也可以使用Substring()。例如:

$path = "Microsoft.PowerShell.Core\FileSystem::\\SERVERNAME\HOSTNAME\FOLDER"
$result = $path.Substring($path.IndexOf("FileSystem::") + 12)
Write-Output $result # Output: "\\SERVERNAME\HOSTNAME\FOLDER"

相关问题