无法将Powershell中的变量从函数传递到脚本

丹·史蒂文森

这是我遇到麻烦的代码部分:


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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

VSTS:将构建/发布变量传递到Powershell脚本任务中

来自分类Dev

将参数从bash脚本传递到脚本中的函数

来自分类常见问题

我无法将变量从函数传递给Shell脚本中的主代码

来自分类Dev

我无法将变量从函数传递给Shell脚本中的主代码

来自分类Dev

脚本未将变量信息传递到If,Else,While或函数中

来自分类Dev

将.csv值传递到Powershell中的脚本

来自分类Dev

将jenkins管道中设置的变量传递到shell脚本

来自分类Dev

将JavaScript变量传递到Rails脚本中

来自分类Dev

将python变量传递到shell脚本中

来自分类Dev

将PHP变量传递到Javascript函数中

来自分类Dev

将参数传递到Powershell脚本

来自分类Dev

将参数传递到Powershell脚本

来自分类Dev

将Datetime传递到Powershell脚本

来自分类Dev

将变量从bash脚本传递到makefile

来自分类Dev

将变量从Java传递到Shell脚本

来自分类Dev

无法将变量作为onClick函数中的参数传递

来自分类Dev

无法将变量作为onClick函数中的参数传递

来自分类Dev

无法将参数传递到React处理函数中

来自分类Dev

将变量从批处理传递到Powershell

来自分类Dev

尝试将管道秘密变量传递到Powershell脚本时出错

来自分类Dev

如何将变量从批处理文件传递到Powershell脚本

来自分类Dev

如何将输入传递到CA NIMSOFT powerhell脚本中的powershell脚本?

来自分类Dev

Powershell将变量传递给函数

来自分类Dev

将变量从Swift传递到javascript函数

来自分类Dev

将变量传递到.then异步函数

来自分类Dev

将变量从循环传递到主函数

来自分类Dev

Swift:我无法将Firebase FIRAuth函数中的变量传递回父函数中的声明变量

来自分类Dev

无法将变量从Jenkins管道的脚本部分传递到Ansible剧本

来自分类Dev

无法将shell变量传递到awk

Related 相关文章

  1. 1

    VSTS:将构建/发布变量传递到Powershell脚本任务中

  2. 2

    将参数从bash脚本传递到脚本中的函数

  3. 3

    我无法将变量从函数传递给Shell脚本中的主代码

  4. 4

    我无法将变量从函数传递给Shell脚本中的主代码

  5. 5

    脚本未将变量信息传递到If,Else,While或函数中

  6. 6

    将.csv值传递到Powershell中的脚本

  7. 7

    将jenkins管道中设置的变量传递到shell脚本

  8. 8

    将JavaScript变量传递到Rails脚本中

  9. 9

    将python变量传递到shell脚本中

  10. 10

    将PHP变量传递到Javascript函数中

  11. 11

    将参数传递到Powershell脚本

  12. 12

    将参数传递到Powershell脚本

  13. 13

    将Datetime传递到Powershell脚本

  14. 14

    将变量从bash脚本传递到makefile

  15. 15

    将变量从Java传递到Shell脚本

  16. 16

    无法将变量作为onClick函数中的参数传递

  17. 17

    无法将变量作为onClick函数中的参数传递

  18. 18

    无法将参数传递到React处理函数中

  19. 19

    将变量从批处理传递到Powershell

  20. 20

    尝试将管道秘密变量传递到Powershell脚本时出错

  21. 21

    如何将变量从批处理文件传递到Powershell脚本

  22. 22

    如何将输入传递到CA NIMSOFT powerhell脚本中的powershell脚本?

  23. 23

    Powershell将变量传递给函数

  24. 24

    将变量从Swift传递到javascript函数

  25. 25

    将变量传递到.then异步函数

  26. 26

    将变量从循环传递到主函数

  27. 27

    Swift:我无法将Firebase FIRAuth函数中的变量传递回父函数中的声明变量

  28. 28

    无法将变量从Jenkins管道的脚本部分传递到Ansible剧本

  29. 29

    无法将shell变量传递到awk

热门标签

归档