我在运行此脚本时遇到错误。我正在运行-whatif并得到此错误:
我收到这些错误
TerminatingError(Set-ADUser): "Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'EmployeeID'. Specified method is not supported."
csv文件看起来像这样
employeeID,name
78549625,ktest
43939399,dtest
这是脚本:
$stuff = Import-Csv c:\temp\empid\finalexport_test.csv
$empid = $stuff.employeeid
$userid = $stuff.name
$empid
$userid
foreach ($user in $userid)
{
Set-ADUser -Identity $user -EmployeeID $empid -whatif
$empid
$user
Write "The user $user is now assigned the employee Id of $empid. This was not actually written to AD yet"
write "The information below shows the user and the feild that would have been updated."
Get-ADUser -identity $user -Property * | select samaccountname, employeeid
}
答案 0 :(得分:0)
因为你正在使用For-each循环错误。
你在userid上做了一个foreach,因为那里有多个值,就像userid empid也有多个值(是一个数组) 所以我建议将脚本更改为:
(你也错过了foreach循环末尾的括号)
$stuff = Import-Csv c:\temp\empid\finalexport_test.csv
foreach ($user in $stuff)
{
$empid = $user.employeeID
$userid = $user.name
Set-ADUser -Identity $userid -EmployeeID $empid -whatif
$empid
$user
Write "The user $userid is now assigned the employee Id of $empid. This was not actually written to AD yet"
write "The information below shows the user and the feild that would have been updated."
}
Get-ADUser -identity $user -Property * | select samaccountname, employeeid