使用循环数组在Powershell中存储用户输入

时间:2021-08-31 00:34:33

I am currently trying to powershell script that will allow users to input a list of servers that will be added a collection and then parsed into XML. I've never tried to loop in Powershell, the ISE doesn't have an immediate window, so I can't see if my array is actually being built. Can anyone validate that this code will work?

我目前正在尝试使用PowerShell脚本,该脚本允许用户输入将添加集合然后解析为XML的服务器列表。我从来没有试过在Powershell中循环,ISE没有立即窗口,所以我无法看到我的数组是否真正被构建。任何人都可以验证此代码是否有效?

$Reponse = 'Y'
$ServerName = $Null
$ServerList = $Null
$WriteOutList = $Null

Do 
{ 
    $ServerName = Read-Host 'Please type a server name you like to minitor. Please use the server FQDN'
    $Response = Read-Host 'Would you like to add additional servers to this list? (y/n)'
    $Serverlist = @($ServerName += $ServerName)
}
Until ($Response -eq 'n')

1 个解决方案

#1


Regarding the array is not storing string values is because the syntax is incorrect. To add new strings in an array use the += operator after the string you want to insert.

关于数组不存储字符串值是因为语法不正确。要在数组中添加新字符串,请在要插入的字符串后面使用+ =运算符。

  1. Declare the empty array with "$Serverlist = @()" (declaring is not required when not running scripts in strict mode)
  2. 使用“$ Serverlist = @()”声明空数组(在严格模式下不运行脚本时不需要声明)

  3. Add new strings to the array using the "+=" operator
  4. 使用“+ =”运算符向数组添加新字符串

  5. Ouput the array using the "Write-Output" cmdlet
  6. 使用“Write-Output”cmdlet输出数组

As Is

$Reponse = 'Y'
$ServerName = $Null
$ServerList = $Null
$WriteOutList = $Null

Do 
{ 
    $ServerName = Read-Host 'Please type a server name you like to minitor. Please use the server FQDN'
    $Response = Read-Host 'Would you like to add additional servers to this list? (y/n)'
    $Serverlist = @($ServerName += $ServerName)
}
Until ($Response -eq 'n')

To Be

$Reponse = 'Y'
$ServerName = $Null
$Serverlist = @()
$WriteOutList = $Null

Do 
{ 
    $ServerName = Read-Host 'Please type a server name you like to minitor. Please use the server FQDN'
    $Response = Read-Host 'Would you like to add additional servers to this list? (y/n)'
    $Serverlist += $ServerName
}
Until ($Response -eq 'n')

Write-Output $Serverlist

Regarding your script, I cannot help wonder why you want to have user input? The whole point of Powershell is to automate as much as possible. I would recommend to use import-csv cmdlet and refere to a file which contains all your server names.

关于你的脚本,我不禁想知道为什么你想要用户输入? Powershell的重点是尽可能自动化。我建议使用import-csv cmdlet并引用包含所有服务器名称的文件。

Importing using Import-CSV

使用Import-CSV导入

$ComputerNames = Import-Csv -Path 'C:\serverlist.csv' -Header 'Computer'
Write-Output $ComputerNames

Note: It will import using PSCustomObject and not an Array

注意:它将使用PSCustomObject而不是Array导入

CSV File Example

CSV文件示例

Server1
Server2
Server3
Server4

Note: Just type all the servers in notepad ending with a carriage return after the server name.

注意:只需在服务器名称后面的回车符中键入记事本中的所有服务器。

Finally check out this cheat sheet from PowershellMagazine. It contains useful commands, operators, arrays, ... This helped me also allot during the first days of powershell scripting.

最后查看PowershellMagazine的备忘单。它包含有用的命令,运算符,数组......这有助于我在powershell脚本编写的最初几天分配。

Url: http://download.microsoft.com/download/2/1/2/2122F0B9-0EE6-4E6D-BFD6-F9DCD27C07F9/WS12_QuickRef_Download_Files/PowerShell_LangRef_v3.pdf

#1


Regarding the array is not storing string values is because the syntax is incorrect. To add new strings in an array use the += operator after the string you want to insert.

关于数组不存储字符串值是因为语法不正确。要在数组中添加新字符串,请在要插入的字符串后面使用+ =运算符。

  1. Declare the empty array with "$Serverlist = @()" (declaring is not required when not running scripts in strict mode)
  2. 使用“$ Serverlist = @()”声明空数组(在严格模式下不运行脚本时不需要声明)

  3. Add new strings to the array using the "+=" operator
  4. 使用“+ =”运算符向数组添加新字符串

  5. Ouput the array using the "Write-Output" cmdlet
  6. 使用“Write-Output”cmdlet输出数组

As Is

$Reponse = 'Y'
$ServerName = $Null
$ServerList = $Null
$WriteOutList = $Null

Do 
{ 
    $ServerName = Read-Host 'Please type a server name you like to minitor. Please use the server FQDN'
    $Response = Read-Host 'Would you like to add additional servers to this list? (y/n)'
    $Serverlist = @($ServerName += $ServerName)
}
Until ($Response -eq 'n')

To Be

$Reponse = 'Y'
$ServerName = $Null
$Serverlist = @()
$WriteOutList = $Null

Do 
{ 
    $ServerName = Read-Host 'Please type a server name you like to minitor. Please use the server FQDN'
    $Response = Read-Host 'Would you like to add additional servers to this list? (y/n)'
    $Serverlist += $ServerName
}
Until ($Response -eq 'n')

Write-Output $Serverlist

Regarding your script, I cannot help wonder why you want to have user input? The whole point of Powershell is to automate as much as possible. I would recommend to use import-csv cmdlet and refere to a file which contains all your server names.

关于你的脚本,我不禁想知道为什么你想要用户输入? Powershell的重点是尽可能自动化。我建议使用import-csv cmdlet并引用包含所有服务器名称的文件。

Importing using Import-CSV

使用Import-CSV导入

$ComputerNames = Import-Csv -Path 'C:\serverlist.csv' -Header 'Computer'
Write-Output $ComputerNames

Note: It will import using PSCustomObject and not an Array

注意:它将使用PSCustomObject而不是Array导入

CSV File Example

CSV文件示例

Server1
Server2
Server3
Server4

Note: Just type all the servers in notepad ending with a carriage return after the server name.

注意:只需在服务器名称后面的回车符中键入记事本中的所有服务器。

Finally check out this cheat sheet from PowershellMagazine. It contains useful commands, operators, arrays, ... This helped me also allot during the first days of powershell scripting.

最后查看PowershellMagazine的备忘单。它包含有用的命令,运算符,数组......这有助于我在powershell脚本编写的最初几天分配。

Url: http://download.microsoft.com/download/2/1/2/2122F0B9-0EE6-4E6D-BFD6-F9DCD27C07F9/WS12_QuickRef_Download_Files/PowerShell_LangRef_v3.pdf