locked
how logs all access denied with and path name too long into a file.Log RRS feed

  • Question

  • hello,

    can some one help me, i have made a script that get all informations (size, fullpath, ...) about directorys and subdirectorys and files, and also the big file in all tree of the start path

    i need to log the output "denied access or path too long" into the file log.txt, if you have any idea how can i do it please ??

    the script :

    ********


    $P = Read-Host saisir votre chemin 
    $taille = Read-Host taille anormale en XXMB


    Get-ChildItem -force $P -Recurse | ? { ( $_.PSIsContainer ) } | 

    foreach {

    Get-ChildItem $_.FullName -Force -File | sort LastWriteTime -Descending | Select-Object  fullName, @{Name=”Size”;Expression={ '{0:N2} MB' -f ($_.Length / 1MB)}}, lastwritetime


    $size = 0
    Get-ChildItem -recurse -force $_.fullname -ErrorAction SilentlyContinue | foreach { $size += $_.length }

    $Gdir =$_.FullName, '{0:N2} MB' -f ($size / 1MB)
    $filebig= Get-ChildItem $P -Force -File -Recurse | Where-Object { $_.Length -gt $taille } | Select-Object fullname, @{Name="SizeInMB"; Expression={ '{0:N2} MB' -f ($_.Length / 1MB)}}

    Write-Host global size directory: "<<" $Gdir ">>" `n 


    Write-Host Big file: "<<" $filebig ` ">>"

    Monday, September 23, 2019 9:46 AM

All replies

  • In Windows 10 and later you need to be sure long path name is turned on. There is no other way except converting the name to its device equivalent but you can only do that if you already know the name.

    https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/


    \_(ツ)_/

    Monday, September 23, 2019 9:55 AM
  • hi Anrck, 

    I see you pb. You would like to do a tree-size in a script but you are facing problems with Acces denied and path too long, exact ?

    Have you tested the Try-catch-finally parameter in you script ? Im' thinking the solution is in this way.

    Just one link, but there are many others : Powershell-exceptions-everything-you-ever-wanted-to-know

    you have already identified the 2 types of problem you may be facing. For each error, you get a typical error message. This is these types you must catch and manage in try-catch

    Oliv

    Tuesday, September 24, 2019 7:32 AM
  • thank you for your request, but i'm trying to put all message denied into a file.log to see exactly what i have as output  
    Monday, September 30, 2019 8:55 AM
  • Hello Oliv.

    it's exactly what i need, but i don't know how can i manage all errors, by identified each one and put a message in outpu with the name of folder/file, and text "denied access".

    thank you in advance if you have helping me !

    Script

    ************************************

    $P = 'C:\Users\acherkao\Desktop\outiles'

    try {
    Get-ChildItem -force $P -Recurse | ? { ( $_.PSIsContainer ) } | 


    foreach {

    Get-ChildItem $_.FullName -Force -File | Select-Object  Name, @{Name=”Size”;Expression={ '{0:N2} MB' -f ($_.Length / 1MB)}}


    $size = 0
    Get-ChildItem -recurse -force $_.fullname -ErrorAction SilentlyContinue | foreach { $size += $_.length }

    $_.FullName, '{0:N2} MB' -f ($size / 1MB)

    }
    }
    catch{

    Write-Host denied access to $_.FullName
    }

    $files = Get-ChildItem $P -Force -Recurse |Where-Object { $_.Length -gt 20MB } | Select-Object Name,@{Name="size";Expression={ ' {0:N2} MB' -f ($_.Length / 1MB)}}
    Write-Host `n******* Taille des fichiers supérieur à 20MB ********
    Write-Host $files
    Write-Host `n******* LastModificationTime **********
    $lastmodifile = Get-ChildItem $P -Force -Recurse | sort LastWriteTime -Descending | select-object fullname,lastwritetime | select -first 1
    Write-Host $lastmodifile

    Write-Host `n***** liste des contenus des réps ********* 

    Monday, September 30, 2019 9:17 AM