2015-01-05 2 views
0

파일 기반 LastWriteTime 속성 my 스크립트는 사용자 지정 출력이있는 'File Age'테이블을 만듭니다. 스크립트가 잘 작동, 내 문제는 특정 칼럼 '나이'의 출력으로 나는 그 것처럼 내가 명령을 설정합니다. 이미 "Sort-Object -descending"cmdlet을 사용하려고했지만 성공하지 않으면 데이터가 임의로 배치됩니다. 그걸 사용자 정의 할 수있는 또 다른 방법이 있을까요? 그런특정 열의 데이터 순서 변경

내 원하는 출력 모양 : 어떤 도움을 환영 할 것이다

$scan = gci c:\MyFolder 
$scan | Add-Member ScriptProperty -Name FileAgeDays -Value { 
      [int]((Get-Date) - ($this.LastWriteTime)).TotalDays} 
$scan | Add-Member ScriptProperty -Name FileAge -Value { 

if ($this.FileAgeDays -le 7) { 
    "1 Day - 1 Week" 
} 
elseif ($this.FileAgeDays -ge 7 -and $this.FileAgeDays -le 30) { 
    "1 Week - 1 Month" 
} 
elseif ($this.FileAgeDays -ge 30 -and $this.FileAgeDays -le 180) { 
    "1 Month - 6 Months" 
} 
elseif ($this.FileAgeDays -ge 180 -and $this.FileAgeDays -le 365) { 
    "6 Months - 1 Year" 
} 
elseif ($this.FileAgeDays -ge 365 -and $this.FileAgeDays -le 730) { 
    "1 Year - 2 Years" 
} 
elseif ($this.FileAgeDays -ge 730) { 
    "< 2 Years" 
} 
} 
$grp = $scan | ?{ ! $_.PSIsContainer } | Group FileAge | 
      Add-Member -MemberType ScriptProperty -Name SizeMB -Value { 
      ($this.Group | Measure-Object Length -sum).Sum/1MB} -PassThru 

$age_frag = $grp | select` 
@{l='Size (MB)';e={"{0:n}" -f ($_.sizemb)}},` 
@{l='Age';e={$_.name}}, Count| 
ConvertTo-Html -Fragment -As Table -PreContent "<br><H2><a name=a009>File Age</a></H2>"| 
Out-String 

:

Age 
1 Day - 1 Week 
1 Week - 1 Month 
1 Month - 6 Months 
6 Months - 1 Year 
1 Year - 2 Years 
< 2 Years 

가 여기 내 스크립트입니다.

답변

2

스위치를 사용하여 text-value를 정렬 가능한 데이터로 바꿀 수 있습니다 (예 : 정수.

샘플 데이터 :

이제
$data = [pscustomobject]@{ ID=1; FileAge="1 Day - 1 Week"}, 
[pscustomobject]@{ ID=4; FileAge="6 Months - 1 Year"}, 
[pscustomobject]@{ ID=2; FileAge="1 Week - 1 Month"}, 
[pscustomobject]@{ ID=5; FileAge="1 Year - 2 Years"}, 
[pscustomobject]@{ ID=6; FileAge="< 2 Years"}, 
[pscustomobject]@{ ID=3; FileAge="1 Month - 6 Months"} 

$data 

ID FileAge   
-- -------   
1 1 Day - 1 Week  
4 6 Months - 1 Year 
2 1 Week - 1 Month 
5 1 Year - 2 Years 
6 < 2 Years   
3 1 Month - 6 Months 

, 현실을 분류하자

$data | Sort-Object { 
    switch($_.FileAge) { 
    "1 Day - 1 Week" { 1 } 
    "1 Week - 1 Month" { 2 } 
    "1 Month - 6 Months" { 3 } 
    "6 Months - 1 Year" { 4 } 
    "1 Year - 2 Years" { 5 } 
    "< 2 Years" { 6 } 
    } 
} 

ID FileAge   
-- -------   
1 1 Day - 1 Week  
2 1 Week - 1 Month 
3 1 Month - 6 Months 
4 6 Months - 1 Year 
5 1 Year - 2 Years 
6 < 2 Years 

개인적으로, 나는 다르게 스크립트를 설계하는 것입니다. 안된입니다 여기 : 나는 .. 1, 2, 3 같은 정수 값으로 FileAgeGroup -property을 추가 한으로 분류하고 사용자 친화적 인 출력 genereate하는 Select-Object을 사용하는 것입니다 :

$data | Sort-Object ID | Select-Object @{n="FileAgeName";e={ 
    switch($_.ID) { 
    1 { "1 Day - 1 Week" } 
    2 { "1 Week - 1 Month" } 
    3 { "1 Month - 6 Months" } 
    4 { "6 Months - 1 Year" } 
    5 { "1 Year - 2 Years" } 
    6 { "< 2 Years" } 
    } 
}} 

FileAgeName 
----------- 
1 Day - 1 Week 
1 Week - 1 Month 
1 Month - 6 Months 
6 Months - 1 Year 
1 Year - 2 Years 
< 2 Years 

UPDATE를 내 첫 번째 솔루션이 구현 된 샘플의 수정 된 버전 (코드를 다시 작성하지 않고 정렬 만). 추가 된 코드 위에 주석을 추가했습니다.

$scan = gci c:\MyFolder 
$scan | Add-Member ScriptProperty -Name FileAgeDays -Value { 
      [int]((Get-Date) - ($this.LastWriteTime)).TotalDays} 
$scan | Add-Member ScriptProperty -Name FileAge -Value { 

if ($this.FileAgeDays -le 7) { 
    "1 Day - 1 Week" 
} 
elseif ($this.FileAgeDays -ge 7 -and $this.FileAgeDays -le 30) { 
    "1 Week - 1 Month" 
} 
elseif ($this.FileAgeDays -ge 30 -and $this.FileAgeDays -le 180) { 
    "1 Month - 6 Months" 
} 
elseif ($this.FileAgeDays -ge 180 -and $this.FileAgeDays -le 365) { 
    "6 Months - 1 Year" 
} 
elseif ($this.FileAgeDays -ge 365 -and $this.FileAgeDays -le 730) { 
    "1 Year - 2 Years" 
} 
elseif ($this.FileAgeDays -ge 730) { 
    "< 2 Years" 
} 
} 
$grp = $scan | ?{ ! $_.PSIsContainer } | Group FileAge | 
      Add-Member -MemberType ScriptProperty -Name SizeMB -Value { 
      ($this.Group | Measure-Object Length -sum).Sum/1MB} -PassThru 

#Scriptblock that can be used to sort by FileAge (Name-property in a Group) 
$fileageSorting = { 
    switch($_.Name) { 
    "1 Day - 1 Week" { 1 } 
    "1 Week - 1 Month" { 2 } 
    "1 Month - 6 Months" { 3 } 
    "6 Months - 1 Year" { 4 } 
    "1 Year - 2 Years" { 5 } 
    "< 2 Years" { 6 } 
    } 
} 

$age_frag = $grp | 
#Sorting 
Sort-Object $fileageSorting | 
select @{l='Size (MB)';e={"{0:n}" -f ($_.sizemb)}}, @{l='Age';e={$_.name}}, Count | 
ConvertTo-Html -Fragment -As Table -PreContent "<br><H2><a name=a009>File Age</a></H2>"| 
Out-String 
+0

안녕하세요. Frode 재생 해 주셔서 감사합니다. 주 스크립트에서 어떻게 구현할 수 있는지 잘 모르겠습니까? if 루프에서 어떻게 설정할 수 있습니까? – kekimian

+0

테스트되지 않은 샘플의 업데이트 된 답변보기. –

+0

큰 도움을 주셔서 감사합니다. – kekimian