读书人

数据库管理Powershell运用Pow

发布时间: 2012-10-13 11:38:17 作者: rapoo

数据库管理——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:

数据库管理——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是不区分大小写,但是还是建议使用规范化的编码格式:

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

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

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

结果如下:

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

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

Get-ChildItem C:\SP2 -recurse

得到一下结果:

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

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


Get-ChildItem E:\DB\*.* -include *.ldf,*.mdf | select name,length -last 8

得到以下结果:

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


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

读书人网 >其他数据库

热点推荐