【问题标题】:Issues with input输入问题
【发布时间】:2020-06-27 16:37:51
【问题描述】:

我创建了一个脚本,用于在混合环境中为本地 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"

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

谢谢

【问题讨论】:

    标签: powershell exchange-server


    【解决方案1】:

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

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

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

    因为您使用的是Read-Host,所以始终检查/更正您收到的输入至关重要。提示告诉人们输入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}
    

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

    【讨论】:

    • 失败了。它根本不喜欢输入。如果我粘贴下面的 Set-DistributionGroup -Identity $DGroup -GrantSendOnBehalfTo $username 然后添加名称 name,name1,name2 然后在 PS 中直接运行然后它可以工作。它只是脚本在输入时不喜欢列表,它不会进行更改。
    • @Kevlar 这一切都取决于输入的样子。您的提示说用逗号分隔用户名,但如果值包含空格或需要引号,则需要引用每个值。您应该在代码中对此进行测试,而不是简单地提供某人通过 Read-Host 输入的任何内容,而不知道输入是否有效..
    • 谢谢。所以名称输入应该是 firstname.lastname,即 sAMAccountName。我已经尝试过您的脚本,但多个条目也失败了。手动输入以连接到 PS,然后运行它在脚本中将它们串在一起时不起作用的命令。
    • @Kevlar 从脚本运行时是否收到错误消息?脚本是否连接?脚本是否使用相同的凭据运行?
    • @Kevlar 文档不是很清楚,但您可以尝试使用 array 用户而不是逗号分隔的字符串。 [1] $users = ($username -split ',' | ForEach-Object { '"{0}"' -f $_.Trim() }) [2] Set-DistributionGroup -Identity $DGroup -GrantSendOnBehalfTo @{Add=$users}.
    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 2018-12-31
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多