简体   繁体   English

PowerShell Active Directory IF语句

[英]PowerShell Active Directory IF statements

In my organization, we spend a large amount of time changing user home folder paths, based on users changing roles, etc. I have written a very simple powershell script that sets the homedir path from a csv.: 在我的组织中,我们花费大量时间来更改用户主文件夹路径,基于用户更改角色等。我编写了一个非常简单的PowerShell脚本,用于设置来自csv的homedir路径:

Import-Csv C:\Dir\homedir.csv | ForEach-Object {
    Set-ADUser $_.SamAccountName –HomeDirectory $_.HomeDirectory
}

What I would like for this to also do, is to check the users for the presence of a -profilepath (roaming profile) and change that (if present) to "$_.HomeDirectory"\\Profile (all roaming profiles follow this construct). 我想要做的是检查用户是否存在-profilepath(漫游配置文件)并将其(如果存在)更改为"$_.HomeDirectory"\\Profile (所有漫游配置文件都遵循此构造) 。 If not present, it would simply skip down to the next user and perform the work as required. 如果不存在,它将简单地跳到下一个用户并根据需要执行工作。

I've played around with if statements, and really done more harm than good. 我玩过if语句,真的弊大于利。 Anyone got a lifeline they can throw me? 任何人都有他们可以扔我的生命线吗?

You need to specify -Properties ProfilePath if you want the Get-ADUser command to return the profile path. 如果希望Get-ADUser命令返回配置文件路径,则需要指定-Properties ProfilePath

$user = Get-ADUser $_.SamAccountName -Properties ProfilePath
If ($user.ProfilePath -ne $null) {
    Set-ADUser $_.SamAccountName -ProfilePath "$($_.HomeDirectory)\Profile"
}

Hard to tell where you're getting stuck, but I assume it's in the 'properties' bit. 很难说你被困在哪里,但我认为它在'属性'位。 ProfilePath is not pulled by default when you issue the command Get-ADUser . 发出命令Get-ADUser时,默认情况下不会提取ProfilePath。 Something like this should do the trick: 像这样的东西应该做的伎俩:

Import-Csv C:\Dir\homedir.csv | ForEach-Object {

    $user = Get-ADUser $_.SamAccountName -Properties 'ProfilePath'

    if($user.ProfilePath -ne $null)
    {
        $profilepath = $_.HomeDirectory + "\Profile"
        Set-ADUser $user –ProfilePath $profilepath
    }
    Set-ADUser $_.SamAccountName –HomeDirectory $_.HomeDirectory
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM