How can I query a temporary PS-Drive while returning files with a name relative to the drive?

后端 未结 2 1326
旧时难觅i
旧时难觅i 2020-12-20 12:41

I am trying to create a script where I will be searching the file servers for non inherited permissions. I have run into the 260 character limit for file names as a result.

2条回答
  •  庸人自扰
    2020-12-20 13:30

    haven't tested, but if you're not opposed to using 'subst' something like this might work for you

    function Get-FreeDriveLetter {
        $drives = [io.driveinfo]::getdrives() | % {$_.name[0]}
        $alpha = 65..90 | % { [char]$_ }
        $avail = diff $drives $alpha | select -ExpandProperty inputobject
        $drive = $avail[0] + ':'
        $drive
    }
    
    $file = gi 'C:\temp\file.txt'
    $fullname = $file.FullName
    
    if ($fullname.length -gt 240) {
        $drive = Get-FreeDriveLetter
        $path = Split-Path $fullname
        subst $drive $path
        $subst = $true
        rv path
        $fullname = Join-Path $drive $(Split-Path $fullname -Leaf)
    }
    
    $fullname
    

提交回复
热议问题