CamFlyerCH / FileShareUtils

Powershell module to help with all file server tasks without using WMI
MIT License
12 stars 3 forks source link

Get-NetShares requires admin rights on target machine #6

Closed Alufolie303 closed 4 years ago

Alufolie303 commented 4 years ago

The Get-NetShares function uses the NetShareEnum with 502 level hardcoded to request share information from the smb server. This information level requires the executing user to be a admin on the server. So default user gets the following error:

PS C:\Windows\system32> Get-NetShares -Server server.fqdn
Error during NetShareEnum: Access is denied
At C:\Program Files\WindowsPowerShell\Modules\FileShareUtils\1.0.19\FileShareUtils.psm1:595 char:16
+ ...             Throw ("Error during NetShareEnum: " + (Get-NetAPIReturnI ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Error during Ne...ccess is denied:String) [], RuntimeException
    + FullyQualifiedErrorId : Error during NetShareEnum: Access is denied

Sadly the microsoft docs doesn't cover the required priviledges for the information levels. Some short tests showed the following: 0/1 - user (or anonymous?) 2 / 502 / 502 - admin

For i.e. filer and share discovery use cases the user usually doesn't have this rights. So a parameter to control the level would be a helpful feature.

The quick and dirty fix to get arround this is to change the information level in the following lines of the FileShareUtils Powershell module: C:\Program Files\WindowsPowerShell\Modules\FileShareUtils\1.0.19\FileShareUtils.psm1

588:         $struct = New-Object Netapi+SHARE_INFO_1
592:            $return = [Netapi]::NetShareEnum($Server,1,[ref]$buffer,-1,[ref]$entries, [ref]$total,[ref]$handle) 
CamFlyerCH commented 4 years ago

I just implemented the Level parameter to Get-NetShares and am now able to get the share list of a Windows 10 Home Edition, where I was previously not able to do so. @Alufolie303 : Could you manually install this version and test it out before I push it to the PowerShell Gallery ?

CamFlyerCH commented 4 years ago

I released and published version 1.0.22 that solves this issue.