从 AD 组中提取用户并添加到 BookInPolicy

从 AD 组中提取用户并添加到 BookInPolicy

因此,我尝试提取一个或多个(未启用邮件)AD 组中用户的电子邮件,并将该输出传递到 Set-CalendarProcessing。我不知道如何获取正确格式的列表,以便命令接受它。

我会让代码来说话:

$allbookers = Get-ADGroupMember -Identity "sec AXD" | Get-ADUser -Properties Mail | Select-Object Mail
$ab_list = '"{0}"' -f (($allbookers | select -ExpandProperty Mail) -join '","')

$ab_list 的输出正是我想要的:“[电子邮件保护]“,”[电子邮件保护]“ ETC

如果我这样做......

Set-CalendarProcessing -Identity $resourcename -BookInPolicy $ab_list

...我在输出中收到一条警告:警告:找不到对象“”[电子邮件保护]“,”[电子邮件保护]“”(并且 Bookinpolicy 也为空)。

但是如果我复制/粘贴 $ab_list 的输出并手动将其插入到上面的命令中,它就可以起作用。

Set-CalendarProcessing -Identity $resourcename -BookInPolicy "[email protected]","[email protected]"

那么我做错了什么?显然这与输出格式有关,但我该如何“转换”它呢?

答案1

我猜想,当你指定"foo","bar"为参数时,它不会以单个字符串的形式提供给 cmdlet - 在此之前,它会被解析PowerShell 本身放入一个包含两个值的数组中,cmdlet 接收一个数组变量。因此,这也是 -BookInPolicy 选项所期望的 - 它需要一个地址数组,例如 中的地址数组$allbookers | select -expand mail。它实际上无法解析预先格式化的字符串。

答案2

我终于让它工作了。这就是我现在的样子。接下来要做的是从命令行传递组名,这样我就可以避免一直编辑脚本。我确信我可以对代码进行大量优化,但至少它现在对我来说足够实用了。:)

感谢大家的帮助评论!\o/

$axd = Get-ADGroupMember -Identity "sec AXD" | Get-ADUser -Properties Mail | Select-Object Mail

# Joining all the groups to one big happy family, remember to add additional groups below
$allbookers = $axd

# Creating array with booker e-mail addresses
$newBookers = $allbookers | select -ExpandProperty Mail

if ($AddBookers = 1)
{
    $previousBookers = (Get-CalendarProcessing -Identity $resourcename).BookInPolicy

    # Make sure temp variable is empty
    $temp = $null

    # Copy new bookers to every booker list
    $EveryBooker = $newBookers

    # Looping through all previously added bookers and extracting e-mail address from each user and adding to every booker list
    foreach ($prevBooker in $previousBookers)
    {
        $temp = Get-Mailbox -Identity $prevBooker | Select-String PrimarySMTPAddress
        $EveryBooker += $temp       
    }

    # Sort list and remove duplicates
    $EveryBooker = $EveryBooker | sort -Unique

    # Set the Bookinpolicy with the updated list
    Set-CalendarProcessing -Identity $resourcename -BookInPolicy $EveryBooker
}
elseif ($AddBookers = 0)
{
    Set-CalendarProcessing -Identity $resourcename -BookInPolicy $newBookers
}

相关内容