Test shared file path and check access for shared file path using power shell script

1. Create folder - "Logs" - file - TestPath.logs
 
2. Create .csv file for input - testPath.csv , column name should be - "FileSharePath"
 
3. Create .ps1 file and write below script
 
4. It will give output csv file with columns FileShrePath, Status, Date, Time 
  1. #Input file should be .csv file format, and column name should be - "FileSharePath"  
  2.  
  3. #############################################################################  
  4.  
  5. #Provide the input and output file path  
  6. $CSVPath ="D:\TestPath.csv"  
  7. $Logfile = "D:\Logs\TestPath.log"    
  8. $outputFilePath = "D:\TestPath_result1.csv"  
  9.  
  10. #############################################################################  
  11.   
  12. $data = @()  
  13. $status = ""  
  14.  
  15. #checking given input .csv path is correct or not  
  16. if (Test-Path $CSVPath)  
  17.   {  
  18.         $csvContent = Import-Csv -Path $CSVPath   
  19.           
  20.     foreach($row in $csvContent)  
  21.     {     
  22.         if (Test-Path $row.FileSharePath)  
  23.             {    
  24.                 #Write-Host $row.FileSharePath "is a valid path"  -foregroundcolor "Green"  
  25.                 #LogWrite $row.FileSharePath "is a valid path"  
  26.   
  27.                 try  
  28.                 {  
  29.                     if(ACL $row.FileSharePath)  
  30.   
  31.                      {  Write-Host $row.FileSharePath " - Access is there" -foregroundcolor "Green"  
  32.                         LogWrite ($row.FileSharePath +" - Access is there")  
  33.                         $status = "Access is there"  
  34.                          
  35.                      }  
  36.                 }  
  37.                 catch  
  38.                 {  
  39.                     Write-Host $row.FileSharePath " - Access Denied" -foregroundcolor "Red"  
  40.                     LogWrite ($row.FileSharePath +" - Access Denied")  
  41.                     $status = "Access Denied"  
  42.                      
  43.                 }                  
  44.             }   
  45.         else   
  46.         {  
  47.            Write-Host $row.FileSharePath " - invalid path"  -foregroundcolor "Red"  
  48.            LogWrite ($row.FileSharePath +" - invalid path")  
  49.             $status = "invalid path"  
  50.             
  51.                
  52.         }  
  53.          
  54.   
  55.     $object = New-Object -TypeName PSObject   
  56.     $object | Add-Member -MemberType NoteProperty -Name 'FileSharePath' -Value $row.FileSharePath  
  57.     $object | Add-Member -MemberType NoteProperty -Name 'Status' -Value $status  
  58.     $object | Add-Member -MemberType NoteProperty -Name 'Date' -Value (get-date -Format dd-MM-yyyy)  
  59.     $object | Add-Member -MemberType NoteProperty -Name 'Time' -Value (get-date -Format HH:mm:ss)  
  60.       
  61.     $data += $object  
  62.                  
  63.   
  64.     }  
  65.      
  66.          #Export to CSV file  
  67.          $data | Export-Csv $outputFilePath -NoTypeInformation -Append  
  68.          Write-Host ""  
  69.         Write-Host "Output file is availabe - " $outputFilePath -foregroundcolor "Yellow"  
  70.   
  71.        Read-Host -Prompt "Please press enter to exit..."   
  72.         
  73.   }  
  74.     
  75.   else  
  76.   {  
  77.      Write-Host $CSVPath " - invalid input .csv file path"  -foregroundcolor "Red"  
  78.            LogWrite ($row.FileSharePath +" - invalid input .csv file path")  
  79.   }  
  80.   
  81.     
  82.       
  83.  function LogWrite  
  84.   {  
  85.       param([string]$logstring)  
  86.   
  87.       $dateTime = get-date -format G  
  88.      #Write-Host " "  
  89.       #Write-Host $dateTime $logstring  
  90.       Add-Content $Logfile -Value " "  
  91.       Add-Content $Logfile -Value ( $dateTime  + $logstring)  
  92.   
  93.     }