如何使用数组创建显示

时间:2014-04-07 01:40:17

标签: powershell

我想创建表,它为用户提供了一些选项,比如将ip更改为静态和动态,并且我有一个像这样的哈希表。我只是想知道创建这个表的其他方法,而不必垃圾邮件$ box变量

$Box=@("г","="," ","¬","¦","-","L","¦","¦")

Write-Host $Box[0]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[3]
Write-Host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Title "  "$box[2] $box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $box[8]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[7]
write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2] $MenuItems[0] $box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$MenuItems[1]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$MenuItems[2]$Box[2]$box[2]"  "$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $box[6]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[5]

$UserInput = Read-Host "Please make a selection [1-3]"
switch($UserInput)
{
    1 {Set-DHCP}
    2 {Set-StaticIP}
    3 {exit}
    default {Main}
}

1 个答案:

答案 0 :(得分:2)

您可以使用.NET字符串格式整合代码很多。例如,前两行如下所示:

$Lines = @(); # Create an empty array
$Lines += ('{0}' + '{1}'*33 + '{2}') -f $Box[0], $box[1], $box[3]; # Format and add the first line
$Lines += ('{0}' + '{1}'*9 + '{2}  ' + '{1}'*10 + '{0}') -f $Box[4], $box[2], $Title; # Format and add the second line
...
...
...
Write-Host -Object ($Lines -join "`n"); # Write out all lines, joined by a line separator.