【问题标题】:PowerShell Active Directory IF statementsPowerShell Active Directory IF 语句
【发布时间】:2014-02-20 17:34:47
【问题描述】:

在我的组织中,我们花费大量时间更改用户主文件夹路径,基于用户更改角色等。我编写了一个非常简单的 powershell 脚本,它从 csv 设置 homedir 路径。:

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

我还希望这样做,是检查用户是否存在 -profilepath(漫游配置文件)并将其(如果存在)更改为 "$_.HomeDirectory"\Profile(所有漫游配置文件都遵循此结构)。如果不存在,它会直接跳到下一个用户并根据需要执行工作。

我玩过if 声明,确实弊大于利。有人有救生索可以扔给我吗?

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    很难说你在哪里卡住了,但我认为它在“属性”位。当您发出命令Get-ADUser 时,默认情况下不会拉取 ProfilePath。像这样的东西应该可以解决问题:

    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
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望Get-ADUser 命令返回配置文件路径,则需要指定-Properties ProfilePath

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 2022-01-04
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 2018-01-27
        • 1970-01-01
        相关资源
        最近更新 更多