Thank you Devin and all who responded – this makes sense and I will be using 
this to declare new arrays (albeit difficult to remember).

The last formatting part is not working because there’s nothing to be piped 
apparently

PS C:\WINDOWS\system32> foreach ($comp in $array){Test-Connection $comp -Count 
1} | select Destination, IPv4Address
At line:1 char:59
+ foreach ($comp in $array){Test-Connection $comp -Count 1} | select De ...
+                                                           ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], 
ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

PS C:\WINDOWS\system32> foreach ($comp in $array){Test-Connection $comp -Count 
1} | gm
At line:1 char:59
+ foreach ($comp in $array){Test-Connection $comp -Count 1} | gm
+                                                           ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], 
ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement


PS C:\WINDOWS\system32> foreach ($comp in $array){Test-Connection $comp -Count 
1 | select Destination,IPv4Address}

Destination IPV4Address
----------- -----------
            172.29.133.130
            172.29.118.101
            172.29.171.28
            172.29.118.164


Thank you,

Ray



From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On 
Behalf Of Devin Rich
Sent: Thursday, October 19, 2017 10:58 AM
To: powershell@lists.myitforum.com
Subject: Re: [powershell] Powershell array question and formatting question

Hi Ray,

Welcome to the world of guessing! Where the powershell developers had to guess 
which type of arrays to give you by default. In Powershell, there are arrays 
that are static (fixed length), and real arrays like you know from C#, etc. In 
powershell, $a = 1,2,3 will give you one of those static arrays. if you then do 
$a += 4, it will create a new array of length $a.Count + 1 and copy all 
contents of $a into the new array before populating the final item with the 4. 
This is why doing looping with += will in cases with lots of loops or larger 
datasets waste a TON of CPU creating and deleting useless arrays. In order to 
get the arraylist that you are thinking of using (that can .add() and 
.remove()), you need to use ArrayList from Syste,Collections. Also, -= is not a 
thing for powershell. Consider this example:

[Inline image 1]

And I could just as easily do:
PS C:\downloads> [System.Collections.ArrayList]$a = 1,2,3
PS C:\downloads> $a.Remove(2)
PS C:\downloads> $a
1
3
PS C:\downloads> $a.Remove(3)
PS C:\downloads> $a
1

Lastly, select should work fine:
foreach ($comp in $array){Test-Connection $comp -Count 1} | Select Destination, 
IPv4Address
foreach ($comp in $array){Test-Connection $comp -Count 1 | Select Destination, 
IPv4Address }
These are functionally the same, but in case 1, it will gather all results from 
the foreach before piping to a single select. Case 2 will pipe each result to 
select before going onto the next iteration. Just a style choice there for you.


Thanks,
Devin Rich
Systems Administrator


On Thu, Oct 19, 2017 at 11:33 AM, Raymond Peng 
<raymond.p...@wageworks.com<mailto:raymond.p...@wageworks.com>> wrote:
Hi All,

Still picking up powershell so please let me know if you see any obvious 
blunders: I see it say the collection is of a fixed size but how do I remove?

I have declared an array with $array=”name1”,”name2”, etc…
I can access each element with $array[0]…

I can use $array += “name5,”name6” but I can not figure out how to remove an 
element
I have tried the Remove method without any luck as well as -=

PS C:\WINDOWS\system32> $array.Remove("cxsupportsystems")
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed 
size."
At line:1 char:1
+ $array.Remove("cxsupportsystems")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NotSupportedException

PS C:\WINDOWS\system32> $array
kcfileserver
wnweb01
pvfile01
corpcxtsgp01
cxsupportsystems

Lastly – formatting question:

Is there any way to just crop out the rest and keep the destination / 
IPv4Address info? I tried format-table / select-object but it does not do what 
I want. I’ve tried piping after the test-connection in the scriptblock but 
output is not what I want


PS C:\WINDOWS\system32> foreach ($comp in $array){Test-Connection $comp -Count 
1}

Source        Destination     IPV4Address      IPV6Address                      
        Bytes    Time(ms)
------        -----------     -----------      -----------                      
        -----    --------
L-SM-RPENG1   kcfileserver    172.29.133.130                                    
        32       62
L-SM-RPENG1   wnweb01         172.29.118.101                                    
        32       66
L-SM-RPENG1   pvfile01        172.29.171.28                                     
        32       72
L-SM-RPENG1   corpcxtsgp01    172.29.118.164                                    
        32       70
L-SM-RPENG1   cxsupportsys... 10.2.1.238                                        
        32       55



Thank you,

Ray





The information contained in this message is privileged, confidential, and 
protected from disclosure. If you are not the intended recipient, you are 
hereby notified that any review, printing, dissemination, distribution, copying 
or other use of this communication is strictly prohibited. If you have received 
this communication in error, please notify us immediately by replying to the 
message and deleting it from your computer.

Reply via email to