Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.

PowerSQL – Find Size, Name, LastAccess and Last Modified time of all bak files across ALL Servers.

We came across a scenario where database bak files being created from long time which are no more in use and hence cleanup those files since we’ve backup tool is in place. We’ve many *.BAK (database backup files) created across ‘N’ number of servers. We have decided to cleanup all *.bak files .  First we’ve listed all *.bak files  and validated it once and make sure which one is eligible for deletion. In this post, I’m just doing information gathering. To get a remote drive location, I’ve referred GetUNCpath function which is already posted in one of my previous blog.

The below code has three parts
1) Get the UNC path of each drive of a any server – get remote drive location
2) Test-Connection with the server – check for successful connection or not
3) Browse through each drive – Recursive search

Note:-

1)Input file =List.txt
2)Input Parameter = Extension (Any file extension BAK,SQL,PS1 etc:- In this case its BAK)
3)OutputFile =FileList.txt

The Server names are included in list.txt
aqvd002612
aqdb1812

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

FUNCTION getUNCPath($infile)
{
$qualifier = Split-Path $infile -qualifier
$drive = $qualifier.substring(0,1)
$noqualifier = Split-Path $infile -noQualifier
“$drive`$$noqualifier”
}

FUNCTION Get-Files
{
Param([String]$extn)
FOREACH ($server in Get-Content List.txt)
{
if (Test-Connection -ComputerName $Server -Count 2 -Quiet )
{
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3}|select deviceid
foreach ($item in $dp)
{
$UNC=getUNCPath($item.deviceid)
$Path=”\\$Server\$UNC”
Write-host $path
Get-childitem $path -RECURSE –FORCE -ERRORACTION silentlycontinue *.$extn |select-object Name,DirectoryName,@{Name=”Size(MB)”;Expression={[math]::round($_.Length/1024/1024,3)}}, CreationTime,LastAccessTime,LastWriteTime |out-file -append c:\FileList.txt
}
}
else
{
Write-Warning “$Server seems dead not pinging”
}
}
}

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

The Function calling code is given below

PS C:\> Get-Files -extn bak

Output – FileList.txt

Name : MMetaService_3-27-2013.bak
DirectoryName : \\aqdb1812\C$
Size(MB) : 5.156
CreationTime : 3/27/2013 10:37:26 PM
LastAccessTime : 4/4/2013 10:26:12 AM
LastWriteTime : 3/27/2013 10:37:26 PM

Name : WSS_Content_3-5-2013.bak
DirectoryName : \\aqdb1812\C$
Size(MB) : 161.166
CreationTime : 3/15/2013 2:08:25 PM
LastAccessTime : 4/29/2013 2:03:31 PM
LastWriteTime : 3/16/2013 1:59:26 AM

Invalid ServerName in List.txt

PS C:\> Get-Files -extn bak
WARNING: aqvd002612 seems dead not pinging


Comments

Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...