Advertisement
opexxx

Get-NetworkShares.ps1

Mar 8th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [cmdletbinding()]
  2. param(
  3.     [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
  4.     [string[]]$ComputerName = $env:computername,
  5.     [string]$OutputDir
  6. )
  7.  
  8. begin {$OutputArray = @()}
  9.  
  10. process {
  11.     foreach($Computer in $ComputerName) {
  12.         $OutputObj = New-Object –TypeName PSObject –Prop (@{
  13.                 'ComputerName'=$Computer.ToUpper();
  14.                 'ShareName' = $null;
  15.                 'Status'=$null;
  16.                 'EveryOneFullControl'=$false
  17.             })
  18.    
  19.    
  20.         Write-Verbose "Working on $Computer"
  21.         if(!(Test-Connection -Computer $Computer -Count 1 -Ea 0)) {
  22.             Write-Verbose "$Computer is offline"
  23.             $OutputObj = New-Object –TypeName PSObject –Prop (@{
  24.                 'ComputerName'=$Computer.ToUpper();
  25.                 'ShareName' = $null;
  26.                 'Status'="OFFLINE";
  27.                 'EveryOneFullControl'=$null
  28.             })
  29.             $OutputObj
  30.             $OutputArray +=$OutputObj
  31.            
  32.             Continue
  33.         }
  34.        
  35.         try {
  36.             $Shares = Get-WmiObject -Class Win32_LogicalShareSecuritySetting `
  37.                                     -ComputerName $Computer `
  38.                                     -ErrorAction Stop
  39.             $Status = "Successful"                     
  40.         } catch {
  41.             Write-Verbose "Failed to Query WMI class. More details: $_"
  42.             $OutputObj = New-Object –TypeName PSObject –Prop (@{
  43.                 'ComputerName'=$Computer.ToUpper();
  44.                 'ShareName' = $null;
  45.                 'Status'="WMIFailed";
  46.                 'EveryOneFullControl'=$null
  47.             })
  48.            
  49.             $OutputObj
  50.             $OutputArray +=$OutputObj
  51.            
  52.             Continue
  53.         }
  54.        
  55.         foreach($Share in $Shares) {
  56.             $OutputObj = New-Object –TypeName PSObject –Prop (@{
  57.                 'ComputerName'=$Computer.ToUpper();
  58.                 'ShareName' = $Share.Name;
  59.                 'Status'=$Status;
  60.                 'EveryOneFullControl'=$false
  61.             })
  62.             $OutputObj.ShareName = $Share.Name
  63.             $Permissions = $Share.GetSecurityDescriptor()
  64.             foreach($perm in $Permissions.Descriptor.DACL) {
  65.                 if($Perm.Trustee.Name -eq "EveryOne" -and $Perm.AccessMask -eq "2032127" -and $Perm.AceType -eq 0) {
  66.                     $OutputObj.EveryOneFullControl = $true
  67.                 } else {
  68.                
  69.                 }
  70.             }
  71.         $OutputObj
  72.         $OutputArray +=$OutputObj
  73.         }
  74.     }
  75.    
  76. }
  77.  
  78.  
  79. end {
  80.     if($OutputDir) {
  81.         $File = Join-Path $OutputDir ("SharePermissions {0}.log" -f $(Get-Date -Format("MMddyyyyHHmmss")))
  82.         $File
  83.         $OutputArray | ? {$_.EveryOneFullControl}| % {
  84.             "\\{0}\{1}" -f $_.ComputerName, $_.ShareName | Out-File -FilePath $File -Force
  85.         }
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement