Avoid using '+=' for anything. Every time you add something using that method,
it destroys and recreates the entire array. For small lists that not a big
deal, but for large lists it is a performance nightmare. I personally prefer
array lists for ease of manipulation, and the fact that I can create arrays of
arrays or arrays of any object, really.
[System.Collections.ArrayList]$Array = @()
$array.add('Something')
$array.add('SomethingElse')
$array.add('SomethingElse2')
To remove:
$array.remove('SomethingElse')
For your second part - does this work?
$array|foreach{test-connection -ComputerName $_ -count 1}|select-object
-Property PSComputerName,Address,IPV4Address
--Donnie Taylor
Microsoft MVP - Cloud and Datacenter
Management<https://mvp.microsoft.com/en-us/PublicProfile/5002833>
From: [email protected] [mailto:[email protected]] On
Behalf Of Raymond Peng
Sent: Thursday, October 19, 2017 12:34 PM
To: [email protected]; [email protected]
Subject: [EXTERNAL] [powershell] Powershell array question and formatting
question
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
Nothing in this message is intended to constitute an electronic signature
unless a specific statement to the contrary is included in this message.
Confidentiality Note: This message is intended only for the person or entity to
which it is addressed. It may contain confidential and/or privileged material.
Any review, transmission, dissemination or other use, or taking of any action
in reliance upon this message by persons or entities other than the intended
recipient is prohibited and may be unlawful. If you received this message in
error, please contact the sender and delete it from your computer.