screenslaver
screenslaver

Reputation: 613

multiple statements in a for loop with a user defined array in powershell

I am writing a simple powershell script, which basically accepts the names of a number of PCs and save it into an array, then do two things (in this case lets take PsInfo and PsGetsid) on each of the PC. What I need is to take PC1, do PsInfo, followed by PsGetsid, then take PC2, do PsInfo, followed by PsGetsid etc. With my code, what I am getting is, it take PC1, do PsInfo, then take PC2, do PsInfo etc. Then it will do the second command on each PC.

However, if I change the array to a static array (no user input), it works perfectly. Here is my code :

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

}

And here is my sample output

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

I am not sure if it has something to do with how the data is read in. Any help will be appreciated. TIY

Upvotes: 1

Views: 128

Answers (2)

Deadly-Bagel
Deadly-Bagel

Reputation: 1620

Rather than "ebinissac", "ebinissac1", "sithu" your Read-Host is returning "ebinissac,ebinissac1,sithu". This means that there is only one element in $list and it just runs all three at once per command.

As Sodawillow has suggested, you can split the output with $list.Split(",") to get an array you can then enumerate. This is required if you wish to keep your current formatting.

An alternative:

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

It would then look like this:

PCName[1]: ebinissac
PCName[2]: ebinissac1
PCName[3]: sithu
PCName[4]: 

and keep looping until you enter a blank name. $PSName would then contain an array without any fiddling (better to use a more specific variable name, you can then save it to $list after).

Entirely depends on how you want to enter the information.

Upvotes: 1

sodawillow
sodawillow

Reputation: 13176

You can try to split user input on , with this:

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

I guess Read-Host cannot directly return a collection.

Upvotes: 3

Related Questions