2013-12-09 48 views
1

我對PowerShell相當陌生,目前我正在使用它來解決Helpdesk的一些管理任務。

我嘗試基於導入的CSV中的對象的屬性來移動AD對象(如果以下術語的使用不正確,請原諒我)的問題。

的CSV是: 的UserPrincipalName,UserToAccess,DaysToLive

喬@ company.com,戴夫@ company.com,90

等等...

我然後傳遞數組通過foreach循環移動AD帳戶:

foreach ($line in $import) {Get-ADUser -filter {userPrincipalName -eq $_.UserToAccess} -SearchBase "DistinguishedName of OU" | Move-ADObject -TargetPath 'DistinguishedName of OU'} 

隨後我收到以下錯誤:

Get-ADUser : Variable: '' found in expression: $.UserToAccess is not defined. At D:\jason\EnableArchiveAccess.ps1:17 char:29 + foreach ($line in $import) {Get-ADUser -filter {userPrincipalName -eq $.UserToA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Variable: '' found in expression: $_.UserToAccess is not defined.,Microsoft.ActiveDirec
tory.Management.Commands.GetADUser

我已經能夠使用上述邏輯來取消隱藏用戶從GAL和我已檢查數組和屬性是那裏作爲noteproperties。

我認爲這是因爲我在命令中使用的不是AD變量,但任何幫助都將非常感謝,但如果我儘快找到答案,我會回覆。

回答

4

只是看着,我認爲你需要改變

$_.UserToAccess 

$line.UserToAccess 
1

另一種方法是:

$import | foreach{ 
    Get-ADUser -filter {userPrincipalName -eq $_.UserToAccess} ` 
    -SearchBase "DistinguishedName of OU" ` 
    | Move-ADObject -TargetPath 'DistinguishedName of OU'} 
0

這是常見的mixups之一在PowerShell中。實際上有兩個foreach「關鍵字」。一個是在Foreach對象cmdlet的別名,並且被用作例如:

$Items = 1,2,3 
$Items | foreach { $_ } 

在該示例中的$_意味着當前對象。第一次是1,第二次是2,第三次是3。

第二的foreach是關鍵字,並且被用作例如

$Items = 1,2,3 
foreach ($item in $items) { 
    $item 
} 

在這個例子中$item表示當前對象。

因此在您的示例中,您必須使用$list而不是$_