Problems enumerating get-smbshareaccess

9 views
Skip to first unread message

Mike Leone

unread,
Aug 18, 2025, 1:26:40 PMAug 18
to NTPowershell Mailing List
OK, I'll admit I'm having a bad day. I have a server with a lot of share. I want to list each share, with all the accesses.

PS C:\PHA Scripts> get-smbshare -name "Whitehall" | Get-SMBShareAccess

Name      ScopeName AccountName                      AccessControlType AccessRight
----      --------- -----------                      ----------------- -----------
Whitehall NT_SAN1   NT AUTHORITY\Authenticated Users Allow             Full
Whitehall NT_SAN1   PHA.PHILA.GOV\Domain Admins      Allow             Full

(don't yell at me, I didn't create the share)

Anyway, what I assume I have to do is store the above returned 2 entries, then loop through and write out each entry as 1 line in a CSV. (eventually, I will create new shares elsewhere, using said CSV).

$Share = Get-SMBShare -Name "Whitehall"
$SharePath = $Share.Path
$ShareAcces = get-smbshareaccess -name "Whitehall"
ForEach ($Entry in $ShareAcces)
{
$ShareName = $ShareAcces.Name
$ShareAccountName = $ShareAcces.AccountName
$ShareAccessControltype = $ShareAcces.AccessControlType
$ShareAccessRight = $ShareAcces.AccessRight
Write-Host "Share Name = " $ShareName
Write-Host "Share Path = " $SharePath
Write-Host "ShareAcctname = " $ShareAcctName
Write-Host "ShareAccessControl = " $ShareAccessControlType
Write-Host "ShareRight = " $ShareAccessRight
}

PS C:\PHA Scripts> .\Save-The-Shares-V2.PS1
Share Name                      =  Whitehall Whitehall
Share Path                      =  G:\departments\conv sites\Whitehall Apts
ShareAcctname           =
ShareAccessControl      =  Allow Allow
ShareRight                      =  Full Full
Share Name                      =  Whitehall Whitehall
Share Path                      =  G:\departments\conv sites\Whitehall Apts
ShareAcctname           =
ShareAccessControl      =  Allow Allow
ShareRight                      =  Full Full

What am I missing here? Why are my ShareName, ShareAccessControl and ShareRight all being duplicated? I am looping through each returned object.

I know it's something simple, but I just can't see it ...



Michael B. Smith

unread,
Aug 18, 2025, 2:24:17 PMAug 18
to ntpowe...@googlegroups.com
Because instead of looking at $Entry, which is a singleton, you are looking at $ShareAcces, which is a collection.


From: ntpowe...@googlegroups.com <ntpowe...@googlegroups.com> on behalf of Mike Leone <tur...@mike-leone.com>
Sent: Monday, August 18, 2025 1:26:25 PM
To: NTPowershell Mailing List <ntpowe...@googlegroups.com>
Subject: [ntpowershell] Problems enumerating get-smbshareaccess
 
--
You received this message because you are subscribed to the Google Groups "ntpowershell" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntpowershell...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ntpowershell/CAHBr%2B%2BjWjgiSx4jz4n0%3DKr1nnygbqtnCRawda0Ht3PMnnsZjTQ%40mail.gmail.com.

Mike Leone

unread,
Aug 18, 2025, 2:37:12 PMAug 18
to ntpowe...@googlegroups.com
On Mon, Aug 18, 2025 at 2:24 PM Michael B. Smith <mic...@smithcons.com> wrote:
Because instead of looking at $Entry, which is a singleton, you are looking at $ShareAcces, which is a collection.

<DUH>

<SIGH> Yes, I'm sure you're right. I'll try that in a bit.

I knew it had to be something simple. But when you try and solve 3-4 things at once, things slip through the cracks.
 


--

Mike. Leone, <mailto:tur...@mike-leone.com>

PGP Fingerprint: 0AA8 DC47 CB63 AE3F C739 6BF9 9AB4 1EF6 5AA5 BCDF
Photo Gallery: <http://www.flickr.com/photos/mikeleonephotos>

Mike Leone

unread,
Aug 18, 2025, 2:53:12 PMAug 18
to ntpowe...@googlegroups.com
On Mon, Aug 18, 2025 at 2:36 PM Mike Leone <tur...@mike-leone.com> wrote:
On Mon, Aug 18, 2025 at 2:24 PM Michael B. Smith <mic...@smithcons.com> wrote:
Because instead of looking at $Entry, which is a singleton, you are looking at $ShareAcces, which is a collection.

<DUH>

<SIGH> Yes, I'm sure you're right. I'll try that in a bit.

I knew it had to be something simple. But when you try and solve 3-4 things at once, things slip through the cracks.

Yep, that's what it was. Sorry for the bother, and thanks.

Michael B. Smith

unread,
Aug 18, 2025, 2:57:15 PMAug 18
to ntpowe...@googlegroups.com

Here. And I fixed it so it also works with admin shares.

 

Param

(

    [String] $shareNm = 'Whitehall'

)

 

$Share = Get-SMBShare -Name $shareNm

$SharePath = $Share.Path

$ShareAcces = $Share | Get-SMBShareAccess

ForEach ($Entry in $ShareAcces)

{

    $ShareName = $Entry.Name

    $ShareAcctName = $Entry.AccountName

    $ShareAccessControltype = $Entry.AccessControlType

    $ShareAccessRight = $Entry.AccessRight

Reply all
Reply to author
Forward
0 new messages