Solved

file vs variable in powershell

Posted on 2014-02-23
7
189 Views
Last Modified: 2014-02-26
How come I have to use the split option when dealing a list in variable but not when pulling from a file with gc? Both look identical to me but clear

$servers='aaa
bbb
ccc'
$a=0;
$servers -split '\n'|%{
write-host $a - $_;  
$a++
}

Open in new window


gc test1.txt
aaa
bbb
ccc
gc test1.txt|%{
write-host $a - $_;  
$a++
}

Open in new window

0
Comment
Question by:Mazdajai
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39881588
Try explicitly declaring the array

$servers= @('aaa','bbb','ccc')
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 39881592
>>Try explicitly declaring the array

Typing 100+ items with single quote and comma would be working backward.

I would like the list in original format for portability purpose.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39881599
Is there some reason you have to type these in ?
What are you trying to do and why can't your list be populated dynamically ?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 21

Author Comment

by:Mazdajai
ID: 39881611
Most list I work with is often one per each line, therefore declaring an array would not work.

I can put them in a file and use gc but for some something quick I would like to do it in one shot. It came across that the split option is needed for variable and there I would like to know, out of curiosity.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39881640
If you've got an array there must be a way to determine an element

A comma works I used the quote to let powershell know its a string

You don't need to use split if a known delimiter is used with each element.
0
 
LVL 39

Accepted Solution

by:
footech earned 200 total points
ID: 39881673
In your first example, $servers is just a string that happens to span multiple lines.  There's nothing that says a string can only be one line.
On the other hand, gc produces an array from a multi-line file.
0
 
LVL 29

Assisted Solution

by:becraig
becraig earned 200 total points
ID: 39881701
The quick answer is your example wraps all the entries (carriage return or not) as a single string.
Eg: $var = "the lazy brown Fox"
Is the same as
$var = "the
Lazy
Brown
Fox"
More information on arrays
 http://ss64.com/ps/syntax-arrays.html
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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