[英]PowerShell - Change attributes BULK to a list of users
I have a CSV file, containing a list of users (samaccountname) on Column A. On the additional columns i have additional attribute values i would like to set For each of these users:我有一个 CSV 文件,其中包含 A 列上的用户列表(samaccountname)。在其他列上,我有其他属性值,我想为这些用户中的每一个设置:
' OFFICE ' on Column B, ' Department ' on Column C and Company on Column D: 'OFFICE' 在 B 栏,'Department' 在 C 栏,公司在 D 栏:
Please help putting together in correct Powershell Synatx.请帮助组装正确的 Powershell Synatx。
So far i have tried the following:到目前为止,我已经尝试了以下方法:
Import-Module Activedirectory
$Attribcsv=Import-csv "D:\powershell\ADuserinformation\SetUserAttributes.csv"
ForEach ($User in $Attribcsv)
{
Set-ADUser $User.samaccountname -department $._Department
}
With a different csv with Two Column: sAMAcountName,Department使用带有两列的不同 csv:sAMAcountName,Department
But i got an error:但我有一个错误:
You can try like this (First Get the user using their SAM Account name with Get-ADUser
and then pipe to Set-ADUser
to set attribute):您可以这样尝试(首先通过Get-ADUser
使用他们的 SAM 帐户名称的用户,然后通过管道传输到Set-ADUser
以设置属性):
Import-Module Activedirectory
$Attribcsv=Import-csv "D:\powershell\ADuserinformation\SetUserAttributes.csv"
ForEach ($User in $Attribcsv)
{
Get-ADUser -Identity $User.Users | Set-ADUser -department $._Department
}
Bit late to the party, but personally I would not use the -Identity
parameter here, because if there are typos in the input CSV the script will bomb out.参加聚会有点晚了,但我个人不会在这里使用-Identity
参数,因为如果输入 CSV 中存在拼写错误,脚本将被炸毁。
A more forgiving option is to use -Filter
like this:一个更宽容的选择是使用-Filter
像这样:
Import-Module Activedirectory -ErrorAction SilentlyContinue
$AttribCsv = Import-csv "D:\powershell\ADuserinformation\SetUserAttributes.csv" | ForEach-Object {
$accountName = $_.Users # make sure the header for this column is correct here!
# now try and find a valid user object using Filter
$user = Get-ADUser -Filter "SamAccountName -eq '$accountName'" -Properties Office, Company, Department -ErrorAction SilentlyContinue
if ($user) {
$user | Set-ADUser -Department $_.Department -Company $_.Company -Office $_.Office
# or use -Replace:
# $user | Set-ADUser -Replace @{Department = $_.Department; Company = $_.Company; Office = $_.Office}
}
else {
Write-Warning "User with SamAccountName '$accountName' not found.."
}
}
Mind you that if any of the properties have a null or empty value the Set-ADUser
cmdlet will return an error.请注意,如果任何属性具有 null 或空值,则Set-ADUser
cmdlet 将返回错误。 Check your input CSV for that.检查您的输入CSV。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.