数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件
来源:互联网 发布:ip端口打开失败 编辑:程序博客网 时间:2024/06/09 17:32
原文译自:
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脚本的步骤,可以加些判断到上面的语句中,对接近和高于阈值时做对应的处理:
- 数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件
- 如何使用PowerShell查找占用磁盘空间最大的文件
- 深入浅出PowerShell—使用变量
- 使用PowerShell脚本更新数据库
- 深入浅出SharePoint—使用PowerShell导出站点的Navigation
- SharePoint 数据库管理-PowerShell
- 深入浅出PowerShell——入门
- 队列——PowerShell版
- 如何运行PowerShell的脚本文件
- 图形界面的 PowerShell 脚本
- 深入浅出Powershell——拷贝GAC中dll文件
- 使用PowerShell下载文件
- [11月26日的脚本] 使用PowerShell列举数据库中未被使用的内容数据库
- 使用SharePoint PowerShell 找出 In Progress 状态的Item
- powershell远程管理服务器磁盘空间
- PowerShell与cmd的对比点评(一)——文件定位和操作
- [3月7日的脚本] 如何移除磁盘空间不足警告 (PowerShell)
- 发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner
- Sphere SUBLEX
- Buffalo WHR-G300N V2刷DD-WRT+Wiwiz全过程记录
- 使用hibernate的自动生成表的时候注意事项
- 小问题 大知识
- 黑马程序员-单例设计模式
- 数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件
- 标准模板库STL中vector使用参考手册
- c++builder中声明全局变量的两个办法
- Lumia 820:诺基亚告诉您11件事
- ruby异常处理
- 多个结构体的函数应用
- hdu1853最小费用最大流
- 一个经典的面试题目:宏替换
- 2440学习(三)——搭建交叉编译环境