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

sat*_*mar 12 powershell

我正在比较所有子文件夹与powershell的文件夹,并在我的本地机器上正常工作.但当我在服务器上尝试它时,它给我错误并追加

Microsoft.PowerShell.Core\FileSystem::\\
Run Code Online (Sandbox Code Playgroud)

到所有文件

如果有人早点做这样的工作,请帮忙

我的剧本是

$Path = '\\Server1\Folder1'

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

它给我错误

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
Run Code Online (Sandbox Code Playgroud)

请帮忙

Sha*_*evy 26

试试Convert-Pathcmdlet:

PS> Convert-Path Microsoft.PowerShell.Core\FileSystem::C:\windows\system32
C:\windows\system32
Run Code Online (Sandbox Code Playgroud)

  • 注意:这要求该路径存在于执行它的计算机上(因此,如果该路径是从远程计算机上的“Invoke-Command”返回的,然后针对本地结果运行“Convert-Path”,您会得到一个错误)。为了避免此类问题,您可以使用:`$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath`(请参阅/sf/answers/212868771/) (4认同)