1. 程式人生 > >資料庫管理——Powershell——使用Powershell指令碼找出消耗最多磁碟空間的檔案

資料庫管理——Powershell——使用Powershell指令碼找出消耗最多磁碟空間的檔案

原文譯自:

 說明一下,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指令碼的步驟,可以加些判斷到上面的語句中,對接近和高於閾值時做對應的處理: