Powershell function returning an array instead of string











up vote
2
down vote

favorite












i'm importing a csv and i would like to add a column to it (with the result based off of the previous columns)



my data looks like this



host address,host prefix,site
10.1.1.0,24,400-01


i would like to add a column called "sub site"



so I wrote this module but the problem is, the actual ending object is an array instead of string



function site {
Param($s)
$s -match '(ddd)'
return $Matches[0]
}

$csv = import-csv $file | select-object *,@{Name='Sub Site';expression= {site $_.site}}


if I run the command



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : {True,400}


when it should look like



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : 400


EDIT: I found the solution but the question is now WHY.



If I change my function to $s -match "ddd" |out-null I get back the expected 400










share|improve this question




















  • 2




    The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
    – Lieven Keersmaekers
    Nov 7 at 7:47






  • 2




    and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
    – Lieven Keersmaekers
    Nov 7 at 7:50










  • thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
    – genx1mx6
    Nov 8 at 8:22















up vote
2
down vote

favorite












i'm importing a csv and i would like to add a column to it (with the result based off of the previous columns)



my data looks like this



host address,host prefix,site
10.1.1.0,24,400-01


i would like to add a column called "sub site"



so I wrote this module but the problem is, the actual ending object is an array instead of string



function site {
Param($s)
$s -match '(ddd)'
return $Matches[0]
}

$csv = import-csv $file | select-object *,@{Name='Sub Site';expression= {site $_.site}}


if I run the command



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : {True,400}


when it should look like



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : 400


EDIT: I found the solution but the question is now WHY.



If I change my function to $s -match "ddd" |out-null I get back the expected 400










share|improve this question




















  • 2




    The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
    – Lieven Keersmaekers
    Nov 7 at 7:47






  • 2




    and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
    – Lieven Keersmaekers
    Nov 7 at 7:50










  • thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
    – genx1mx6
    Nov 8 at 8:22













up vote
2
down vote

favorite









up vote
2
down vote

favorite











i'm importing a csv and i would like to add a column to it (with the result based off of the previous columns)



my data looks like this



host address,host prefix,site
10.1.1.0,24,400-01


i would like to add a column called "sub site"



so I wrote this module but the problem is, the actual ending object is an array instead of string



function site {
Param($s)
$s -match '(ddd)'
return $Matches[0]
}

$csv = import-csv $file | select-object *,@{Name='Sub Site';expression= {site $_.site}}


if I run the command



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : {True,400}


when it should look like



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : 400


EDIT: I found the solution but the question is now WHY.



If I change my function to $s -match "ddd" |out-null I get back the expected 400










share|improve this question















i'm importing a csv and i would like to add a column to it (with the result based off of the previous columns)



my data looks like this



host address,host prefix,site
10.1.1.0,24,400-01


i would like to add a column called "sub site"



so I wrote this module but the problem is, the actual ending object is an array instead of string



function site {
Param($s)
$s -match '(ddd)'
return $Matches[0]
}

$csv = import-csv $file | select-object *,@{Name='Sub Site';expression= {site $_.site}}


if I run the command



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : {True,400}


when it should look like



PS C:>$csv[0]

Host Address :10.1.1.0
host prefix :24
site :400-01
sub site : 400


EDIT: I found the solution but the question is now WHY.



If I change my function to $s -match "ddd" |out-null I get back the expected 400







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 7:45

























asked Nov 7 at 7:36









genx1mx6

14528




14528








  • 2




    The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
    – Lieven Keersmaekers
    Nov 7 at 7:47






  • 2




    and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
    – Lieven Keersmaekers
    Nov 7 at 7:50










  • thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
    – genx1mx6
    Nov 8 at 8:22














  • 2




    The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
    – Lieven Keersmaekers
    Nov 7 at 7:47






  • 2




    and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
    – Lieven Keersmaekers
    Nov 7 at 7:50










  • thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
    – genx1mx6
    Nov 8 at 8:22








2




2




The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
– Lieven Keersmaekers
Nov 7 at 7:47




The $s -match '(ddd)' outputs True. That gets passed to the pipeline if you do not suppress it.
– Lieven Keersmaekers
Nov 7 at 7:47




2




2




and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
– Lieven Keersmaekers
Nov 7 at 7:50




and fwiw - if you subsite is always 3 characters like your regex shows, following woud do without the need of an extra function *,@{Name='Sub Site';expression= {$_.site.SubString(0,3)}}
– Lieven Keersmaekers
Nov 7 at 7:50












thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
– genx1mx6
Nov 8 at 8:22




thanks, great idea. However my case is a bit more complicated then I actually posted so it isn't always at the front!
– genx1mx6
Nov 8 at 8:22












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Good you found the answer. I was typing this up as you found it. The reason is because the -match returns a value and it is added to the pipeline, which is all "returned" from the function.



