Frage

I am trying to create a report of files that are one day old, I currently have a script to include filename and date:

get-childitem -Path \\UNC_PATH_TO_FILE
         | where-object {$_.lastwritetime -gt (get-date).addDays(-1)}
         | Foreach-Object { $_.Name, $_.CreationTime }

Previously I was exporting this to a file for each UNC path, however this has now grown and will result in 22 separate files to be read.

I want to consolidate this into a CSV file which can contain a coll for Server, Filename ($_.Name) and Date ($_.CreationTime).

This is outside of my Powershell realm and cannot find anything to really help me. Would someone be able to offer some assistance?

War es hilfreich?

Lösung

You could try this:

$paths = "\\server1\unc\path", "\\server1\unc\path" 
Get-ChildItem -Path $paths
         | Where-Object {$_.lastwritetime -gt (Get-Date).addDays(-1)}
         | Select-Object @{n="Server";e={([uri]$_.FullName).Host}},
                         @{n="Filename";e={$_.Name}},
                         @{n="Date";e={$_.CreationTime}},
                         @{n="FileSize(MB)";e={[Math]::Round($_.Length/1MB,3)}}
         | Export-Csv test.csv -NoTypeInformation

It extracts the servername from the UNC path and renames the other properties with the names you specified.

Andere Tipps

Here's an example that creates a new object for each file:

Get-ChildItem -Path \\UNC_PATH_TO_FILE |
Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | 
ForEach-Object { 
    New-Object PSObject -Property @{
        Server = $_.FullName -replace '^\\\\([\w]+)\\.+$','$1'
        Name = $_.Name
        CreationTime = $_.CreationTime    
    }
} | Export-Csv -Path files.csv -NoTypeInformation

The following should work:

get-childitem -Path \\UNC_PATH_TO_FILE
         | where { $_.lastwritetime -gt (get-date).AddDays(-1)} 
         | foreach-object { $env.COMPUTERNAME,$_.Name, $_.CreationTime}
         | Export-Csv -Path foo.txt -NoTypeInformation
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top