这是我遇到麻烦的代码部分:
function getGroup {
$Script:confirmdggroup = "N"
$Script:dggroup = "none"
$dggroup = Read-Host -Prompt 'Enter the name of the group to add a member'
write-host "You entered $dggroup"
$confirmdggroup = Read-Host -Prompt 'If the group name is correct enter Y'
write-host "You entered $confirmdggroup when asked to confirm group name"
}
If ($confirmdggroup -ne "Y") {getGroup}
write-host "Group name to be modified is $dggroup"
write-host "the confirmdggroup variable is: $confirmdggroup"
这是我运行它时输入和输出的图像。您可以看到变量是在函数内部正确分配的,而不是在函数外部正确分配的。我究竟做错了什么?
好的,我正在尝试重写并且不使用“脚本:”,但是我无法获取该函数以返回任何内容。这是新功能:
function getGroup {
while($confirmDGGroup -ne "Y")
{
$DGGroup = Read-Host -Prompt 'Enter the name of the Exchange group to modify'
""
write-host "You entered $DGGroup"
""
$confirmDGGroup = Read-Host -Prompt 'If the group name is correct enter Y'
}
}
cls
write-host "This script will add or remove email addresses from an Exchange Distibution Group"
""
$GroupName = getGroup
write-host "The get group function returned $GroupName"
pause
这是我现在得到的输出:
此脚本将从Exchange分发组中添加或删除电子邮件地址 输入要修改的Exchange组的名称:office 您输入的office 如果组名正确,请输入Y:Y 返回“获取组”功能 按Enter继续...:
此功能可获取用户输入,询问是否正确,如果不正确则重复该操作,最后将文本发送给调用方。
function Get-ExchangeGroupName
{
$Prompt = 'Please enter the Group name '
$Choice = ''
while ($Choice -eq '')
{
$Choice = Read-Host $Prompt
Write-Host ('You entered [ {0} ].' -f $Choice)
$YesNo = Read-Host ' Is that correct? [n/y]'
if ($YesNo -eq 'y')
{
# send the result out to the caller
$Choice
}
else
{
$Choice = ''
}
}
} # function Get-ExchangeGroupName
$ChosenGroupName = Get-ExchangeGroupName
''
$ChosenGroupName
输出带有n
&的y
“正确吗?”响应 ...
Please enter the Group name : qwerty
You entered [ qwerty ].
Is that correct? [n/y]: n
Please enter the Group name : AlfaBravo
You entered [ AlfaBravo ].
Is that correct? [n/y]: y
AlfaBravo
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句