Test-Path / System.IO.Directory::Exists not working as expected

假装没事ソ 提交于 2019-12-12 16:33:09

问题


I'm having trouble using the Powershell CmdLet Test-Path.

I created a Share on a Server (\\Servername\MyShare$), which allows one Group of users to just create a file and append data, while others users can just read and delete these files. The first group is not allowed to do anything else, including Read-Permissions, Read-Attributes and so on.

If I do a Test-Path on this folder as member of the "drop files only" group, it returns false. Even [System.IO.Directory]::Exists() returns false.

I am searching for a way to test if a directory exists without the need to have Read-Permissions or Read-Attributes rights. Any ideas?

Is this the correct behaviour of those two methods?

Regards, teamoo


回答1:


Ok, if nobody want to venture ...

Here is a solution :

I'am not allowed to access to directory .\f1\f2 and .\f1\f3 does not exists.

PS C:\Silogix> Get-ChildItem .\f1\f2 -ErrorAction silentlycontinue
PS C:\Silogix> $Error[0].exception
L'accès au chemin d'accès 'C:\Silogix\f1\f2' est refusé.
PS C:\Temp\Temp> $Error[0].exception.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     UnauthorizedAccessException              System.SystemException

PS C:\Silogix> Get-ChildItem .\f1\f3 -ErrorAction silentlycontinue
PS C:\Silogix> $Error[0].exception.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ItemNotFoundException

So if you test $Error[0].exception.gettype() after the Get-Item you can understand why it's not accessible, so if it exists or not. You can call that "bricolage", but it's also a solution. Sorry for the french in the example.



来源:https://stackoverflow.com/questions/5632908/test-path-system-io-directoryexists-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!