使用PowerShell,根据显示名称为用户设置AD主目录 [英] Using PowerShell, set the AD home directory for a user, given the display name

查看:274
本文介绍了使用PowerShell,根据显示名称为用户设置AD主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于包含用户名列表的csv文件来设置主目录。

I would like to set the home directory based on a csv file containing a list of usernames.

我想象有一个get-user组合,set-用户和提供正确更新的foreach命令。
这是我正在使用的代码,但无法逻辑地跳转到将输出传递到设置主目录的Set-ADUser命令。

I imagine there is a combination of get-user, set-user, and foreach commands that give the correct updates. Here is the code I am using but I'm unable to make the logical jump to piping this output to a Set-ADUser command which sets the home directory.

function ConvertUser($user)
{
    $search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
    $search.filter = "(&(objectClass=user)(displayName=$user))"
    $results = $search.Findall()

    foreach($result in $results){
         $userEntry = $result.GetDirectoryEntry()
         Write-Output($userEntry.sAMAccountName)
    }
 }

function ConvertUsers
{
     process{
        foreach($user In $_){
            ConvertUser($user)
        }
    }
}

Get-Content ".Users.txt" | ConvertUsers

我确定我缺少一些简单的东西,但是我是Powershell新手。

I'm sure I'm missing something simple but alas I am a Powershell newb.

编辑:我想从ConverUsers获得输出,即用户名,然后将其输出到set-aduser命令。每当我尝试通过管道将其传递给set-aduser时,我都会收到语法错误,空管道或错误的数据输出。

I would like to have the output from ConverUsers which is the username and then pipe that to the set-aduser command. Any time I try and pipe it to set-aduser I either get syntax errors, empty pipe, or the wrong data output.

推荐答案

您正在寻找 Set-ADUser cmdlet。它具有 -HomeDirectory 参数,该参数显然允许您设置用户的主目录,以及 -Identity 参数,指定您正在编辑的用户。它还具有 -HomeDrive 参数,用于指定其主目录的驱动器号。

You're looking for the Set-ADUser cmdlet. It has a -HomeDirectory parameter, which obviously allows you to set the user's home directory, and an -Identity parameter that specifies which user you are editing. It also has a -HomeDrive parameter that specifies the drive letter for their home directory.

# 1. Get the user, based on their "display name"
$User = Get-ADUser -LDAPFilter '(&(displayname=Trevor Sullivan))';
# 2. Change the user's home directory and home drive
Set-ADUser -Identity $User.SamAccountName -HomeDirectory \\fileserver\users\trevor -HomeDrive U;

给出以下CSV文件内容:

Given the following CSV file contents:

以下脚本应设置主驱动器:

The following script should set the home drives:

# 1. Import the user data from CSV
$UserList = Import-Csv -Path c:\test\Users.csv;

# 2. For each user ...
foreach ($User in $UserList) {
    # 2a. Get the user's AD account
    $Account = Get-ADUser -LDAPFilter ('(&(displayname={0}))' -f $User.DisplayName);
    # 2b. Dynamically declare their home directory path in a String
    $HomeDirectory = '\\fileserver\users\{0}' -f $Account.SamAccountName;
    # 2c. Set their home directory and home drive letter in Active Directory
    Set-ADUser -Identity $Account.SamAccountName -HomeDirectory $HomeDirectory -HomeDrive u;
}

这篇关于使用PowerShell,根据显示名称为用户设置AD主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