forhell中的多个语句,在PowerShell中具有用户定义的数组

时间:2016-06-30 10:23:58

标签: arrays powershell

我正在编写一个简单的PowerShell脚本,它基本上接受了许多PC的名称并将其保存到一个数组中,然后在每台PC上执行两项操作(在本例中为PsInfo和PsGetsid)。我需要的是拿PC1,做PsInfo,然后是PsGetsid,然后拿PC2,做PsInfo,然后是PsGetsid等。用我的代码,我得到的是,它需要PC1,做PsInfo,然后拿PC2,做PsInfo然后它将在每台PC上执行第二个命令。

但是,如果我将数组更改为静态数组(没有用户输入),它可以完美地工作。这是我的代码:

$list =  read-host "Enter the pc names seperated by comma :"
foreach ($element in $list){
Write-Host "PsInfo"
psinfo \\$element
Write-Host "PsGetSid"
psgetsid \\$element

}

这是我的示例输出

PS C:\Users\ebinissac\Desktop> ./test1.ps1
Enter the pc names seperated by comma :: ebinissac,ebinissac1,sithu
PsInfo

PsInfo v1.77 - Local and remote system information viewer
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\ebinissac:
Uptime:                    0 days 3 hours 22 minutes 1 second
Kernel version:            Windows 7 Enterprise, Multiprocessor Free
Product type:              Professional
Product version:           6.1
Service pack:              0
Kernel build number:       7601
Registered organization:   xxxxx
Registered owner:          xxxx
IE version:                9.0000
System root:               C:\WINDOWS
Processors:                8
Processor speed:           3.5 GHz
Processor type:            Intel(R) Core(TM) i7-4790 CPU @
Physical memory:           3170 MB
Video driver:              Intel(R) HD Graphics 4600

System information for \\ebinissac1:
Uptime:                    0 days 8 hours 3 minutes 37 seconds
Kernel version:            Windows 10 Enterprise, Multiprocessor Free
Product type:              Professional
Product version:           6.3
Service pack:              0
Kernel build number:       10586
Registered organization:   xxx
Registered owner:          xxx
IE version:                9.0000
System root:               C:\WINDOWS
Processors:                8
Processor speed:           3.5 GHz
Processor type:            Intel(R) Core(TM) i7-4790 CPU @
Physical memory:           304 MB
Video driver:              Intel(R) HD Graphics 4600

System information for \\sithu:
Uptime:                    14 days 1 hour 13 minutes 36 seconds
Kernel version:            Windows 7 Enterprise, Multiprocessor Free
Product type:              Professional
Product version:           6.1
Service pack:              0
Kernel build number:       7601
Registered organization:   xxx
Registered owner:          xxxx
IE version:                9.0000
System root:               C:\WINDOWS
Processors:                8
Processor speed:           3.3 GHz
Processor type:            Intel(R) Core(TM) i7-3770 CPU @
Physical memory:           3988 MB
Video driver:              Intel(R) HD Graphics 4000
PsGetSid

PsGetSid v1.44 - Translates SIDs to names and vice versa
Copyright (C) 1999-2008 Mark Russinovich
Sysinternals - www.sysinternals.com

\\ebinissac:
SID for \\ebinissac:
S-1-5-21-xxxxxxxxxxxxxxxxx

\\ebinissac1:
SID for \\ebinissac1:
S-1-5-21-xxxxxxxxxxxxxxxxx

\\sithu:
SID for \\sithu:
S-1-5-21-xxxxxxxxxxxxxxxxxx

我不确定它是否与读取数据有关。任何帮助都将不胜感激。 TIY

2 个答案:

答案 0 :(得分:3)

您可以尝试在,上拆分用户输入:

$list = (Read-Host "Enter the pc names seperated by comma").Split(",")

我猜Read-Host无法直接返回集合。

答案 1 :(得分:1)

而不是child.sendline('')您的读取主机正在返回"ebinissac", "ebinissac1", "sithu"。这意味着$ list中只有一个元素,它只是每个命令一次运行所有三个元素。

正如Sodawillow建议的那样,您可以使用"ebinissac,ebinissac1,sithu"拆分输出以获得可以枚举的数组。如果您希望保留当前格式,则需要这样做。

替代方案:

$list.Split(",")

它看起来像这样:

Param([Parameter(Mandatory = $true)] [String[]]$PCName)

并保持循环,直到您输入空白名称。然后,$ PSName将包含一个没有任何摆弄的数组(最好使用更具体的变量名称,然后可以将其保存到$ list之后)。

完全取决于您希望如何输入信息。