raandree / NTFSSecurity

Managing permissions with PowerShell is only a bit easier than in VBS or the command line as there are no cmdlets for most day-to-day tasks like getting a permission report or adding permission to an item. PowerShell only offers Get-Acl and Set-Acl but everything in between getting and setting the ACL is missing. This module closes the gap.
MIT License
429 stars 59 forks source link

Get-ChildItem2 fails if UNC-Path contains .lnk file #46

Open karlheinz-hildebrandt opened 5 years ago

karlheinz-hildebrandt commented 5 years ago

Using Get-ChildItem2 on a network UNC-Path shows error, if the folder contains a .lnk file. Error is:

Get-ChildItem2 : (3) The target directory is a file, not a directory: [\] In Zeile:1 Zeichen:1

Using Get-ChildItem runs without problem.

AspenForester commented 5 years ago

I'm seeing the same error on a directory that has no .lnk files in it, but it does have an odd zero-length .xlsx file whose name starts with a $ (dollar sign).

When I change that $ to a ~, the error goes away. So in my case it's looking like a problem handling questionable characters in the filename.

AspenForester commented 5 years ago

I am able to reproduce the error with a directory containing a .lnk file.
Get-ChildItem2 -Path \\myserver\myshare\MyDir -Directory when \myserver\myshare\MyDir contains a file that's a .lnk.

karlheinz-hildebrandt commented 5 years ago

I can confirm the problem with leading $ in the filename, but only if the file is on a share. As far as I know the $ is allowed in filenames, especially when Get-ChildItem runs without problem. Thanks for the warning.

Sup3rlativ3 commented 4 years ago

For what it's worth, I'm unable to reproduce this error with or without the dollarsign on the .lnk file.

PS C:\> Get-ChildItem2 -Path \\MyComputer\RBAC

    Directory: \\MyComputer\RBAC

Mode    Inherits             LastWriteTime         Size(M) Name
----    --------             -------------         ------- ----
-a---       True      09/01/2020   6:54 AM               0 MyLink.lnk
d----       True      08/01/2020  12:08 PM   <DIR>         Folder1
d----       True      27/11/2019  12:10 PM   <DIR>         Folder2
d----       True      27/11/2019  12:04 PM   <DIR>         Folder3

PS C:\> Get-ChildItem2 -Path \\8ZHFWT2\RBAC

    Directory: \\MyComputer\RBAC

Mode    Inherits             LastWriteTime         Size(M) Name
----    --------             -------------         ------- ----
-a---       True      09/01/2020   6:54 AM               0 $MyLink.lnk
d----       True      08/01/2020  12:08 PM   <DIR>         Folder1
d----       True      27/11/2019  12:10 PM   <DIR>         Folder2
d----       True      27/11/2019  12:04 PM   <DIR>         Folder3

I also tested the example that @karlheinz-hildebrandt mentioned in his first post but wasn't able to get the same error. I did change it from '\' to '.\' which I think is required as the latter pulls from the root drive rather than the current folder. I should also mention that both work though.

PS C:\RBAC> Get-ChildItem2 -Path .\

    Directory: C:\RBAC

Mode    Inherits             LastWriteTime         Size(M) Name
----    --------             -------------         ------- ----
-a---       True      09/01/2020   6:54 AM               0 $Banklink.lnk
d----       True      08/01/2020  12:08 PM   <DIR>         Folder1
d----       True      27/11/2019  12:10 PM   <DIR>         Folder2
d----       True      27/11/2019  12:04 PM   <DIR>         Folder3

If I move into a UNC path and then run the command on the current location I get an error though it's not the same one.

PS C:\RBAC> cd \\MyComputer\RBAC

PS Microsoft.PowerShell.Core\FileSystem::\\MyComputer\RBAC> Get-ChildItem2 -Path .\
Get-ChildItem2 : Unable to find the specified file.
At line:1 char:1
+ Get-ChildItem2 -Path .\
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (.\:String) [Get-ChildItem2], FileNotFoundException
    + FullyQualifiedErrorId : FileNotFound,NTFSSecurity.GetChildItem2

I'm unsure if any of this helps but I hope so.

nashiooka commented 3 years ago

I'm having a similar issue, but I can offer some additional specifics on when it occurs. In my case it doesn't seem to have anything to do with .lnk files. As others have indicated looks like the leading character of the filename matters. So far I've found the problem with the following leading characters -, +, #, {, $ . Without hunting for every possible character I think it's fair to say the issue occurs with any legal, leading non-alphanumeric character.

Note: It's very tough to control file naming in the wild, so all of these are problematic. However, the "$" leads many names in the common $RECYCLE.BIN folder. I presume this would only increase the prevalence of the issue.

