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

简介: 原文: 数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件 原文译自: http://www.
原文: 数据库管理——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脚本的步骤,可以加些判断到上面的语句中,对接近和高于阈值时做对应的处理:



目录
相关文章
|
23天前
|
监控 关系型数据库 数据库
OceanBase数据库常见问题之文件存在但是数据库提示文件不存在如何解决
OceanBase 是一款由阿里巴巴集团研发的企业级分布式关系型数据库,它具有高可用、高性能、可水平扩展等特点。以下是OceanBase 数据库使用过程中可能遇到的一些常见问题及其解答的汇总,以帮助用户更好地理解和使用这款数据库产品。
|
1月前
|
存储 关系型数据库 MySQL
如何处理爬取到的数据,例如存储到数据库或文件中?
【2月更文挑战第23天】【2月更文挑战第73篇】如何处理爬取到的数据,例如存储到数据库或文件中?
|
2月前
|
存储 安全 Windows
PowerShell系列(六):PowerShell脚本执行策略梳理
【2月更文挑战第1篇】PowerShell 脚本执行策略用于控制何时以及何种方式执行 PowerShell 脚
|
29天前
|
SQL Java 数据库连接
从来没想到我们会扒拉nohup文件去找我们想要的数据,然后往数据库中添加。。。...
从来没想到我们会扒拉nohup文件去找我们想要的数据,然后往数据库中添加。。。...
17 0
|
3月前
|
数据库 数据安全/隐私保护 Python
写一个定时备份数据库的脚本,且只保留最近3天
写一个定时备份数据库的脚本,且只保留最近3天
67 3
|
29天前
|
SQL Java 数据库连接
springboot解析txt文件顺便加到数据库中(nohup文件)
springboot解析txt文件顺便加到数据库中(nohup文件)
108 1
|
29天前
|
SQL 编解码 数据库
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
12 1
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
|
1月前
|
Java 关系型数据库 MySQL
Java调用shell脚本实现数据库备份功能
本篇文章主要介绍怎样使用Java程序,执行服务器上的数据库备份Shell脚本进行MySQL数据库的备份功能。
|
3月前
|
关系型数据库 MySQL Shell
linux shell脚本实现自动备份Mysql数据库脚本
linux shell脚本实现自动备份Mysql数据库脚本
182 3
|
3月前
|
运维 开发工具 Windows
PowerShell系列(五):PowerShell通过脚本方式运行笔记
【1月更文挑战第7天】方便迁移,比如在之前工作经验积累下来的运维脚本,可以保存下来。如果业务场景用的到的话,直接文件拷贝过来就可以运行。