您的位置:首页 > 运维架构 > Shell

数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件

2015-03-06 12:02 691 查看
原文:

数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件

原文译自:

http://www.mssqltips.com/sqlservertip/2774/powershell-script-to-find-files-that-are-consuming-the-most-disk-space/?utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=2012923

说明一下,CSDN的编辑功能相当的烂,把我的脚本都搞得乱七八糟,看的辛苦请莫见怪。

在平时的备份过程中,或多或少会遇到空间不足的问题,为了预防这种情况,可以做定期检查磁盘空间的操作,但是由于纯SQL语句比较难实现,所以可以借助Powershell来实现这类功能,在此,使用Get-ChileItem:

语法:

Get-ChildItem [[-path] ] [[-filter] ] [-include ] [-exclude ] [-name] [-recurse] [-force]

[CommonParameters]

首先打开Powershell,注意,本文通过两种方式来打开Powershell:



为了得到Get-ChildItem更多的信息,可以在Powershell中执行以下语句:

## for detailed information

get-help Get-ChildItem -detailed

## For technical information, type:

get-help Get-ChildItem -full

首先先来看看Get-ChildItem的一些例子:

在第一个例子中,先查询当前目录下的文件和文件夹列表,虽然Powershell是不区分大小写,但是还是建议使用规范化的编码格式:



第二个例子:根据名字降序排序:

Get-ChildItem C:\Python27 | sort-Object -property name -Descending

结果如下:



第三个例子:使用–recurse参数文件夹的内容及其子文件夹:

Get-ChildItem C:\SP2 -recurse

得到一下结果:



你可以使用-include/-exclude参数来查找或者排除特定条件文件。可以使用-first[number of rows](从上到下)来限定输出的行数。或者使用-last[number of rows](从下到上)参数来限定。

Get-ChildItem E:\DB\*.* -include *.ldf,*.mdf | select name,length -last 8
得到以下结果:



可以使用where-object cmdlet来查找基于特定条件的信息。Where-object子句后面需要跟着curly braces {}中并以$_前缀开头。Powershell使用以下操作符来实现对比:

-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
-eq Equal to
-ne Not equal to
-like uses wildcards for pattern matching

Get-ChildItem E:\DB\*.* -include *.mdf | where-object {$_.name -like "T*"}

由于我创建了一个test库,所以以T开头,得到以下结果:



言归正传:

可以使用下面的脚本来查找大文件,在脚本中,必须定义$path(用于指定路径)、$size(用于限制查找的大小)、$limit(用于限制行数)和$Extension(用于限定文件扩展名)的值。

在本例中,与原文有点出入,改为我本地的目录和文件名。查找E:\DB及其子目录下,文件大于1M的,后缀名为mdf的前五个文件。

##Mention the path to search the files

$path = "E:\"

##Find out the files greater than equal to below mentioned size

$size = 1MB

##Limit the number of rows

$limit = 5

##Find out the specific extension file

$Extension = "*.mdf"

##script to find out the files based on the above input

$largeSizefiles = get-ChildItem -path $path -recurse -ErrorAction "SilentlyContinue" -include $Extension | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt $size} | sort-Object -property length -Descending | Select-Object Name, @{Name="SizeInMB";Expression={$_.Length
/ 1MB}},@{Name="Path";Expression={$_.directory}} -first $limit

$largeSizefiles
得到以下结果:



可以把脚本存为filename.ps1。然后在Powershell中使用./运行即可,如下:

注意,由于win7默认禁止执行,所以第一次执行的时候会显式红字的错误,可以按截图中的步骤更改后再执行即可。



也可以使用Export-Csv把文件导出成csv来查看:

##Mention the path to search the files
$path = "E:\"
##Find out the files greater than equal to below mentioned size
$size = 1MB
##Limit the number of rows
$limit = 5
##Find out the specific extension file
$Extension = "*.mdf"
##script to find out the files based on the above input
$largeSizefiles = get-ChildItem -path $path -recurse -ErrorAction "SilentlyContinue" -include $Extension | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt $size} | sort-Object -property length -Descending | Select-Object Name, @{Name="SizeInMB";Expression={$_.Length / 1MB}},@{Name="Path";Expression={$_.directory}} -first $limit
$largeSizefiles |Export-Csv c:\lsfreport.csv


执行脚本后,C盘会出现lsfreport.csv的文件。剩下的,你懂得。

2008的作业中有执行Powershell脚本的步骤,可以加些判断到上面的语句中,对接近和高于阈值时做对应的处理:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: