2011-02-14 2 views
0

나는 시도했지만 어떤 결과를 다시 얻지 못했습니다 내가 잘하고 있는지 확실하지 않습니다. 내가이 시도 사전필터링 및 공유 목록 powershell 함께 항목을

[DateTime] $CreatedDate = $item["Created"] 
$convertedCreatedDate = $CreatedDate.ToString("yyyy-MM-dd") 
$today = (Get-Date).AddDays(-1).ToString("yyyy-MM-dd") 

foreach ($item in $list.items | where {$convertedCreatedDate -eq $today}) { 

    if ($list.items | where {$convertedCreatedDate -eq $today}) 
    { 
     Write-Host $item["Created"] 
    } 

    Write-Host $item["Created"] 
} 

답변

2

당신은 당신의 foreach에 복잡한 표현을 사용할 수 있습니다. 나는 조금 더 읽기 코드를 만들기 위해 @()에 포장 것이며, 결과가 배열 (중 길이가 0, 1 또는 n) 예를 보장하기 위해 :

foreach ($item in @($list.items | where {$convertedCreatedDate -eq $today})) { 

당신은 또한 당신이 날짜 테스트하고 단순화 할 수 있습니다 날짜 시간 등의 Date 속성을 사용하여 :

$convertedCreatedDate = ([DateTime]$item["Created"]).Date 
$today = (Get-Date).Date 

당신은 또한 if 문 상태에서 복잡한 표현을 넣을 수 있지만 PowerShell은 단지 문이 $true 또는 $false 여부를 평가합니다. PowerShell은 다음과 같은 것을 시도하려고 많은 강요를합니다 :

$list.items | where {$convertedCreatedDate -eq $today} 

그리고이를 부울로 변환하십시오. 본질적으로 파이프 라인이 비어 있지 않은 결과로 평가되면 결과는 $true이고 그렇지 않으면 $false입니다. 이것은 아마도 당신이 의도 한 것이 아닙니다.

+0

감사합니다. 키스. 나는 SPQuery와 caml을 사용하여 작업을 완료했습니다. – naijacoder

2

에 감사의 foreach 또는 내 if 문에서 필터링 할 수 있습니다 위하고있는 것처럼

$today=(Get-Date).AddDays(-1).ToString("yyyy-MM-dd") 

foreach ($item in $list.items) { 
    [DateTime]$CreatedDate=$item["Created"] 
    $convertedCreatedDate=$CreatedDate.ToString("yyyy-MM-dd") 
    if ($convertedCreatedDate -eq $today) { 
     Write-Host $item["Created"] 
    } 
} 
관련 문제