Solved

PowerShell: Append to an array from a file

Posted on 2010-09-07
2
1,033 Views
Last Modified: 2012-05-10
I have the following code that is used to populated a $ServerList (starting a $ServerList[1]), but I'd also like to be able to read a file of servernames... and append those names to the end of the ServerList string type.

The issue is, that $ServerList is a pre-dementioned array.  That reason for this is because I get the error:

"Cannot index into a null array."

If attempt to run the code below:

# Grab list of servers use QAD then select the name object, and then short the results by the name

$a = (Get-QADComputer -OSName 'Windows*Server*' | Select-Object name | Sort-Object name )
$b = ($a | ForEach-Object {$_.name})

# Create the list of servers array, but start the elements from 1, not zero.

$ServerList = New-Object string[] ($b.Count+2)

for ($j=0; $j -le $b.count; $j++) { $ServerList[$j+1]=$b[$j]}


As yiu can see, there is a whole lot of swapping going on here... primarily because the output of $a gives me headers, and the populating of $b starts at zero (e.g. $b[0]).  Which causes issues with friggen Excel, so we then dump the contents of $b into $ServerList.

So then I take a CSV file, and I want to add it to the $ServerList array, but I get an ERROR... because the array is not dimmensioned to fit to additional strings:

# Load CSV list of additional servers NOT on the Domain and add them to the list of servers

$a = Import-Csv y:\AddititionalServerList.csv
$b = ($a | ForEach-Object {$_.name})

What I'd like to do, is simply append these to the end of the $ServerList array:

$ServerList = $ServerList+$b

But that doesn't work...

Can anyone work this out for me?

GB
0
Comment
Question by:gerhardub
2 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33625674
I do not exactly know, why it does not work for you, as it workes for me.
But anyway, the code is not very nice and efficient.
My suggestion is this (you can change the text "insertion" in the last line to anything):
$serverlist = [collections.arraylist] (@(Get-QADComputer -OSName 'Windows*Server*' | Select-Object -ExpandProperty name), 

    @(import-csv y:\AddititionalServerList.csv | Select-Object -ExpandProperty name) | %{$_} | Sort-Object)

$ServerList.insert(0,"Insertion")

Open in new window

0
 
LVL 1

Author Closing Comment

by:gerhardub
ID: 33627556
Awesome...

I figure a few hundred more learning experiences like this and I'll actually know what I'm doing.

That's exactly the information I needed to fix this.

Thank YOU!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now