# PowerShell noob here - Problem populating an array



## itavenger (Jun 12, 2009)

Greetings everyone. I am currently working my way through a couple of tutorials for PS beginners. I am working on a script that will give me a list of all live computers on my lan showing their computer names and ip addresses. Using Win32_PingStatus, I can find all on-line ip-addresses and save them into an array, then later, loop back through the array and get the computer names of each IP address.

The problem I am having is saving the IP addresses into the array. I'm not sure if I'm not declaring the array right or if there is something else wrong with the way I am trying to add values to it. When I run the script, I get the error:
****************************************************
Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned to like a variable or a property.
At C:\myscripts\isalive2.ps1:11 char:20
+ $arrIP[$arrCount] = <<<< $Pingy.Address
****************************************************
This is the entire script:
******************
$arrIP = @()
for ($i = 0;$i -lt 100;$i++){
$arrIP += @($i)
}
$arrCount = 0
$i =1
$Ip = "192.168.1."
Do { $Ip4th = $Ip + $i
$Pingy = get-WmiObject Win32_PingStatus -f "Address='$Ip4th'"
if($Pingy.StatusCode -eq 0) {"{0,0} {1,5} {2,5}" -f 
$arrIP[$arrCount] = $Pingy.Address
$arrCount++} 
$i++
}
until ($i -eq 20)
******************


----------



## Squashman (Apr 4, 2003)

I haven't had the chance to play with PowerShell yet but do you realize there is plenty of programs out there that can do this for you in one pass.


----------



## itavenger (Jun 12, 2009)

Thanks squashman - yea, I'm aware of things like spiceworks and I have a VB script that does it too but I'm wanting to get some hands-on with ps and couldn't figure out the problem with my array. This is more of a learning process for me than something mission critical. ps doesn't seem to be real popular yet does it ?


----------



## Squashman (Apr 4, 2003)

If the status code is true why don't you just add in some code to get the hostname and then write the ip and hostname to a file. Something like this.
http://braunblog.com/index.php/2008...esses-and-resolved-hostnames-with-powershell/

Or, if the status code is true, write the IP address to a text file. Then you can use the text file in a loop to get the host name later on.


----------

