0

I'm trying to find old files on my servers and having a little trouble with the drive path for the Get-NeglectedFiles function. My serverpath keeps showing up as \server\ .

Function Get-NeglectedFiles

{

 Param([string[]]$path,

       [int]$numberDays)

 $cutOffDate = (Get-Date).AddDays(-$numberDays)

 Get-ChildItem -Path $path -Recurse |

 Where-Object {$_.LastAccessTime -le $cutOffDate}

}


$Endresults = @()
$serverlist = get-content "C:\temp\serverlist.txt"


foreach($server in $serverlist) {
    $results = Get-WmiObject -ComputerName $Server -Class Win32_Share | select name

    $Endresults += New-Object psobject -Property @{
        Servername = $server
        Result = $results
        }
    }
foreach($drive in $server){
    $drives = $results | Where-Object { $_ -ne $null}
    $serverpath = "\\" + $server + "\" + $drives + "\"
}
    {Get-NeglectedFiles -path $serverpath -numberDays 90 | select name, lastaccesstime
}
ctw
  • 35
  • 5
  • I not sure if it has anything to do with it, but [`$null` should be at the left side of the comparison operator](https://stackoverflow.com/a/60996703/1701026) – iRon Dec 11 '20 at 19:32

1 Answers1

1

You're probably looking to do something like this (I've simplified it a bit but you can extend on it):

$serverlist = Get-Content 'C:\temp\serverlist.txt';
foreach ($server in $serverlist) {
    $drives = Get-WmiObject -ComputerName $Server -Class Win32_Share;

    foreach ($drive in $drives.Name) {
        $serverpath = "\\$server\$drive\";
        $serverpath;

        Get-NeglectedFiles -path $serverpath -numberDays 90 | select Name, LastAccessTime;
    };
};

Explanation:

  • Get list of server names from file serverlist.txt
  • For each server in that list:
    • Retrieve the list of share names on that server
      • For each share on that server generate a serverpath and run Get-NeglectedFiles

Side note:

You also should probably inspect what is being returned by:

Get-WmiObject -ComputerName $Server -Class Win32_Share

And make sure that all of the shares returned are ones you want to use. For example, when I run it, I get shares like IPC$, print$, ADMIN$, as well as the default drive shares, and all other custom shares that have been created on the server. You probably aren't going to be cleaning files out of those.

Another side note:

You might want to consider using the -File parameter inside of your Get-NeglectedFiles command so that you are only targeting files and not directories.

Chad Baldwin
  • 2,239
  • 18
  • 32
  • That works perfectly! One last question, do you know how to use Export-Excel to list each server on a different sheet? – ctw Dec 11 '20 at 20:28
  • Also it doesn't seem to want me to be able to use -Exclude on those drive letters: $serverlist = Get-Content 'C:\temp\serverlist.txt'; foreach ($server in $serverlist) { $drives = Get-WmiObject -ComputerName $Server -Class Win32_Share | Where-Object { $_.Name -notlike 'C$','ADMIN$','IPC$','print$'} foreach ($drive in $drives.Name) { $serverpath = "\\$server\$drive\"; $serverpath; Get-NeglectedFiles -path $serverpath -numberDays 365 | select Name, LastAccessTime, LastWriteTime, Directory | Export-Excel -Path $PWD -AutoFilter }; }; – ctw Dec 14 '20 at 21:27
  • 1
    @ctw `-notlike` doesn't accept an array of items, try changing it to `-notin` – Chad Baldwin Dec 14 '20 at 21:43
  • That did the trick, also I figured out the Export-Excel issue: Get-NeglectedFiles -path $serverpath -numberDays 365 | select Name, LastAccessTime, LastWriteTime, Directory | Export-Excel -WorksheetName "$serverpath" -Path "C:\temp\OldFiles($server).xlsx" -AutoFilter – ctw Dec 14 '20 at 22:04