1

I have been trying to run this script that captures all the files / folders within a share directory and passes it back to splunk. However the script is giving PathTooLongException 's even though the longest path i can find comes in at 197 characters.

I have tried mapping a share as a folder called z on the root of the C drive which doesnt help at all. Can someone shed some light on how to get around this or what is causing the errors?

param (
[string]$directory = "C:\Windows\Logs"
 )
 $errorCount = 0
$OutputList = @()
$directoryLink = 'c:\z'

#set up symbolic link
cmd /c mklink /d $directoryLink $directory

 try
{
$colItems = (Get-ChildItem -Path $directoryLink -Recurse | Sort-Object) 
}
catch
{
$errorCount += 1   
}
foreach ($i in $colItems) 
{
try
{        
    $subFolderItems = (Get-Item $i.FullName | Measure-Object -property 
    length -sum -ErrorAction SilentlyContinue) 
    $acl=$i.GetAccessControl()
    $SizeValue= [Math]::Round(($subFolderItems.sum / 1MB),3)

    $SplunkFileList = New-Object PSObject  
    $SplunkFileList | Add-Member -type NoteProperty -name Filename -Value $i.FullName
    $SplunkFileList | Add-Member -type NoteProperty -name SizeMb -Value $SizeValue
    $SplunkFileList | Add-Member -type NoteProperty -name LastAccess -Value $i.LastAccessTime
    $SplunkFileList | Add-Member -type NoteProperty -name Owner -Value $acl.Owner

    if ($i.PSIsContainer)
    {
        $SplunkFileList | Add-Member -type NoteProperty -name Type -Value "D"
    }
    else
    {
        $SplunkFileList | Add-Member -type NoteProperty -name Type -Value "F"
    }

    $OutputList += $SplunkFileList  

} 
catch [System.IO.IOExeception]
{
    Write-Host 'An Exception was caught.'
    Write-Host "Exception : $($_.Exception.GetType().FullName)"
    $errorCount += 1
    }   
} 
$OutputList | Select-Object Filename,SizeMb,LastAccess,Owner,Type | Format-
List
1
  • Get-LongPathName: Robocopy is used to recursively search through a folder structure to find file or folder names that have more than a certain number of characters. The function returns an object with three properties: FullPath,Type and FullPath. Commented Sep 20, 2018 at 13:40

1 Answer 1

3

If you're using a modern version of Powershell (v5.1+ I think) you can get to the paths longer than 260 characters using the unicode version of Windows API.

To do this you prefix the path with \\?\ eg:

Get-ChildItem -LiteralPath '\\?\C:\Windows\Logs' -Recurse

Note: LiteralPath instead of Path


If you want to use a UNC path (\\server\C$\folder) the syntax is slightly different:

Get-ChildItem -LiteralPath '\\?\UNC\server\C$\folder' -Recurse 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, unfortunately splunk natively uses powershell 2.0. Im guessing there isnt a way around this then?
That's an ancient PS version that only came on Server 2008/2008R2. If you're using those OS you need to update more than just Powershell! The only other method is a library like github.com/alphaleonis/AlphaFS but I've not used this as have modern PS version.

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.