728x90
Get-DriveSpace.ps1
List the percentage of free disk space for multiple computers.
# Display the drive space on all drives
# if any have < 20% free space, log to a file for review
function DriveSpace { param( [string] $strComputer) "$strComputer ---- Free Space (percentage) ----" # Does the server responds to a ping (otherwise the WMI queries will fail) $query = "select * from win32_pingstatus where address = '$strComputer'" $result = Get-WmiObject -query $query if ($result.protocoladdress) { # Get the Disks for this computer $colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3" # For each disk calculate the free space foreach ($disk in $colDisks) { if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))} else {$PercentFree = 0} $Drive = $disk.DeviceID "$strComputer - $Drive - $PercentFree" # if < 20% free space, log to a file if ($PercentFree -le 20) {"$strComputer - $Drive - $PercentFree" | out-file -append -filepath "C:\logs\Drive Space.txt"} } } } foreach ($computer in cat C:\batch\servers.txt) {DriveSpace "$computer"}
This assumes you have saved a list of computernames to check in the file 'servers.txt'
Example
Assuming the script above is saved in the current directory as Get-DriveSpace.ps1:
PS C:\> ./Get-DriveSpace
728x90
'IT이야기 > Powershell' 카테고리의 다른 글
Where-Object (0) | 2018.12.13 |
---|---|
Powershell – Remoting (원격 접속) (0) | 2016.12.20 |
원격 서버 이벤트로그를 파일로 저장 (0) | 2016.02.15 |
디스크 사용가능한 퍼센테이지 쿼리 파워쉘 (0) | 2016.02.15 |
CSV 파일을 이용한 Foreach Query (0) | 2016.02.05 |