Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file vs variable in powershell

Posted on 2014-02-23
7
Medium Priority
?
196 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 41

Accepted Solution

by:
footech earned 800 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 800 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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

916 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