【问题标题】:Switching Lastname, Firstname in Powershell AD script在 Powershell AD 脚本中切换姓氏、名字
【发布时间】:2019-08-20 04:43:56
【问题描述】:

我尝试通过 Firstname, Lastname 从 OU 中导出 AD 组和用户,但我只让它与 Lastname, Firstname 一起使用。

我尝试的所有其他方法都为我提供了一个空字符串。我尝试将 Select-Object 下的行更改为:

@{Name='Member';Expression={$_.FirstName = GetFirstName $_.Name $_.LastName = GetLastName $_.Name}},

$firt = $_.firstname
$last = $_.lastname
@{Name='Member';Expression={$_.name = "$first,$last"}}, 

这是工作代码,但名称应该调换一下。

$OU = 'OU=Groups,OU=City,OU=Continent,DC=DomainControler, DC=Domain, DC=net' #Change this to get different groups
$DateTime = Get-Date -f "dd-MM-yyyy" 

$MyFileName = "CompanyName-Groups_"+$DateTime+".csv"
$Path = Join-Path $PSScriptRoot $MyFileName

$Groups =  get-adobject -Filter 'ObjectClass -eq "group"' -SearchBase $OU
$i=0
$tot = $Groups.count
$Data = foreach ($Group in $Groups) {
    $i++ 
    $status = "{0:N0}" -f ($i / $tot * 100) 
    Write-Progress -Activity "Exporting AD Groups" -status "Processing Group $i of $tot : $status% Completed" -PercentComplete ($i / $tot * 100) 
    Get-ADGroupMember -Identity $Group | 
    Select-Object @{Name='Group';Expression={$Group.Name}}, 
                  @{Name='Member';Expression={$_.Name}}, 
                  @{Name='Enabled';Expression={if ($_.ObjectClass -eq 'user') {Get-ADUser $_ | Select-Object -Expand Enabled} else {'NA/Group'}}}
    }
$Data | Export-Csv -Path $Path -NoTypeInformation

这是一个示例输出:

Group, "member", enabled
Admin, "Mario, Speedwagon", True
Admin, "Petey, Cruiser", True
Admin, "Anna, Sthesia", False
HR, "Paul, Molive", True
HR, "Phaedra, Lugt", True
IT, "Paul, Molive", False
IT, "Cliff, Hanger", True

应该是这样的:

Group, "member", enabled
Admin, "Speedwagon, Mario", True
Admin, "Cruiser, Petey", True
Admin, "Sthesia, Anna", False
HR, "Molive, Paul", True
HR, "Lugt, Phaedra", True
IT, "Molive, Paul", False
IT, "Hanger, Cliff", True

【问题讨论】:

  • 什么是GetFirstNameGetLastName?哪儿来的呢? ///// 您在Enabled 部分调用Get-ADUser ... 那么为什么不使用它作为GivenNameSurname 的来源?
  • 导出 .csv 后,我在 Excel 中按, 拆分并将数据放入数据透视表中。 Enabled 部分让我可以选择按启用或禁用来过滤帐户。甚至@{Name='Member';Expression={$_.GivenName}} 给了我一个空的成员字符串。
  • 很高兴知道你在正轨上...... [grin]
  • 请查看我的答案,它包含CombinedName,这就是您要查找的内容。
  • 最好把顺序放在第一位,之后你可以拆分,重新排序索引并再次加入,即("Mario, Speedwagon" -split ', ')[1,0] -join ', '

标签: excel powershell csv active-directory


【解决方案1】:

我认为这可能会为您解决问题:

$OU = 'OU=Groups,OU=City,OU=Continent,DC=DomainControler, DC=Domain, DC=net'

$PathParams = @{
    Path      = $PSScriptRoot
    ChildPath = "PA-AD-Groups_{0}.csv" -f (Get-Date -f "dd-MM-yyyy")
}
$FilePath = Join-Path @PathParams

$Groups = Get-ADObject -Filter 'ObjectClass -eq "group"' -SearchBase $OU
$i = 0
$tot = $Groups.count

$Data = foreach ($Group in $Groups) {
    $i++
    $ProgressParams = @{
        Activity        = 'Exporting AD Groups'
        PercentComplete = ($i / $tot * 100)
        status          = "Processing Group $i of $tot : {0:N0} Completed" -f
        ($i / $tot * 100)
    }
    Write-Progress @ProgressParams

    Get-ADGroupMember -Identity $Group |
        Select-Object @{Name = 'Group'; Expression = {$Group.Name}},
    @{Name = 'Member'; Expression = {$_.Name}},
    @{Name = 'Enabled'; Expression = {
            $Script:User = $false
            if ($_.ObjectClass -eq 'user') {
                $Script:User = Get-ADUser $_
                if ($User.Enabled) {$true} else {$false}
            }
            else {
                'NA/Group'
            }
        }
    },
    @{Name = 'FirstName'; Expression = {
            if ($User) {
                $User.GivenName
            }
        }
    },
    @{Name = 'LastName'; Expression = {
            if ($User) {
                $User.Surname
            }
        }
    },
    @{Name = 'CombinedName'; Expression = {
            if ($User) {
               "{0}, {1}" -f $User.GivenName, $User.Surname
            }
        }
    }
}

$Data | Export-Csv -Path $FilePath -NoTypeInformation

您遇到的问题是您不能在Select-Object 内的Expression 之外使用$User 的属性。这可以通过创建一个在整个脚本中可用的变量来解决,因此称为Script scope,用作$Script:User

更多信息请访问Get-Help about_Scopeshere

在旁注中,我建议您使用适当的缩进,这会使内容更具可读性。 splatted 参数hashtable 在这方面也有帮助。最后一个提示:如果您只使用一次,请不要创建变量。否则只会让你以后感到困惑。

【讨论】:

  • 感谢您的详细回复,您可能会看到我对 Powershell 很陌生。我在互联网上找到了大部分代码。我也在从 0 开始上一门课程。你的代码输出给了我一个非常奇怪的输出,所以我用我想要实现的内容编辑了第一篇文章。我正在尝试更改 member 而不是 enabled 中的值
  • 只需删除您不需要的Select-Object 部分,然后根据自己的喜好重命名Name。删除Member,将CombinedName重命名为成员,...
  • 像魅力一样工作!非常感谢花时间的朋友!你为我节省了很多手动输入!
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多