2014-10-01 1 views
0

필자는 csv 파일에서이 새로운 사용자를 AD에 추가하는이 powershell 스크립트를 가지고 있습니다. 내 문제는 내가 csv 파일에서 방금 추가 한 CSV 파일에서 새 사용자를 삭제할 수 있기를 원한다는 것입니다. 따라서 새 사용자는 csv 파일의 enymore에 없습니다. @ 매트 이미 질문에 대한 의견에서 지적CSV 파일에서 항목을 삭제하려면 어떻게해야합니까?

Import-Module ActiveDirectory 

# Import list of Users From CSV into $Userlist 
$userlist = Import-Csv "C:\employee.csv" 

# Step through Each Item in the List 
foreach($person in $userlist){ 

    $givenName = $person.givenName 
    Write-Output $givenName 

    $Initials = $person.Initials 
    Write-Output $Initials 

    $mail = $person.mail 
    Write-Output $mail 

    $title = $person.title 
    Write-Output $title 

    $displayName = $person.displayName 
    Write-Output $displayName 

    $sn = $person.sn 
    Write-Output $sn 

    $telephoneNumber = $person.telephoneNumber 
    Write-Output $telephoneNumber 

    $manager = $person.manager 
    Write-Output $manager 

    #Adding the new user to AD 
    New-ADUser $givenName -OtherAttributes @{givenName=$givenName;Initials=$Initials;mail=$mail;title=$title;displayName=$displayName;sn=$sn;telephoneNumber=$telephoneNumber;manager= 'CN='+$manager+',CN=Users,DC=jol,DC=local'} 


    #Delete item in csv file after adding the new user. 

} 
+0

이 응답을 기꺼이 추측 하겠지만 CSV 파일이 완료 처리되지 않고 전체 파일을 제거 할 수 있습니까? 사용자가 이미 존재하는지 확인하는 것을 볼 수 없으므로 csv에 새로운 사용자가 모두 포함되어 있다고 가정해야합니까? – Matt

+0

예,이 파일에는 모든 새 사용자 만 포함됩니다. – user2996075

+5

그런 다음 개별 사용자를 삭제할 목적은 무엇입니까? 결국 빈 파일로 끝날 것입니다. – Matt

답변

0

: 파일에 나열된 모든 사용자를 만들고 나중에 파일에서 각 사용자를 제거하면 루프가 완료된 후, 파일은 일반적으로 빈 것, 그래서 당신도 파일을 제거 할 수 있습니다.

그러나, 당신은 계정 생성은이 같은 것을 할 수있는 실패하는 기록을 보존하려는 경우 : PowerShell을 v3으로 이전 -Append 매개 변수를 지원하지 않습니다

foreach($person in $userlist){ 
    ... 
    New-ADUser $givenName -OtherAttributes @{...} -EA SilentlyContinue 
    if (-not $?) { $person | Export-Csv "C:\employee.csv" -NoType -Append } 
} 

Export-Csv 있음을. 이전 버전의 경우 ... | ConvertTo-Csv -NoType | Out-File "C:\employee.csv" -Append이 필요합니다.

+1

'if (! $?)'를 의미하지 않습니까? – TheMadTechnician

+0

@ TheMadTechnician 사실. 머리를 가져 주셔서 감사합니다. 결정된. –

0

사용자가 올바르게 작성되지 않은 경우 tmpfile을 만드는 방법은 무엇입니까? 이렇게하려면 제대로 작성되지 않은 사용자를 잡기 위해 일종의 오류 처리가 필요합니다. TRY/Catch와 함께 New-ADUser 명령을 래핑하는 것이 좋습니다.

#Before the foreach loop 
out-file -FilePath "C:\tmpEmployee.csv" -force 

    #Inside the loop - add the new user to AD using TRY/Catch for error handling 
    Try { 
     New-ADUser $givenName -OtherAttributes @{givenName=$givenName;Initials=$Initials;mail=$mail;title=$title;displayName=$displayName;sn=$sn;telephoneNumber=$telephoneNumber;manager= 'CN='+$manager+',CN=Users,DC=jol,DC=local'} 
    } 
    Catch {[Microsoft.ActiveDirectory.Management.Commands.NewADUser] 
     #additional error handling code 
     #export skipped user to tmpCSV File 
    } 

#After the foreach loop completes 

#Rename files 
Remove-Item -Path "C:\Employee.csv" -Force 
Rename-Item "C:\tmpEmployee.csv" -NewName "C:\Employee.csv" 

# 
1

당신은 당신이 뭔가를 할 수 있습니다, 그들이 만들어 그들을 제거하고 싶었 또는 경우 :

가 만들어으로 목록에서 사용자를 제거하고
$ParsedCSV = $userlist 
ForEach($Person in $UserList){ 
    Blah, blah, code and stuff to add users 
    if($?){$ParsedCSV = $ParsedCSV | Where{$_ -ne $Person} 
    $ParsedCSV | Export-CSV 'C:\employee.csv' -notype -force} 
} 

명령을 추가하는 경우 사용자는 $ ParsedCSV 배열에서 그 사람을 제거하고 기존 파일을 덮어 쓰는 CSV 파일로 출력합니다. 광고의 속도를 고려할 때 파일 작성 및 잠금 해제가 모든 경우가 아니라 대부분의 계정 생성보다 빠르게 발생해야한다고 생각합니다.

관련 문제