VMware Onyx is cool project, it converts mouse click actions to power shell script for automation. I tested with it and found it very useful.
You can download and participate in communities using blow link,
www.vmware.com/go/onyx
Enjoy Scripting!
Friday, July 9, 2010
Script to report the Folders not accessed for some period - PowerShell
In File servers there would be many folders which wouldn't be accessed for a long time. We can archive to slower disks/delete it to save space.
To report the folders not used for a period to a excel, we can use the below script. I've put the period as 365 days. We can edit the script even to delete.
trap{continue}
clear
$excel = new-object -comobject excel.application
$excel.visible = $false
$workbook = $excel.workbooks.add()
$sheet = $workbook.WorkSheets.Item(1)
$sheet.cells.item(1,1) = "FileName"
$sheet.cells.item(1,2) = "LastAccessDate"
$sheet.cells.item(1,3) = "Path"
$sheet.cells.item(1,4) = "Is it a FOLDER"
$today = Get-Date
$lastaccess = $today.adddays(-365)
$x = 2
$strpath = "c:\access.xls"
$1 = get-ChildItem "e:\" -Recurse | where {$_.lastaccesstime -le $lastaccess} | where {$_.psiscontainer -eq $true}
foreach ($file in $1) {
$sheet.cells.item($x,1) = $file.name
$sheet.cells.item($x,2) = $file.lastaccesstime
$sheet.cells.item($x,3) = $file.FullName
$sheet.cells.item($x,4) = $file.PSIscontainer
$x++}
$range = $sheet.usedRange
$range.EntireColumn.AutoFit()
out-null
IF(Test-Path $strPath){
Remove-Item $strPath
$Excel.ActiveWorkbook.SaveAs($strPath)}
ELSE{
$Excel.ActiveWorkbook.SaveAs($strPath)}
$excel.quit()
spps -name excel
To report the folders not used for a period to a excel, we can use the below script. I've put the period as 365 days. We can edit the script even to delete.
trap{continue}
clear
$excel = new-object -comobject excel.application
$excel.visible = $false
$workbook = $excel.workbooks.add()
$sheet = $workbook.WorkSheets.Item(1)
$sheet.cells.item(1,1) = "FileName"
$sheet.cells.item(1,2) = "LastAccessDate"
$sheet.cells.item(1,3) = "Path"
$sheet.cells.item(1,4) = "Is it a FOLDER"
$today = Get-Date
$lastaccess = $today.adddays(-365)
$x = 2
$strpath = "c:\access.xls"
$1 = get-ChildItem "e:\" -Recurse | where {$_.lastaccesstime -le $lastaccess} | where {$_.psiscontainer -eq $true}
foreach ($file in $1) {
$sheet.cells.item($x,1) = $file.name
$sheet.cells.item($x,2) = $file.lastaccesstime
$sheet.cells.item($x,3) = $file.FullName
$sheet.cells.item($x,4) = $file.PSIscontainer
$x++}
$range = $sheet.usedRange
$range.EntireColumn.AutoFit()
out-null
IF(Test-Path $strPath){
Remove-Item $strPath
$Excel.ActiveWorkbook.SaveAs($strPath)}
ELSE{
$Excel.ActiveWorkbook.SaveAs($strPath)}
$excel.quit()
spps -name excel
Subscribe to:
Posts (Atom)