Powershell Get File/Disk Size
知识点:
1、获取路径中的文件夹:Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object
2、获取文件夹的总大小Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum
3、 正则表达式:$DBname= $fullname -replace "^.*data\\" 、
文档:\\localhost\D$\WENDY1\DBDATA\WENDY-MBX
匹配:WENDY-MBX
$startFolders="FOLDERPATH1","FOLDERPATH2" foreach ($startFolder in $startFolders) { $colItems = (Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object) foreach ($i in $colItems) { $subFolderItems = (Get-ChildItem $i.FullName -recurse | Measure-Object -property length -sum) $fullname=$i.FullName $SERVER=$fullname -replace "WENDY-MBX.*" $DBname= $fullname -replace "^.*data\\" $size=("{0:N2}" -f ($subFolderItems.sum / 1GB)) } }
$size=("{0:N2}" -f ($subFolderItems.sum / 1GB)) 保留小数点后两位
$size=[math]::truncate($disk.size/1GB) 截取小数点,保留整数
$date=get-date).TOSTRING("yyyy-dd-MM") $servers= get-adcomputer -Filter {Name -Like "GAGA*" -or Name -Like "WENDY*" } -SearchScope Subtree -SearchBase "DC=WENDY,DC=CORP,DC=COM" foreach ($server in $servers) { $servername=$server.name $disks =Get-WmiObject Win32_LogicalDisk -ComputerName $servername -Filter "DriveType='3'" forEach ($disk in $disks) { $DE=$disk.DeviceID $size=[math]::truncate($disk.size/1GB) $space=[math]::truncate($disk.freespace/1GB) } }