【问题标题】:Import Users with properties, to include msDS-cloudextensionattribute1 using powershell使用 powershell 导入具有属性的用户,以包括 msDS-cloudextensionattribute1
【发布时间】:2022-09-27 09:28:44
【问题描述】:

我有一个适用于所有其他属性的脚本。我不明白如何做到这一点。我是脚本新手。在添加 msDS-cloudExtensionAttribute1 之前,我的导入效果很好。我还需要添加几个。感谢您的浏览。

#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 = GGGG\"                      = $User.GGGG
            # 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

 

    标签: powershell azure-active-directory


    【解决方案1】:

    哈希表键/属性名称msDS-cloudExtensionattribute1 包含-,被解释为-操作员(减法),因此会破坏 $User.msDS-cloudExtensionattribute1 这样的表达式

    让 PowerShell 识别 msDS-cloudExtensionattribute1作为一个整体

    • 作为一个hashtable

      • '...' 中包含(文字)属性名称:

        @{ 'msDS-cloudExtensionattribute1' = 'foo' }
        
    • 作为一个属性名称

      • 将属性名称括在'...' 中(适用于两个都PowerShell 版本):

        $User.'msDS-cloudExtensionattribute1'
        
      • PowerShell(核心) 7+仅,您也可以将名称包含在 {...} 中,类似于多变的名称可以明确指定:

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

    注意PowerShell 更普遍地允许您指定属性(成员)名称间接地, 通过变量明确的表达式使用(...)grouping operator

    因此,以下内容也可以:

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

    分别地,您尝试将splattingNew-AdUser 一起使用无法正常工作,因为有名为 =msDS-cloudExtensionattribute1 的参数(这不是有效的参数名称)。

    【讨论】:

    • 谢谢你。我尝试了变量。它失败了。我尝试了第一个示例 ['msDS-cloudExtensionAttribute1' = $User.'msDS-cloudExtensionAttribute1'] 它没有用。我收到 [New-ADUser:找不到与参数名称“msDS-cloudExtensionAttribute1”匹配的参数。在 line:60 char:21 + New-ADUser @userProps + ~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Man。 ...
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多