Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 输入问题_Powershell_Exchange Server - Fatal编程技术网

Powershell 输入问题

Powershell 输入问题,powershell,exchange-server,Powershell,Exchange Server,我创建了一个脚本,用于在hyrbid环境中为DG on prem添加代表发送的PERM,但在处理名称列表时遇到了一些问题。如果你添加了一个名字,它会很好地添加另一个名字,它会删除旧名字并添加新名字。事实就是这样。因此,您通常使用的命令字符串名称列表由逗号分隔,然后它将添加所有名称,效果良好。脚本允许我列出它们,但是找不到条目。有人能提出为什么或编辑,这将使这项工作 #Script prompts for the required information needed to perform the

我创建了一个脚本,用于在hyrbid环境中为DG on prem添加代表发送的PERM,但在处理名称列表时遇到了一些问题。如果你添加了一个名字,它会很好地添加另一个名字,它会删除旧名字并添加新名字。事实就是这样。因此,您通常使用的命令字符串名称列表由逗号分隔,然后它将添加所有名称,效果良好。脚本允许我列出它们,但是找不到条目。有人能提出为什么或编辑,这将使这项工作

#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"
理想情况下,我会看起来分裂,这样它会显示当前的烫发,然后询问谁需要添加,这样你就可以只添加脚本中的所有名称。我是新手,所以仍在学习,希望完成一个部分,然后扩展它


谢谢

我自己无法测试,但是:

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

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

由于您使用的是
Read Host
,因此务必检查/更正您获得的输入。提示会告诉人们输入
First Lastname
,但要唯一标识启用邮件功能的用户或组,最好询问
SamAccountName
Emailaddress
UserPrincipalName

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

比如:

$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
是否确实存在。

。它根本不喜欢输入。如果我将下面的DistributionGroup-Identity$DGroup-grantSendonbehalf粘贴到$username,然后添加name、name1、name2,然后在PS中直接运行,那么它就可以工作了。只是脚本不喜欢输入时的列表,它不进行更改。@Kevlar这一切都取决于输入的外观。提示中说用逗号分隔用户名,但如果值包含空格或需要引号,则需要引用每个值。您应该在代码中对此进行测试,而不是简单地提供某人通过Read Host输入的任何内容,而不知道输入是否有效。谢谢。因此输入的名称应该是firstname.lastname,即sAMAccountName。我已经尝试了你的脚本,但对于多个条目,它也失败了。手动输入以连接到PS,然后运行命令。在脚本中将PS串在一起时,该命令不起作用。@Kevlar从脚本运行时,是否收到错误消息?脚本连接了吗?脚本是否使用相同的凭据运行?@Kevlar文档不太清楚,但您可以尝试使用用户数组而不是逗号分隔的字符串。[1]
$users=($username-split','| ForEach对象{''{0}'-f$\.Trim()。