An array in Windows is of fixed size. If you want something of variable size, 
you use a list or a table. For example, this shows a list which contains 
strings:

[cid:image001.png@01D348E1.EE4D2130]

Now, there is also something called an ArrayList, but let's not make things 
confusing.

PowerShell has lots of syntactic sugar around hash tables and ordered hash 
tables. They might be useful to you.

The formatting question is a little more complicated. That's because the 
Test-Connection cmdlet is changing the name of the "Destination" column behind 
your back. (The way it does that is complicated, and trust me, you don't really 
want to know.) To get what you want:

[cid:image002.png@01D348E5.1272F230]

But if you are happy with the column being named "Address", which is the true 
name of the variable:

[cid:image003.png@01D348E5.527A8C30]

From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On 
Behalf Of Raymond Peng
Sent: Thursday, October 19, 2017 1:34 PM
To: powersh...@lists.myitforum.com; ntsysadm@lists.myitforum.com
Subject: [NTSysADM] 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



Reply via email to