16

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
  • What happened to all your underscores in $_ variables? Commented Feb 1, 2013 at 18:05

3 Answers 3

34

Try the Convert-Path cmdlet:

PS> Convert-Path Microsoft.PowerShell.Core\FileSystem::C:\windows\system32
C:\windows\system32
Sign up to request clarification or add additional context in comments.

1 Comment

NB: This requires that the path exist on the machine on which it's executed (so if the path were returned from Invoke-Command on a remote machine, then Convert-Path were run against the results locally, you'd get an error). To avoid such issues you can use: $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath (see stackoverflow.com/a/3040982/361842)
3

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))

4 Comments

Now script give me other error Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'IndexOf'. At \AppData\Local\Temp\78c0b727-7a26-4b1f-8250-43262dbb326e.ps1:14 char:110 + Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.SubString($_.IndexOf <<<< ($Path))#$_.FullName.Substring($Path) + CategoryInfo : InvalidOperation: (IndexOf:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
@satishkumar oops, I forgot the fullname property. See my updated answer.
Again the same error Directory: \\Server1\ServerListPowershell\PowerScript Mode LastWriteTime Length Name ---- ------------- ------ ---- -ar- 10/9/2012 12:03 PM 1473 01 Server.ps1 Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero. Parameter name: startIndex" At AppData\Local\Temp\78c0b727.ps1:16 char:108 + Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.SubString <<<< ($_.FullName.IndexOf($Path)) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedEr
@satishkumar No, that's not the same error, you're getting "StartIndex cannot be less than zero" which means that IndexOf() can't find $Path in the string. You have something else going on. What is the value of $_ and $Path when it fails?
0

You could use the Substring() as well. For example:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.