输入问题

Issues with input

我创建了一个脚本,用于在混合环境中为本地 DG 添加发送代表权限,但在处理名称列表时遇到了一些问题。如果您添加一个名称,则可以添加另一个名称,它会删除旧名称并添加新名称。就是这样。因此,您通常将名称列表串起来并用逗号分隔的命令然后将它们全部添加并且工作正常。该脚本允许我列出它们,但找不到条目。任何人都可以建议为什么或进行编辑以使这项工作有效吗?

#Script prompts for the required information needed to perform the required actions. 
$Distro = Read-Host 'Insert Distribution Group to check perms.'
$DGroup = Read-Host 'Insert Distribution Group Name e.g. DG-Test@test.com'
$username = Read-Host 'Insert User who needs Send On Behalf. Please include all shown in the last step seperate each name by a comma. Format is First Lastname.'

#Displys the current settings for Send on Behalf rights for the DG.

Get-DistributionGroup $Distro | FL GrantSendOnBehalfTo

Read-Host -Prompt "Press Enter to proceed to next step"

#Now script adds the required permissions input at the beginning.

Set-DistributionGroup -Identity $DGroup -GrantSendOnBehalfTo $username

Read-Host -Prompt "Press Enter to proceed to next step"

#Now the script shows the current permissions after changes

Get-DistributionGroup $Distro | FL GrantSendOnBehalfTo

Read-Host -Prompt "Press Enter to exit"

理想情况下,我看起来会分开,这样它会显示当前权限,然后询问谁需要添加,这样您就可以在脚本中添加所有名称。我对此很陌生,所以仍在学习并希望完成一个部分然后扩展它。

谢谢

我自己无法测试,但来自 the docs:

要添加或删除一个或多个值而不影响任何现有条目,请使用以下语法:

Set-DistributionGroup -Identity $DGroup -GrantSendOnBehalfTo @{Add="$username", "$username2"}

因为您正在使用 Read-Host,所以始终 check/correct 您得到的输入是至关重要的。提示告诉人们输入 First Lastname,但要唯一标识启用邮件的用户或组,要求输入 SamAccountNameEmailaddressUserPrincipalName 会更好.

因为输入的名称可以包含需要引号的空格或字符,我的建议是 总是 引用用户输入的任何内容。

类似于:

$prompt = @"
Insert user(s) that need Send On Behalf.
You can enter multiple usernames, separated by a comma.
Allowed names are SamAccountName, EmailAddress or user full name (Firstname Lastname).

"@
# get the (string) input from the user
$username = Read-Host $prompt
# as per your latest comment and testing, simply split the input on comma
# to create an array of names. (Trim to remove unwanted whitespace)
$users = ($username -split ',').Trim()

然后将这些添加到组

Set-DistributionGroup -Identity $DGroup -GrantSendOnBehalfTo @{Add=$users}

不用说,您应该检查从读取主机输入接收到的所有内容,因此在对这些值执行任何操作之前,还要测试给定的 $Distro$DGroup 是否确实存在。 .