首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入具有属性的用户,以包括msDS cloudextensionattribute1 1

导入具有属性的用户,以包括msDS cloudextensionattribute1 1
EN

Stack Overflow用户
提问于 2022-09-26 21:32:43
回答 3查看 177关注 0票数 0

我有一个脚本,它可以很好地处理所有其他属性。我不知道该怎么做。我是个脚本新手。在添加msDS-cloudExtensionAttribute1之前,我有一个工作良好的导入。我还需要再加几个。谢谢你过来看看。

代码语言:javascript
复制
#Import active directory module for running AD cmdlets
Import-Module ActiveDirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$Users = Import-csv C:\Test\TESTUSER3a.csv


#Loop through each row containing user details in the CSV file 

foreach ($User in $Users)



 {
    # Read user data from each field in each row
    # the username is used more often, so to prevent typing, save that in a variable
   $Username       = $User.SamAccountName
   
   }
  

    # Check to see if the user already exists in AD
    if (Get-ADUser -F {SamAccountName -eq $Username}) {
         #If user does exist, give a warning
         Write-Warning "A user account with username $Username already exist in Active Directory."
    }
    else {
        # User does not exist then proceed to create the new user account

        # create a hashtable for splatting the parameters
       

        $userProps = @{
            SamAccountName             = $User.SamAccountName                   
            Path                       = $User.path      
            GivenName                  = $User.GivenName 
            Surname                    = $User.Surname
            Initials                   = $User.Initials
            Name                       = $User.Name
            DisplayName                = $User.DisplayName
            UserPrincipalName          = $user.UserPrincipalName 
            Department                 = $User.Department
            Description                = $User.Description
            Office                     = $User.Office
            OfficePhone                = $User.OfficePhone
            EmailAddress               = $User.EmailAddress
            StreetAddress              = $User.StreetAddress
            POBox                      = $User.POBox
            City                       = $User.City
            State                      = $User.State
            PostalCode                 = $User.PostalCode
            Title                      = $User.Title
            Company                    = $User.Company
            msDS-cloudExtensionAttribute1 = $User.msDS-cloudExtensionattribute1
            # AccountPassword            = (ConvertTo-SecureString $User.password -AsPlainText -Force) 
            Enabled                    = $false
            ChangePasswordAtLogon      = $false
        }   #end userprops   
}
         New-ADUser @userProps
         Write-Host "The user account $User is created." -ForegroundColor Cyan
   

     #end else
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-09-29 00:40:46

这是我的解决方案。

$Users _ foreach {

$User.SamAccountName -Replace @{‘msDS-cloudExtensionAttribute1 1’=$..GGGG} Set-ADUser $User.SamAccountName -Replace @{‘msDS-cloudExtensionAttribute1 2’=$..PK} Set-ADUser $User.SamAccountName -Replace @{‘msDS-cloudExtensionAttribute1 3’=$..OK}

票数 -1
EN

Stack Overflow用户

发布于 2022-09-27 02:08:57

如果您查看新-Aduser命令,它有很长的参数列表,包括创建帐户的所有公共属性。但是有许多用户属性,包括msDS-cloudExtensionAttribute1,它们不是此命令的参数。

您对哈希表的操作是将其内容替换为命令中的单个参数:

代码语言:javascript
复制
New-Aduser -SamAccountName $User.SamAccountName -Path $User.path `
-GivenName $User.GivenName -Surname $User.Surname ...

由于msDS-cloudExtensionAttribute1属性不在默认的New-Aduser参数列表中,因此需要使用-Otherattributes参数添加该属性。这是它自己的属性名称和值对的哈希表。您可以包含由分号分隔的多个属性。您仍然需要属性名周围的引号,因为dash:-Otherattributes = @{'msDS-cloudExtensionAttribute1'="testing"}

在参数哈希表中,应该如下所示:

代码语言:javascript
复制
$userProps = @{
    SamAccountName         = "TestTest2"               
    Path                   = "OU=TEST,DC=example,DC=net"  
    GivenName              = "Test"
    Surname                = "test2"
    Name                   = "TestTest2"
    AccountPassword        = (ConvertTo-SecureString $pass -AsPlainText -Force) 
    Enabled                = $false
    ChangePasswordAtLogon  = $true
    Otherattributes        = @{'msDS-cloudExtensionAttribute1'="testing"}
}
New-ADUser @userProps

(请注意,只要使用文本输入和最小数量的参数来创建帐户,就可以简化故障排除的过程。)

票数 1
EN

Stack Overflow用户

发布于 2022-09-26 23:03:15

您的哈希表键和属性名称msDS-cloudExtensionattribute1包含-,它在语法上被解释为-运算符(减法),因此中断了一个表达式,例如$User.msDS-cloudExtensionattribute1

使msDS-cloudExtensionattribute1 将识别为一个完整的键/名称:

代码语言:javascript
复制
- Enclose the (literal) property name in `'...'`:

@{‘msDS-cloudExtensionattribute1 1’= 'foo‘}

  • 作为属性名
代码语言:javascript
复制
- Enclose the property name in `'...'` (works in _both_ PowerShell editions):

$User.'msDS-cloudExtensionattribute1‘1’

代码语言:javascript
复制
- In _PowerShell (Core) 7+_ only, you may alternatively enclose the name in `{...}` analogous to how _variable_ names can be specified unambiguously:

PS 7+ only $User.{msDS-cloudExtensionattribute1 1}

请注意,更一般地,使用(...)、<>C42,PowerShell允许您通过变量和显式e 139表达式E 240指定属性(成员)名称

因此,以下内容也将起作用:

代码语言:javascript
复制
# Using a variable:
$propName = 'msDS-cloudExtensionattribute1'
$User.$propName

# Using an explicit expression (contrived example):
$User.( 'msDS' + '-' + 'cloudExtensionattribute1' )

Separately,您使用 溅溅 新-AdUser 的尝试不能作为工作,因为没有名为-msDS-cloudExtensionattribute1的参数(这将不是一个有效的参数名)。

相反,扩展属性必须通过传递给-OtherAttributes参数的(嵌套)哈希表传递。

因此,您需要以下内容:

代码语言:javascript
复制
$userProps = @{
  SamAccountName             = $User.SamAccountName                   
  # ... 
  OtherAttributes = @{ 'msDS-cloudExtensionAttribute1' = $User.'msDS-cloudExtensionattribute1' }
  # ...
}       
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73860171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档