Similar to what James pointed out, my reproductions have been a little inconsistent though. When testing locally it seems harder to reproduce, for example in c:\temp. This remains true even when I use the UNC to the local path. I also had some difficulty reproducing it in a purely remote folder when starting the interrogation at that same level, but that's been inconsistent so far.

Also note I discovered the problem in Remove-Item2, but can reproduce and have tested with Get-ChildItem2. I haven't looked at any code so I don't know if one leverages the other.

Sorry if this is coming to the conversation late. I hope it's helpful.

raandree commented 3 years ago

Thanks for reporting this issue. Working on it...

raandree commented 3 years ago

This is a bug in AlphaFS which I cannot solve quickly. The issue is tracked in alphaleonis/AlphaFS#460.

nashiooka commented 3 years ago

Thanks for all your hard work. I've actually been using the module for years. Don't rush on my account. The permission stuff is working fine I use it all the time. I only encountered the error in a script where I was expecting paths >260 chars. I went back to the core *item cmdlets but using the \\UNC\... syntax.

rao-source commented 3 years ago

Hello Raimund, Thank you for this module it's excellent. I hit an error similar to previously described but on 7Zip files and on "My Data Sources" directory:

`get-childitem2 : (3) The target directory is a file, not a directory: [\[COMPUTER]\C$\Users[USER]\AppData\Local\WebEx\wbxcache\mticket_7z] At \[COMPUTER][MYSCRIPT].ps1:48 char:21 ... countPath = get-childitem2 -Path $stPath -Recurse -Hidden -System -fo ...


     CategoryInfo          : NotSpecified: (\\[COMPUTER]\C$\...ache\mticket_7z:String) [Get-ChildItem2], DirectoryNotFoundException
     FullyQualifiedErrorId : DirUnspecifiedError,NTFSSecurity.GetChildItem2`

The full command I'm using to obtain the path items:

`$userAccountPath = get-childitem2 -Path "\\[remotecomputer]\C$\Users\[username]" -Recurse -Hidden -System -force -SkipMountPoints -SkipSymbolicLinks`

The items in that path WebEx\wbxcache are definitely 7Zip files, but when do basic get-childitem on same path shows as Directories.

Same error in a different location:
Path:
`\\[remotecomputer]\[userprofile]\My Documents\My Data Sources`
Directory Outline of "My Documents" showing that directory's attributes:

`d---s-        2020-02-06   9:40 AM                My Data Sources`

Hope it helps.
raoue commented 2 years ago

I'm receiving a similar error: Commands: Get-ChildItem2 -Path "\\ServerShare\User Path" -Recurse Remove-Item2 : (3) The target directory is a file, not a directory

Get-ChildItem2 : (3) The target directory is a file, not a directory: [\\ServerShare\User Path\My Documents\My Pictures]
At line:4428 char:33
+ ... tPath = Get-ChildItem2 -Path "\\ServerShare\User Path" -Recurse
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\ServerShare\User Path\...\My Pictures:String) [Get-ChildItem2], 
DirectoryNotFoundException
    + FullyQualifiedErrorId : DirUnspecifiedError,NTFSSecurity.GetChildItem2

This is a repeatable error. PSVersion: 5.1.19041.1645 NTFSSecurity Version: 4.2.6

When I perform a Get-Item on same path:

PSPath               : Microsoft.PowerShell.Core\FileSystem::\\ServerShare\User Path\My Documents\My Pictures
PSParentPath         : Microsoft.PowerShell.Core\FileSystem::\\ServerShare\User Path\My Documents
PSChildName          : My Pictures
PSProvider           : Microsoft.PowerShell.Core\FileSystem
PSIsContainer        : True
Mode                 : d-r---
BaseName             : My Pictures
Target               :
LinkType             :
Owner                : UNDISCLOSED
IsInheritanceBlocked : False
Name                 : My Pictures
FullName             : \\ServerShare\User Path\My Documents\My Pictures
Parent               : My Documents
Exists               : True
Root                 : \\ServerShare
Extension            :
CreationTime         : 2021-12-02 6:56:05 AM
CreationTimeUtc      : 2021-12-02 1:56:05 PM
LastAccessTime       : 2022-04-22 1:23:53 PM
LastAccessTimeUtc    : 2022-04-22 7:23:53 PM
LastWriteTime        : 2022-04-22 1:23:53 PM
LastWriteTimeUtc     : 2022-04-22 7:23:53 PM
Attributes           : ReadOnly, Directory

It has a single file in that path that has a "," comma as the first character in the filename.

Hope this helps.

Sup3rlativ3 commented 2 years ago

Hi @raoue, I believe that the error is outside the control of this project as per raandree's comment