Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get Share Path

$
0
0

Does anyone know how I can edit this code I found on the Internet to get the path for a share using the Get-WmiObject-ClassWin32_LogicalShareSecuritySetting.  Currently it provides a path that is not easily traced to the drive letter and folder - such as "\\ServerName\root\cimv2:Win32_LogicalShareSecuritySetting.Name=""FolderName""". 

I'd like the output for the path to be "D:\FolderName" for example..... Thank you.

 ////////////////////////////////////////////////////////////////////

 

Param
(
 [Parameter(Mandatory=$false)]
 [Alias('Computer')][String[]]$ComputerName=$Env:COMPUTERNAME,

 [Parameter(Mandatory=$false)]
 [Alias('NTFS')][Switch]$NTFSPermission
)


$RecordErrorAction = $ErrorActionPreference
#change the error action temporarily
$ErrorActionPreference = "SilentlyContinue"


Function GetSharedFolderPermission($ComputerName)
{
    #test server connectivity
 $PingResult = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
 if($PingResult)
 {
  #check the credential whether trigger
  if($Credential)
  {
   $SharedFolderSecs = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -ComputerName $ComputerName -Credential $Credential -ErrorAction SilentlyContinue
  }
  else
  {
   $SharedFolderSecs = Get-WmiObject -Class Win32_LogicalShareSecuritySetting -ComputerName $ComputerName -ErrorAction SilentlyContinue
           
     }
  
  foreach ($SharedFolderSec in $SharedFolderSecs)
  {
      $Objs = @() #define the empty array
   
                   
                  $ACLS = $SharedFolderSec.GetSecurityDescriptor().Descriptor.DACL
                
                   
                         foreach($ACL in $ACLS)
                   { 
                $User = $ACL.Trustee.Name
                                                if(!($user)){$user = $ACL.Trustee.SID}
                                                $Domain = $ACL.Trustee.Domain
                                                switch($ACL.AccessMask)
                                                {
                                                    2032127 {$Perm = "Full Control"}
                                                    1245631 {$Perm = "Change"}
                                                    1179817 {$Perm = "Read"}
                                                }
       
                              


    #customize the property

           

    $Properties = @{'ComputerName' = $ComputerName
        #'ConnectionStatus' = "Success"
        'SharedFolderName' = $SharedFolderSec.Name
                                'SharedFolderPath' = $SharedFolderSec.Path
         #'SecurityPrincipal' = $UserName
                                 'SecurityPrincipal' = "$Domain\$User"
         #'FileSystemRights' = [Security.AccessControl.FileSystemRights]`
                                 'FileSystemRights' = $Perm}
         #$($DACL.AccessMask -as [Security.AccessControl.FileSystemRights])
         #'AccessControlType' = [Security.AccessControl.AceType]$DACL.AceType
                                
    $SharedACLs = New-Object -TypeName PSObject -Property $Properties
    $Objs += $SharedACLs

         }

   $Objs |
           |
            Select-Object ComputerName,SharedFolderName,SharedFolderPath,SecurityPrincipal,FileSystemRights -Unique
          
     } 
 }

 else
 {
  $Properties = @{'ComputerName' = $ComputerName
      #'ConnectionStatus' = "Fail"
      'SharedFolderName' = "Not Available"
                        'SharedFolderPath' = "Not Available"
      'SecurityPrincipal' = "Not Available"
      'FileSystemRights' = "Not Available"
      #'AccessControlType' = "Not Available"
                        }
  $SharedACLs = New-Object -TypeName PSObject -Property $Properties
  $Objs += $SharedACLs
  $Objs|Select-Object ComputerName,SharedFolderName,SharedFolderPath,SecurityPrincipal, `
  FileSystemRights
 }
}


foreach($CN in $ComputerName)
{
 
 if($NTFSPermission)
 {
  GetSharedFolderNTFSPermission -ComputerName $CN
 }
 else
 {
  GetSharedFolderPermission -ComputerName $CN
 }
}
#restore the error action
$ErrorActionPreference = $RecordErrorAction


Viewing all articles
Browse latest Browse all 6937

Trending Articles