Powershell退出do-until循环后提早终止

时间:2018-06-29 13:54:36

标签: powershell do-while terminate

我正在使用Powershell脚本,并且试图从选项列表中接受自定义用户输入。我遇到的问题是,当满足退出条件(键入字符'd')时,脚本终止并且不执行其余代码(该代码应根据用户定义的数组$复制快捷方式) OfficeProgramNames)

 {
        "id": "76",
        "Name": "Billy Bob",
        "Title": "",
        "First_Name": "Billy",
        "Last_Name": "Bob",
        "Company": "",
        "Company_Type": "",
        "Job": "",
        "Sector": "",
        "Website": "http:\/\/",
        "Address": "",
        "Postcode": "",
        "Country": ""
    },

我尝试插入几个“写主机”命令进行调试,并查看代码在何处终止。我颇为困惑,无法查看控制流的位置,因此,如果有人可以帮助我进行澄清,将不胜感激!

为进一步清楚起见,输出如下(由于有一些Clear-Host命令,因此进行了一些修改):

#OfficeShortcuts -- Creates shortcuts for the main four Office 2016 applications.

#Optional command-line parameters that can be passed to create icons for All (-a) icons, or a CUSTOM set of icons (-c)
param([switch] $a, [switch] $c)

#Creates a shortcut on the desktop that copies from the start menu shortcuts.
function CreateOfficeDesktopShortcut([string] $ShortcutName)
{
    Copy-Item -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\$ShortcutName.lnk" "$env:USERPROFILE\Desktop"
}

function Show-Menu
{
     Clear-Host
     Write-Host "Select which items you would like placed on the desktop:"

     Write-Host "1: Word"
     Write-Host "2: Excel"
     Write-Host "3: PowerPoint"
     Write-Host "4: Outlook"
     Write-Host "5: Access"
     Write-Host "6: OneNote"
     Write-Host "7: Publisher"
     Write-Host "D: Press 'D' when done."
}

function GetCustomUserSelection($OfficeProgramNames)
{
    $OfficeProgramNames = @()
    do
    {
        Clear-Host
        Show-Menu
        Write-Host "You have selected: $OfficeProgramNames"
        $foo = Read-Host "Please make a selection:"
        switch($foo)
        {
            '1'{
                $OfficeProgramNames += "Word*"
            }
            '2'{
                $OfficeProgramNames += "Excel*"
            }
            '3'{
                $OfficeProgramNames += "PowerPoint*"
            }
            '4'{
                $OfficeProgramNames += "Outlook*"
            }
            '5'{
                $OfficeProgramNames += "Access*"
            }
            '6'{
                $OfficeProgramNames += "OneNote*"
            }
            '7'{
                $OfficeProgramNames += "Publisher*"
            }
            '8'{
                $OfficeProgramNames += "Skype*"
            }
            'd'{
                return
            }
        }
    }
    until ($foo -eq 'd')
    Write-Host "Now do something else"
}

#Loop through the array of Shortcut names, and create a shortcut with a matching alias
function SelectOfficeShortcuts($arg1, $arg2)
{
    [string[]] $OfficeProgramNames = @("Word", "Excel", "PowerPoint", "Outlook")

    if ($a -eq $True)
    {
        $OfficeProgramNames += "Access", "OneNote*", "Publisher"
    }
    elseif($c -eq $True)
    {
        $OfficeProgramNames = @()
        GetCustomUserSelection $OfficeProgramNames
    }
    for ($i=0; $i -lt $OfficeProgramNames.Length; $i++)
    {
        CreateOfficeDesktopShortcut $OfficeProgramNames[$i]
    }
}

SelectOfficeShortcuts($a, $c)

在脚本的末尾调用“ SelectOfficeShortcuts($ a,$ c)”,其中$ a和$ c是脚本的切换参数。

0 个答案:

没有答案