For example, run this one line and see what is does:



"Hello" -match 'h'


It prints True.



Since I had this typed up, here is another way to phrase your question with the fix...



function site {
Param($s)
$null = $s -match '(ddd)'
$ret = $Matches[0]

return $ret
}

$csv = @"
host address,host prefix,site
10.1.1.1,24,400-01
10.1.1.2,24,500-02
10.1.1.3,24,600-03
"@

$data = $csv | ConvertFrom-Csv

'1 =============='
$data | ft -AutoSize

$data2 = $data | select-object *,@{Name='Sub Site';expression= {site $_.site}}

'2 =============='
$data2 | ft -AutoSize





share|improve this answer





















  • Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
    – genx1mx6
    Nov 8 at 8:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185186%2fpowershell-function-returning-an-array-instead-of-string%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Good you found the answer. I was typing this up as you found it. The reason is because the -match returns a value and it is added to the pipeline, which is all "returned" from the function.



For example, run this one line and see what is does:



"Hello" -match 'h'


It prints True.



Since I had this typed up, here is another way to phrase your question with the fix...



function site {
Param($s)
$null = $s -match '(ddd)'
$ret = $Matches[0]

return $ret
}

$csv = @"
host address,host prefix,site
10.1.1.1,24,400-01
10.1.1.2,24,500-02
10.1.1.3,24,600-03
"@

$data = $csv | ConvertFrom-Csv

'1 =============='
$data | ft -AutoSize

$data2 = $data | select-object *,@{Name='Sub Site';expression= {site $_.site}}

'2 =============='
$data2 | ft -AutoSize





share|improve this answer





















  • Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
    – genx1mx6
    Nov 8 at 8:21















up vote
1
down vote



accepted










Good you found the answer. I was typing this up as you found it. The reason is because the -match returns a value and it is added to the pipeline, which is all "returned" from the function.



For example, run this one line and see what is does:



"Hello" -match 'h'


It prints True.



Since I had this typed up, here is another way to phrase your question with the fix...



function site {
Param($s)
$null = $s -match '(ddd)'
$ret = $Matches[0]

return $ret
}

$csv = @"
host address,host prefix,site
10.1.1.1,24,400-01
10.1.1.2,24,500-02
10.1.1.3,24,600-03
"@

$data = $csv | ConvertFrom-Csv

'1 =============='
$data | ft -AutoSize

$data2 = $data | select-object *,@{Name='Sub Site';expression= {site $_.site}}

'2 =============='
$data2 | ft -AutoSize





share|improve this answer





















  • Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
    – genx1mx6
    Nov 8 at 8:21













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Good you found the answer. I was typing this up as you found it. The reason is because the -match returns a value and it is added to the pipeline, which is all "returned" from the function.



For example, run this one line and see what is does:



"Hello" -match 'h'


It prints True.



Since I had this typed up, here is another way to phrase your question with the fix...



function site {
Param($s)
$null = $s -match '(ddd)'
$ret = $Matches[0]

return $ret
}

$csv = @"
host address,host prefix,site
10.1.1.1,24,400-01
10.1.1.2,24,500-02
10.1.1.3,24,600-03
"@

$data = $csv | ConvertFrom-Csv

'1 =============='
$data | ft -AutoSize

$data2 = $data | select-object *,@{Name='Sub Site';expression= {site $_.site}}

'2 =============='
$data2 | ft -AutoSize





share|improve this answer












Good you found the answer. I was typing this up as you found it. The reason is because the -match returns a value and it is added to the pipeline, which is all "returned" from the function.



For example, run this one line and see what is does:



"Hello" -match 'h'


It prints True.



Since I had this typed up, here is another way to phrase your question with the fix...



function site {
Param($s)
$null = $s -match '(ddd)'
$ret = $Matches[0]

return $ret
}

$csv = @"
host address,host prefix,site
10.1.1.1,24,400-01
10.1.1.2,24,500-02
10.1.1.3,24,600-03
"@

$data = $csv | ConvertFrom-Csv

'1 =============='
$data | ft -AutoSize

$data2 = $data | select-object *,@{Name='Sub Site';expression= {site $_.site}}

'2 =============='
$data2 | ft -AutoSize






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 7:51









Kory Gill

4,8711824




4,8711824












  • Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
    – genx1mx6
    Nov 8 at 8:21


















  • Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
    – genx1mx6
    Nov 8 at 8:21
















Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
– genx1mx6
Nov 8 at 8:21




Thanks, I also thought of another way and that's to take the last index of the array that gets returned so site $($_.site)[-1] However I think I like your solution more since you aren't guaranteed to get an array back from every function.
– genx1mx6
Nov 8 at 8:21


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185186%2fpowershell-function-returning-an-array-instead-of-string%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()