Skip to content

Instantly share code, notes, and snippets.

@223n 223n/コード1.ps
Created May 24, 2017

Embed
What would you like to do?
PowerShellでフォルダ内のサイズを求めるやつ
function Get-SharedFolders
{
[CmdletBinding()]
param
(
[parameter(
position = 0,
mandatory = 1,
valuefrompipeline = 1,
valuefrompipelinebypropertyname = 1)]
[string[]]
$Path = $null
)
process
{
(net view $Path) | % { if($_.IndexOf(' Disk ') -gt 0){ $_.Split(' ')[0]; } };
}
}
function Get-DirectoryVolume
{
[CmdletBinding()]
param
(
[parameter(
position = 0,
mandatory = 1,
valuefrompipeline = 1,
valuefrompipelinebypropertyname = 1)]
[string[]]
$Path = $null,
[parameter(
position = 1,
mandatory = 0,
valuefrompipelinebypropertyname = 1)]
[validateSet("KB", "MB", "GB")]
[string]
$Scale = "KB",
[parameter(
position = 2,
mandatory = 0,
valuefrompipelinebypropertyname = 1)]
[switch]
$Recurse = $false,
[parameter(
position = 3,
mandatory = 0,
valuefrompipelinebypropertyname = 1)]
[switch]
$Ascending = $false,
[parameter(
position = 4,
mandatory = 0,
valuefrompipelinebypropertyname = 1)]
[switch]
$OmitZero
)
process
{
$path `
| %{
Write-Verbose ("Checking path : {0}. Scale : {1}. Recurse switch : {2}. Decending : {3}" -f $_, $Scale, $Recurse, !$Ascending)
if (Test-Path $_)
{
$result = Get-ChildItem -Path $_ -Recurse:$Recurse `
| where PSIsContainer `
| %{
$subFolderItems = (Get-ChildItem $_.FullName | where Length | measure Length -sum)
[PSCustomObject]@{
Fullname = $_.FullName
$scale = [decimal]("{0:N4}" -f ($subFolderItems.sum / "1{0}" -f $scale))
}} `
| sort $scale -Descending:(!$Ascending)
if ($OmitZero)
{
return $result | where $Scale -ne ([decimal]("{0:N4}" -f "0.0000"));
}
else
{
return $result;
}
}
}
}
}
$dirs = (Get-SharedFolders -Path \\fileServer1) -replace "`r", "" -split "`n";
foreach ($dir in $dirs) {
$path2 = '\\fileServer1\' + $dir
Get-DirectoryVolume -Path $path2 -Scale KB;
}
ls \\fileServer1\Documents -Recurse | Measure-Object –Property Length –Sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.