日期:2014-05-16  浏览次数:20432 次

数据库管理——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的前五个文件。


友情链接: 爱易网 云虚拟主机技术 云服务器技术 程序设计技术 开发网站 APP开发教程
Copyright © 2013-2024 爱易网页 当前在线:519人  网站在8时5分1秒内访问总人数:81549人 当前 16.77%  粤ICP备18100884号-2