Solved

file vs variable in powershell

Posted on 2014-02-23
7
190 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 40

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

821 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