【问题标题】:How to change the loop Powershell content from the csv file automatically?如何自动更改 csv 文件中的循环 Powershell 内容?
【发布时间】:2020-03-12 14:51:59
【问题描述】:

我想自动更改 CSV 文件中的循环 PowerShell 内容,有人知道如何实现这个目标吗?

我想运行循环 PowerShell,如下所示,我使用 One@contoso.com 之类的 CSV 文件导出 AD 组成员 UPN。我想用UPN自动更改Userlist,因为组中有1000多个成员,我必须将upn从one@contoso.com更改为'one@contoso.com',很容易错过,有没有聪明的方法可以达到目的,谢谢。

Export-ADGroupmember UPN:

Get-ADGroupmember -identity adgroup | % { get-aduser $_.samaccountname | select userprincipalname } | export-csv upn.csv -notypeinformation 

循环 PowerShell:

Loop PowerShell:
$UserList = @(
    'One@contoso.com'
    'Two@contoso.com'
    'Three@contoso.com'
    'Four@contoso.com'
    'Five@contoso.com'
    )

foreach ($UL_Item in $UserList)
    {
    $ARAGU_Params = @{
        TenantName = "contoso"
        HostPoolName = "contosoHostPool"
        AppGroupName = "Desktop Application Group"
        UserPrincipalName = $UL_Item
        }
    Add-RdsAppGroupUser @ARAGU_Params
}

【问题讨论】:

  • 你说你需要把UserPrincipleName从one@contoso.com改成one@contoso.com,但是..区别在哪里?你能给我们举一个更好的例子吗?
  • 您的Get-ADGroupmember 调用和$UserList 集合之间没有任何联系。有吗?在上一个问题中,我的意思是您应该使用类似Get-AdUser 的输出自动填充 $Var。也许$UserList = (Get-ADGroupmember -Identity $TargetGroup).SamAccountName。 ///// 我没有 AD 访问权限,因此您需要将其调整为实际有效的东西。 [咧嘴]
  • @Theo 我认为有一个“”标记。我认为它会产生一些影响。
  • @Lee_Dailey 是的,你是对的。很抱歉我是 PowerShell 的新手。对了,上次你告诉我如何显示输出,有没有代码可以显示我添加了多少用户?
  • @Arthur - 对于“添加了多少”,您可以在循环中添加一个计数器,或者使用$UserList.Count,或者比较之前的总数和之后的总数。我不会使用其中的第三个,寿。 [咧嘴]

标签: windows powershell


【解决方案1】:

您无法更改 $UserList 中的内容。更准确地说,这是可能的,但会影响性能,因为您需要从文件中读取并将其分成多个段。最好的方法是一次下载所有用户。你可以这样做:

$UserList=Get-ADGroupmember -identity adgroup | % { get-aduser $_.samaccountname | select userprincipalname } 
foreach ($UL_Item in $UserList)
    {
    $ARAGU_Params = @{
        TenantName = "contoso"
        HostPoolName = "contosoHostPool"
        AppGroupName = "Desktop Application Group"
        UserPrincipalName = $UL_Item.userprincipalname
        }
    Add-RdsAppGroupUser @ARAGU_Params
}

【讨论】:

  • 非常感谢您的评论。稍后我将进行测试并标记它。顺便说一句,在Get-ADGroupmember的结果中,单元格的内容是userprincipalname,当我运行Add-RdsAppGroupuser 时会不会有一些影响?
  • 是的,它有效果。此参数“您要为应用组分配访问权限的用户的用户主体名称 (UPN)。”
  • 我尝试了评论并以 与 RD 租户关联的 Azure AD 中不存在指定的 UerPrincipalName 错误结束。 > 有没有办法可以删除 "userprinxipalname" 的标题,只保留 UPN Like > One@contoso.com' 'Two@contoso.com' 。现在“UserList”的标题为 userprincipalname,我想删除它,只保留 UPN 的内容,如“One@contoso.com”“Two@contoso.com”“Three@contoso.com”“Four@contoso” .com''Five@contoso.com'
  • @Vad - $UserList 集合看起来将包含具有一个属性的对象 - userprincipalname。但是,当您使用对象时,您指的是整个对象......并且怀疑UserPrincipalName = $UL_Item 应该是UserPrincipalName = $UL_Item.UserPrincipleName。 [咧嘴]
  • @Arthur - 请花时间探索代码每个阶段的项目。当您不了解您正在使用的事物的结构 时,您就无法编写代码。使用$Thing | Get-Member$Thing | Select-Object -Property * 深入了解对象的结构。一旦你这样做了,你就会明白你的错误来自哪里...... [grin]
猜你喜欢
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2011-02-12
  • 2015-12-31
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
相关资源
最近更新 更多