无法设置;PasswordNeverExpires“;Powershell 2.0中的参数设置为true

无法设置;PasswordNeverExpires“;Powershell 2.0中的参数设置为true,powershell,active-directory,powershell-2.0,Powershell,Active Directory,Powershell 2.0,下面是我的Powershell脚本- Import-Module ActiveDirectory $objOU=[ADSI]“LDAP://OU=Service,OU=Accounts,DC=xyz,DC=com”; $dataSource=import-csv “add_user2.csv”; foreach($dataRecord in $datasource) { $cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName $sAMAc

下面是我的Powershell脚本-

Import-Module ActiveDirectory
$objOU=[ADSI]“LDAP://OU=Service,OU=Accounts,DC=xyz,DC=com”;
$dataSource=import-csv “add_user2.csv”;

foreach($dataRecord in $datasource) 
{
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName

$displayName=$sn + “, ” + $givenName
$userPrincipalName=$sAMAccountName + “@test.com”;

#Additional Attributes
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“sn”,$sn)

#Place the additional attributes into the record

$objUser.Put("PasswordNeverExpires", $true)
$objUser.SetInfo()

}
我正在尝试使用上述脚本设置ActiveDirectory用户的值。我面临的问题是无法将Account选项卡中Account Options下的“PasswordNeverExpires”属性设置为True

我的输入文件“add_user1.csv”如下所示-

FirstName   LastName
Test              Account1
我将感谢所有的帮助


问候。

没有密码NeverExpires属性。如果在
$objUser
上运行
Get Member
,您将看到这一点。这些属性由UserAccountControl控制。了解更多信息

详细信息如何将“密码永不过期”属性设置为true:

Setting "Password never expire" attribute on user object 
This property unlike many other properties of AD object are contained in bitmask
attribute UserAccountControl 
(not related in any way with User Account Control feature of Windows). 
To set it you need to retrieve current value of this attribute and use binary OR
operation (-bor) to calculate new value.


$User = [ADSI]"LDAP://cn=Gusev,ou=Users,ou=Lab,dc=contoso,dc=com"
$UAC = $User.UserAccountControl[0] -bor 65536
$User.Put("userAccountControl",$UAC)
$User.SetInfo()
您的脚本需要进行如下修改:

$objUser.SetInfo()

#Place the additional attributes into the record
$UAC = $objUser.UserAccountControl[0] -bor 65536
$objUser.Put("userAccountControl",$UAC)
$objUser.SetInfo()

如果不运行两次
SetInfo()
,脚本将抛出错误。

没有PasswordNeverExpires属性。如果在
$objUser
上运行
Get Member
,您将看到这一点。这些属性由UserAccountControl控制。了解更多信息

详细信息如何将“密码永不过期”属性设置为true:

Setting "Password never expire" attribute on user object 
This property unlike many other properties of AD object are contained in bitmask
attribute UserAccountControl 
(not related in any way with User Account Control feature of Windows). 
To set it you need to retrieve current value of this attribute and use binary OR
operation (-bor) to calculate new value.


$User = [ADSI]"LDAP://cn=Gusev,ou=Users,ou=Lab,dc=contoso,dc=com"
$UAC = $User.UserAccountControl[0] -bor 65536
$User.Put("userAccountControl",$UAC)
$User.SetInfo()
您的脚本需要进行如下修改:

$objUser.SetInfo()

#Place the additional attributes into the record
$UAC = $objUser.UserAccountControl[0] -bor 65536
$objUser.Put("userAccountControl",$UAC)
$objUser.SetInfo()

如果不运行
SetInfo()
两次,脚本将抛出一个错误。

另一个可以用来避免不得不处理UserAccountControl属性的方法是使用
Set ADUser
PasswordNeverExpires
参数

$objUser | Set-ADUser -PasswordNeverExpires
事实上,通过使用
newaduser

Import-Module ActiveDirectory
$dataSource=import-csv “add_user2.csv”;

foreach($dataRecord in $datasource) 
{
    $cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
    $sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
    $givenName=$dataRecord.FirstName
    $sn=$dataRecord.LastName
    $displayName=$sn + “, ” + $givenName
    $userPrincipalName=$sAMAccountName + “@test.com”;

    New-ADUser $cn -SamAccountName $sAMAccountName -GivenName $givenName `
        -Surname $sn -DisplayName $displayName -UserPrincipalName $userPrincipalName `
        -PasswordNeverExpires $true -Path "OU=Service,OU=Accounts,DC=rjfdev,DC=com"
}

另一个可以用来避免处理UserAccountControl属性的方法是使用
setaduser
PasswordNeverExpires
参数

$objUser | Set-ADUser -PasswordNeverExpires
事实上,通过使用
newaduser

Import-Module ActiveDirectory
$dataSource=import-csv “add_user2.csv”;

foreach($dataRecord in $datasource) 
{
    $cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
    $sAMAccountName=$dataRecord.FirstName + “.” + $dataRecord.LastName
    $givenName=$dataRecord.FirstName
    $sn=$dataRecord.LastName
    $displayName=$sn + “, ” + $givenName
    $userPrincipalName=$sAMAccountName + “@test.com”;

    New-ADUser $cn -SamAccountName $sAMAccountName -GivenName $givenName `
        -Surname $sn -DisplayName $displayName -UserPrincipalName $userPrincipalName `
        -PasswordNeverExpires $true -Path "OU=Service,OU=Accounts,DC=rjfdev,DC=com"
}

谢谢你,丽莎。这也有帮助。再次感谢,谢谢丽莎。这也有帮助。再次感谢。