Не удалось получить powershell для возврата туда, где результаты GCI с использованием ACL

507
Rossaluss

Я пытаюсь заставить Powershell перечислять файлы в каталоге, которые старше определенной даты и соответствуют определенному пользователю. На данный момент у меня есть скрипт ниже, который дает мне все файлы старше определенной даты, перечисляет каталог и кому он принадлежит:

$date=get-date  $age=$date.AddDays(-30)  ls '\\server\share\folder' -File -Recurse | ` where {$_.lastwritetime -lt "$age"} | ` select-object $_.fullname,{(Get-ACL $_.FullName).Owner} | ` ft -AutoSize 

Однако, когда я пытаюсь использовать дополнительный параметр where, чтобы выбрать только файлы, принадлежащие определенному пользователю, я не получаю никаких результатов, даже если знаю, что должен, основываясь на совпадении, которое пытаюсь получить (как показано ниже):

$date=get-date  $age=$date.AddDays(-30)  ls '\\server\share\folder' -File -Recurse | ` where ({$_.lastwritetime -lt "$age"} -and {{(get-acl $_.FullName).owner} -eq "domain\user"}) | ` select-object $_.fullname,{(Get-ACL $_.FullName).Owner} | ` ft -AutoSize 

Я что-то пропустил? Могу ли я не использовать команду get-acl в условии where, как я пытался?

Любая помощь будет оценена.

Спасибо

0

1 ответ на вопрос

0
Tim Ferrill

This appears to work.

# Get the full list of files ls '\\server\share\folder' -File -Recurse | # Limit to files with the right age and owner where {($_.lastwritetime -lt "$age") -and ((get-acl $_.FullName).owner -eq "domain\user")} | # Add an Owner column to the object ForEach-Object {$_ | Add-Member -type NoteProperty -name Owner -value (Get-ACL $_.FullName).Owner -PassThru} | # Get just the filename and the owner select-object fullname, owner | # Format the output ft -AutoSize 

Also, a couple of tips.

  • You had used the escape character at the end of each line. The pipe character will let you carry over to the next line, so there was no need for the escape.
  • Also, Where-Object uses { and } to define the script block. Grouping conditions within the script block can be done using ( and ).
Ты гений. Огромное спасибо. Я не знал о команде add-member. Кроме того, спасибо за другие советы, я сейчас приведу в порядок другие мои скрипты :) Rossaluss 9 лет назад 0