set-aduser,在Powershell脚本中一次更新几个扩展属性变量以及其他属性

时间:2018-08-15 18:52:29

标签: powershell import-csv

因此,我有一个基本脚本,可以在对值进行硬编码时使用。当文件中的值是动态的时,我需要一些帮助使其工作:

这是基本脚本:

 set-ADUser -Identity test.hsi -replace @{extensionAttribute4="LoadedFromInterface";extensionAttribute5="2";extensionAttribute6="2"} -Manager jim.james

我要执行的操作是使用Import-CSV从文件中读取,将重要的列加载到变量中,检查是否为空/空条件,然后如果变量为null则重新设置。最终执行与上述相同的操作,但带有从文件中加载的变量。 scopeionAttribute5和extensionAttribute6都是文件中的值(有时为null),管理器也是从文件中分配的变量。

Import-CSV C:\Users\user1\Documents\WTKtoAD\WTKtoAD.csv | %{$SAM = $_.SamAccountName;If ($_.PhoneTypeMobile -eq $Null) {$PhoneMobile = "NotProvided"} Else {$PhoneMobile = $_.PhoneTypeMobile};If ($_.PhoneTypeHome -eq $Null) {$PhoneHome = "NotProvided"} Else {$PhoneHome = $_.PhoneTypeHome}} | set-ADUser -Identity $SAM -Add @{extensionAttribute4="LoadedFromKronos";extensionAttribute5=$PhoneHome;extensionAttribute6=$PhoneMobile} -Manager $_.Manager

运行脚本时,我在Powershell ISE(x86)“以管理员身份运行”中收到以下错误。

PS C:\Users\user1>     Import-CSV C:\Users\user1\Documents\WTKtoAD\WTKtoAD.csv | %{$SAM = $_.SamAccountName;If ($_.PhoneTypeMobile -eq $Null) {$PhoneMobile = "NotProvided"} Else {$PhoneMobile = $_.PhoneTypeMobile};If ($_.PhoneTypeHome -eq $Null) {$PhoneHome = "NotProvided"} Else {$PhoneHome = $_.PhoneTypeHome}} | set-ADUser -Identity $SAM -Add @{extensionAttribute4="LoadedFromKronos";extensionAttribute5=$PhoneHome;extensionAttribute6=$PhoneMobile} -Manager $_.Manager
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:1 char:318
+ ... User -Identity $SAM -Add @{extensionAttribute4="LoadedFromKronos";extensionAttri ...
+                    ~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

文件如下:

> SamAccountName,PhoneTypeMobile,PhoneTypeHome,Manager
> test.hsi,333-234-3433,'',bob.henst

1 个答案:

答案 0 :(得分:0)

发生错误的原因是因为您将<template> <main class="main"> <AddUser :key="$route.params.userId"/> <UserList/> </main> </template> 放在错误的位置(在ForEach对象之后)。

您几乎无法从代码中分辨出来,因为您将所有内容放在同一行上,这使得它很难阅读。我的建议是给它一些空气。这样,错误更容易发现。

至于代码。我没有进行测试,因为我现在没有可用的广告,但是我认为这会更好:

Set-ADUser

如您所见,我还对# this is the content of the WTKtoAD.csv file # SamAccountName,PhoneTypeMobile,PhoneTypeHome,Manager # test.hsi,333-234-3433,'',bob.henst Import-CSV C:\Users\user1\Documents\WTKtoAD\WTKtoAD.csv | ForEach-Object { $SAM = $_.SamAccountName if ([string]::IsNullOrEmpty($_.PhoneTypeMobile)) { $PhoneMobile = "NotProvided" } else { $PhoneMobile = $_.PhoneTypeMobile } if ([string]::IsNullOrEmpty($_.PhoneTypeHome)) { $PhoneHome = "NotProvided" } else { $PhoneHome = $_.PhoneTypeHome } if ([string]::IsNullOrEmpty($_.Manager)) { $Manager = $null } else { $Manager = $_.Manager } $props = @{ extensionAttribute4 = "LoadedFromKronos" extensionAttribute5 = $PhoneHome extensionAttribute6 = $PhoneMobile } Set-ADUser -Identity $SAM -Replace $props -Manager $Manager } 进行了一次小测试。如果在CSV中为空字符串,则它将清除用户的管理员属性。