powershell 在本地AD中大量编辑属性

inkz8wg9  于 2023-03-12  发布在  Shell
关注(0)|答案(1)|浏览(141)

我正在尝试查找一个PowerShell脚本,该脚本可以为大量用户更新AD中的标题属性。我希望找到一个脚本,该脚本可以从csv文件导入更改,并仅为列表中的用户更新属性。我找到了以下脚本,但显然它仅适用于Azure AD,我需要它为当地的广告。也许有人更开关比我可以帮我修改下面的脚本。

#Import Active Directory module
Import-Module ActiveDirectory 

#Import CSV File to set variable for the user’s logon name + update data + delimiter
$Users = Import-CSV -Delimiter ";" -Path "c:\psscripts\users.csv"

#Using your code to filter AD Sam Accounts listed CSVData is listed with the information you wish to update

Foreach($user in $users){
    
    #Using your code to filter AD Sam Accounts Based on column samaccountname in the csv file
    Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" | Set-ADUSer `
        -title $($User.Title)`
}
doinxwow

doinxwow1#

除了一些变量一致性和缺乏检查之外,这段代码很好,并且确实针对本地AD,尽管如果您只是使用标准csv文件,那么使用该分隔符可能不常见。(通常是电子邮件地址)和“标题”,然后将文件另存为csv,下面修改的代码应该为您工作。添加了测试空白标题的逻辑,因为你不能给属性赋一个空值。

#Import Active Directory module
Import-Module ActiveDirectory 

#Import CSV File with AD SAM account and Title data from users.csv in the C:\psscripts directory of your computer
$Users = Import-CSV -Path "c:\psscripts\users.csv" | Where {$_}

#Filter AD Sam Accounts listed in CSV and update title for listed accounts

Foreach($user in $Users){
    #Check for value of $user.Title in case of null value
    If ($user.Title){
        #Filter AD Sam Accounts Based on column SamAccountName in the csv file and update the account Title field
        Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" | Set-ADUSer -Title $($user.Title)
    }
    else {
        #Filter AD Sam Accounts Based on column SamAccountName in the csv file and clear the account Title field
        Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" | Set-ADUSer -clear -Title
    }
}

我建议在你的实际列表上做一个或两个测试用户账户之前先测试一下。不用说,你需要作为一个域账户登录到PS会话中,并拥有足够的特权来在运行脚本时对账户进行更改。VSStudioCode是一个很好的工作环境,你可以作为提升的账户启动程序(shift +右键单击程序图标,选择以其他用户身份运行),以沙箱保护您在VS Studio代码中正在处理的权限。
如果你尝试在Azure AD中工作,则需要添加这些行并在Azure中批准你的帐户访问请求,以实际成功运行脚本,具体取决于你的租户设置。根据租户配置,在混合AD/Azure AD环境中可能需要执行此操作,而不管你是否打算应用于本地AD。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
Select-MgProfile -Name "beta"

最好的问候,没有给予或暗示的保证,请接受作为答案,如果这对你的作品。

相关问题