gpt4 book ai didi

csv - 如何处理自动重复用户删除

转载 作者:行者123 更新时间:2023-12-03 09:13:27 24 4
gpt4 key购买 nike

我有以下内容:

@(Import-Csv C:\Users\Administrator\Desktop\dbs\Monday.csv) +
@(Import-Csv C:\Users\Administrator\Desktop\dbs\Tuesday.csv) +
@(Import-Csv C:\Users\Administrator\Desktop\dbs\Wednesday.csv) +
@(Import-Csv C:\Users\Administrator\Desktop\dbs\Thursday.csv) +
@(Import-Csv C:\Users\Administrator\Desktop\dbs\Friday.csv) |
sort first_name,last_name,phone1 -Unique |
Export-Csv C:\Users\Administrator\Desktop\dbs\joined.csv

Import-Module ActiveDirectory

#EDIT PATH SO IT POINTS TO DB FILE \/
$newUserList = Import-Csv C:\Users\Administrator\Desktop\dbs\joined.csv

ForEach ($item in $newUserList){
$fname = $($item.first_name)
$lname = $($item.last_name)
$phone = $($item.phone1)

$username=$fname+$lname.substring(0,1)

# Puts Domain name into a Placeholder.
$domain='@csilab.local'

# Build the User Principal Name Username with Domain added to it
$UPN=$username+$domain

# Create the Displayname
$Name=$fname+" "+$lname

$newusers1 = (New-ADUser -GivenName $fname -Surname $lname -HomePhone $phone -Name $Name -DisplayName $Name -SamAccountName $username -AccountPassword (ConvertTo-SecureString "1NewPassword" -asplaintext -force) -ChangePasswordAtLogon $true -UserPrincipalName $UPN -Path "ou=test,dc=csi,dc=lab" -Enabled $true -PassThru) |

# I need this block to check for duplicates missed by the csv sort & merge
# as well as any in the destination OU itself as the script will run daily
# with inevitable possibility that user is unique to the csv but not the AD.
$newusers1 | Get-ADUser -Filter * -SearchBase "OU=Active Users,DC=csilab,DC=local" |
Sort-Object -Unique |
Remove-ADUser -confirm:$false

但是我运行它并得到:

Get-ADUser : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At C:\Users\Administrator\Desktop\Team2.ps1:40 char:14
+ $newusers1 | Get-ADUser -Filter * -SearchBase "OU=Active Users,DC=csilab,DC=loca ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (CN=Bethanie Cut...csilab,dc=local:PSObject) [Get-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我也担心即使它确实可以删除唯一用户而不是重复用户。
get-AdUser $username |  Move-ADObject -TargetPath 'OU=Active Users,dc=csilab,dc=local'  
}

我该怎么做才能确保所有用户都在那里,而不会删除任何原件,只删除重复项?

最佳答案

New-ADUser语句的末尾,您仍然有一个空管道,这将导致脚本失败,并显示“不允许使用空管道元素”错误,但是好吧...

为避免冲突,只需在尝试创建帐户之前检查该帐户是否已经存在,并仅在不存在的情况下创建它:

$username = $fname + $lname.substring(0,1)
...
if (Get-ADUser -Filter "SamAccountName -eq '$username'") {
Write-Host "Account $username already exists."
} else {
New-ADUser -SamAccountName $username -Name $Name ... -PassThru
}

另外,您也使CSV处理过于复杂。只需通过 ForEach-Object处理文件列表:
$domain = '@csilab.local'

Set-Location 'C:\Users\Administrator\Desktop\dbs'

'Monday.csv', 'Tuesday.csv', 'Wednesday.csv', 'Thursday.csv', 'Friday.csv' |
ForEach-Object { Import-Csv $_ } |
Sort-Object first_name, last_name, phone1 -Unique |
ForEach-Object {
$fname = $_.first_name
$lname = $_.last_name
$phone = $_.phone1
...
}

关于csv - 如何处理自动重复用户删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34089070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com