【问题标题】:Powershell - SET-ADUSER Modify samaccountnamePowershell - SET-ADUSER 修改 samaccountname
【发布时间】:2017-10-10 16:20:49
【问题描述】:

PowerShell 新手,仍在尝试获取逻辑。

我想过滤掉特定 OU 中活动目录中的 samaccountname,它在字符串 (phil_test_adm) 的末尾也包含“adm”。当找到用户时,我想用“tst”替换“adm”。到目前为止,我有以下内容,我再次在逻辑上苦苦挣扎。

import-module activedirectory
$Path = 'OU=Example, OU=Users,DC=uk,DC=random,DC=com'
$users = Get-ADUser -filter 'SamAccountName -like "*adm"' -SearchBase $Path | select-object samaccountname, name
#foreach ($user in $users) 
#{
#Set-ADUser $user.samaccountname -Replace { 
#if $user.sammaccountname -eq "*adm"  }

正如你所看到的 foreach 是从哪里开始的,我一直在玩。 感谢您的任何帮助!

【问题讨论】:

  • 当您存储在 $users 中的结果已经在 adm 上过滤时,为什么底部有“if $user.sammaccountname -eq "*adm"”?

标签: powershell powershell-2.0


【解决方案1】:

假设你的前几行正在努力检索正确的结果,这个 foreach 块应该可以工作:

foreach ($user in $users) 
{
    $newSamAccoutname = $user.SamAccountName.replace("adm","tst")
    Set-ADUser $user -Replace @{samaccountname=$newSamAccoutname} 
}

【讨论】:

  • 哇.. 想想真的不难!感谢您的输入非常有帮助!现在只是进行一些错误处理:) 干杯!
【解决方案2】:

你也可以试试:

foreach ($user in $users) {Set-ADUser $user -samaccountname $($user.SamAccountName -replace 'adm$','tst')}

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多