Adam Lane
Adam Lane

Reputation: 31

Auditing Permissions with Powershell

A little background first: I've been asked as part of a Scrum team at work to prepare a Powershell script that will collate a list of SMB Shares that have been flagged as safe to archive from the CSV files that were given to me nested in a folder structure (each share equates to one CSV file, with a list of files within, but the only relevance for this is that they are titled SHARENAME.csv), and the current permissions of each, in case they need to be restored at a later date.

At the moment, I have the following for the first step (nice and easy):

get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"

This gives me a single CSV file with the names of all of the 188 shares in a single column. The second step, exporting a CSV file for each with a list of permissions, is not going as well. So far I have the following:

$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
ForEach ($share in $share) {
get-smbshareaccess -name $share -cimsession euukpopdfs005
}

Obviously at the moment there's no export-csv command in there, but essentially I'll want the script to use the same share name that it calls with 'get-smbshareaccess -name' as the file name for the CSV file. Unfortunately I hit 188 errors in the following format at this point:

get-smbshareaccess : euukpopdfs005: No MSFT_SMBShare objects found with property 'Name' equal to '@{BaseName=Brand}'.  Verify the value of the property and retry.
At line:3 char:1
+ get-smbshareaccess -name $share -cimsession euukpopdfs005
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{BaseName=Brand}:String) [Get-SmbShareAccess], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_Name,Get-SmbShareAccess
    + PSComputerName        : euukpopdfs005

So rather than calling the BaseName (for instance "Brand" in this first case), it's calling (@{BaseName=Brand}:String), which it then doesn't find anything for.

Is there anyway I can tidy this up? Ideally in some way that will work for both the Get-SmbShareAccess command and the export-csv command, but I'm not against doing a little more jiggery pokery as needed.

This is my first post, so I apologise if this is too long, lacking in detail, etc etc. Let me know if you need anything else! Thanks in advance.


Final (working) code after suggestions:

    get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"
$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
$share | Format-List
ForEach ($item in $share) {
get-smbshareaccess -name $item.BaseName -cimsession euukpopdfs005
}

Upvotes: 1

Views: 1040

Answers (1)

Adam Lane
Adam Lane

Reputation: 31

Final working code (Thanks everyone!):

get-childitem -path 'C:\users\adam.lane\desktop\_with adam\_remove' | select BaseName | export-csv -path "..\test.csv"
$share = Import-Csv -path 'C:\users\adam.lane\desktop\_with adam\test.csv' -Header 'BaseName'
$share | Format-List
ForEach ($item in $share) {
get-smbshareaccess -name $item.BaseName -cimsession euukpopdfs005
}

Upvotes: 2

Related Questions