2015-04-02 55 views
0

Basicly我想找到不活動的用戶,以便用在搜索基礎foreach循環,POWERSHELL - 使用數組在foreach的樣子,看着一個OU中的用戶 -

IM現在我試着去impliment看着幾個OU在開始的時候就是一個陣容。

原來這就是我試圖...

[email protected]("OU Path 1", 
     "OU Path 2", 
     "OU Path 3") 

$OU | ForEach ($user in (Get-ADUser -SearchBase $_ -filter {(lastlogondate -notlike "*" -OR lastlogondate -le $90days) -AND (passwordlastset -le $90days) -AND (enabled -eq $True)} -Properties lastlogondate, passwordlastset | Select-Object name, lastlogondate, passwordlastset, samaccountname)) 
{ 

.... 

,並得到錯誤....

At line:18 char:22 + $OU | ForEach ($user in (Get-ADUser -SearchBase $_ -filter {(lastlogondate -notl ... + ~~ Unexpected token 'in' in expression or statement. At line:18 char:21 + $OU | ForEach ($user in (Get-ADUser -SearchBase $_ -filter {(lastlogondate -notl ... + ~ Missing closing ')' in expression. At line:18 char:293 + ... samaccountname)) + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

這工作得很好

$SearchBase1 = "OU1" 
$SearchBase2 = "OU2" 
$SearchBase3 = "OU3" 


ForEach ($user in(Get-ADUser -SearchBase $SearchBase1 -filter {(lastlogondate -notlike "*" -OR lastlogondate -le $90days) -AND (passwordlastset -le $90days) -AND (enabled -eq $True)} -Properties lastlogondate, passwordlastset | Select-Object name, lastlogondate, passwordlastset, samaccountname)) 
{ 

回答

1

的問題是,你試圖創建一個嵌套的ForEach循環,並忘記了循環的第一部分。在這裏,您想將$OU管道放入ForEach循環中,並在該ForEach循環內根據第一個ForEach循環的當前對象創建另一個循環。

$OU | ForEach{ 
    ForEach ($user in (Get-ADUser -SearchBase $_ -filter {(lastlogondate -notlike "*" -OR lastlogondate -le $90days) -AND (passwordlastset -le $90days) -AND (enabled -eq $True)} -Properties lastlogondate, passwordlastset | Select-Object name, lastlogondate, passwordlastset, samaccountname)) 
    { 
     <Do Stuff> 
    } #End inner ForEach Loop for current OU 
} 
相關問題