powershell 正在尝试清理我创建的PS脚本

c2e8gylq  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(141)

我在一个K-12学区工作,我一直在编写一个脚本,将参加课外活动的学生添加到不同的AD安全组,这样我就可以专门针对他们后来的时间表申请门禁卡访问权限(目前所有学生都有标准的时间,我们只是为了放学后的事情而不锁门)。
我们学生的课外活动都在我们的SIS中,Azure可以提取所有这些花名册并将其应用于用户。我们的访问控制管理器只与AD同步,而不与Azure同步,所以我创建了AD安全组,该脚本试图将Azure组同步到安全组。我对PowerShell还很陌生,所以虽然我确实让它工作了,但我的脚本很长,花了很长时间才弄明白。我正在寻找关于我如何缩短它的参考资料

$VB =  Get-AzureADGroupMember -ObjectId zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz  #volleybally
$SOB = Get-AzureADGroupMember -ObjectId zzzzzzzzzzzzzzzzzzzzzzzzzzzz  #Boys Soccer
$SOG = Get-AzureADGroupMember -ObjectId xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5  #Girls Soccer
$BG = Get-AzureADGroupMember -ObjectId 7xxxxxxxxxxxxxxxxxxxxxxxxxxx   #Boys Golf
$GG = Get-AzureADGroupMember -ObjectId xxxxxxxxxxxxxxxxxxxxxxxxxxxxx1c   #Girls Golf
$CC = Get-AzureADGroupMember -ObjectId zzzzzzzzzzzzzzzzzzzzzzzzzzzzz  #Cross Country
$FCL = Get-AzureADGroupMember -ObjectId xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  #Fall Cheerleading
$SDD = Get-AzureADGroupMember -ObjectId xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #Speech Drama &  Debate

# initiate SG Groups

$SGCC = Get-ADGroup -Identity sample group 1
$SGFC = Get-ADGroup -Identity sample group 2
$SGFB = Get-ADGroup -Identity sample group 3
$SGGO = Get-ADGroup -Identity sample group 4
$SGSO = Get-ADGroup -Identity sample group 5
$SGVB = Get-ADGroup -Identity sample group 6

# convert azure values to AD and add to AD groups

$FB= Foreach($ad in $FB) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$VB = Foreach($ad in $VB) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
    # your text
}

$SOB = Foreach($ad in $SOB) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$SOG = Foreach($ad in $SOG) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$BG = Foreach($ad in $BG) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$GG = Foreach($ad in $GG) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}
$CC = Foreach($ad in $CC) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$FCL = Foreach($ad in $FCL) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

$SDD = Foreach($ad in $SDD) {
    Get-ADUser ($ad.UserPrincipalName.Split('@')[0])
}

#### add AD Members to SG

Add-AdGroupMember -Identity $SGFB -Members $FB

# Looking to simplify, To be continued

我已经写了很久了。好奇有没有人有办法把它做得更好。就像我说的,我是PS新手。

xqnpmsa8

xqnpmsa81#

我认为您可以使用哈希表($map)来简化您的任务,其中键是每个Azure组的GUID,值是需要添加Az组成员的每个AD组。
例如:

$map = @{
    'xxxxxxxxxxxx' = 'group 1', 'group 5', 'group 8' # Football
    'zzzzzzzzzzzz' = 'group 2'                       # Volleyball
    'yyyyyyyyyyyy' = 'group 3', 'group 4'            # Boys Soccer
    # and so on here
}

foreach($pair in $map.GetEnumerator()) {
    # I think you could use `DisplayName` instead of `UserPrincipalName` here
    # and don't have a need to parse the UPNs
    $azMembers = (Get-AzureADGroupMember -ObjectId $pair.Key).DisplayName
    foreach($adGroup in $pair.Value) {
        Add-ADGroupMember -Identity $adGroup -Members $azMembers.DisplayName
    }
}

正如内联注解中所述,我相信使用.DisplayName就足够了,因为-Members采用以下值之一:

  • 可分辨名称
  • GUID(ObjectGUID)
  • 安全标识(ObjectSid)
  • SAM帐户名(SAMAcCountName)

但考虑到情况可能并非如此,而且也不起作用,那么解析用户的UserPrincipalName而不是使用.Split('@')[0]的一种更简单、更安全的方法是使用MailAddress class,因此使用它的代码将如下所示:


# here goes the `$map` too!

foreach($pair in $map.GetEnumerator()) {
    $azMembers = [mailaddress[]] (Get-AzureADGroupMember -ObjectId $pair.Key).UserPrincipalName
    foreach($adGroup in $pair.Value) {
        Add-ADGroupMember -Identity $adGroup -Members $azMembers.User
    }
}

相关问题