【问题标题】:Capture multiple variables捕获多个变量
【发布时间】:2016-05-09 10:31:04
【问题描述】:

我目前有一个 powershell 脚本,它允许我一次输入一个用户的详细信息,然后可以在以后用作变量。目前脚本会调用每个输入,然后将其保存为变量,然后重复 10 次。

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$NameOne = [Microsoft.VisualBasic.Interaction]::InputBox("Enter UserName")
$firstname,$surname = $NameOne -split("\.")
$NameTwo = [Microsoft.VisualBasic.Interaction]::InputBox("Enter UserName")
$firstname,$surname = $NameTwo -split("\.")

有没有办法缩短脚本,既允许输入和存储用户名,又在 InputBox 没有数据输入时继续执行脚本的下一部分?

谢谢 汤姆

【问题讨论】:

    标签: powershell variables input inputbox


    【解决方案1】:

    使用while 循环:

    $Users = while(($Username = [Microsoft.VisualBasic.Interaction]::InputBox("Enter UserName")).Trim() -ne "")
    {
        $firstname,$surname = $Username -split '\.'
        New-Object psobject -Property @{
            Firstname = $firstname
            Surname   = $surname
            Username  = $Username
        }
    }
    

    当用户不输入任何内容或空格时,循环将退出,否则它将创建一个新的“用户对象”,该对象将最终出现在 $Users 数组中

    【讨论】:

    • 太棒了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    相关资源
    最近更新 更多