Powershell skip element in array, if it blank
I have a powershell script, where I receive names of elements as a variables from Jenkins:
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in $IISarray)
"some code goes here"
Sometimes random elements can be blank. How can I add a check to see if the current element in array is blank, skip it and go to next element?
powershell jenkins condition
add a comment |
I have a powershell script, where I receive names of elements as a variables from Jenkins:
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in $IISarray)
"some code goes here"
Sometimes random elements can be blank. How can I add a check to see if the current element in array is blank, skip it and go to next element?
powershell jenkins condition
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
1
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19
add a comment |
I have a powershell script, where I receive names of elements as a variables from Jenkins:
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in $IISarray)
"some code goes here"
Sometimes random elements can be blank. How can I add a check to see if the current element in array is blank, skip it and go to next element?
powershell jenkins condition
I have a powershell script, where I receive names of elements as a variables from Jenkins:
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in $IISarray)
"some code goes here"
Sometimes random elements can be blank. How can I add a check to see if the current element in array is blank, skip it and go to next element?
powershell jenkins condition
powershell jenkins condition
edited Nov 13 '18 at 17:11
Ravindranath Barathy
521418
521418
asked Nov 13 '18 at 17:05
Vasiliy VegasVasiliy Vegas
395116
395116
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
1
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19
add a comment |
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
1
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
1
1
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19
add a comment |
3 Answers
3
active
oldest
votes
It's easiest to use -ne ''
to created a filtered copy of the array that excludes empty entries, courtesy of the ability of many PowerShell operators to act as a filter with an array-valued LHS.
Note: I'm assuming you mean to filter out empty strings, not also blank (all-whitespace) ones, given that undefined environment variables expand to an empty string.
# Sample array with empty elements.
# Note: No need for @(...)
$IISarray = "foo", "", "bar", "baz", ""
# Note the `-ne ''`, which filters out empty elements.
foreach ($string in $IISarray -ne '')
$string # echo
The above yields:
foo
bar
baz
soundstripe's answer offers a Where-Object
solution, which potentially provides added flexibility via the ability to specify an arbitrary filter script block, but the use of a pipeline is a bit heavy-handed for this use case.
Fortunately, PSv4+ offers the .Where()
collection method, which performs noticeably better.
Let me demonstrate it with a solution that also rules out blank (all-whitespace) elements:
# Note the all-whitespace element, which we want to ignore too.
PS> ("foo", " ", "bar", "baz", "").Where( $_.Trim() )
foo
bar
baz
Similar to the Where-Object
cmdlet, you pass a script block to the .Where()
method, inside of which the automatic $_
variable represents the input element at hand.
The .Trim()
method trims leading and trailing whitespace from a string and returns the result.
An all-whitespace string therefore results in the empty string.
In a Boolean context (as the .Where()
method script block implicitly is), the empty string evaluates to $false
, whereas any non-empty string is $true
.
You can choose to be explicit, however ($_.Trim() -ne ''
), or even use a .NET method ([string]::IsNullOrWhiteSpace($_)
).
add a comment |
You can use Where-Object
to filter out null or empty values. It is very commonly used, so ?
is shorthand for Where-Object
.
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in ($IISarray | ? $_))
"some code goes here"
The $_
is an automatic variable representing each incoming object in the pipeline. Both $null
and the empty string ''
are falsy in Powershell, so only non-null values with length > 0 will be passed in to your for loop.
add a comment |
# you can skip the `@` and brackets as well as the quotation marks
$IISarray = $ENV:Cashier_NAME, $ENV:Terminal_NAME, $ENV:Content_Manager_NAME, $ENV:Kiosk_BO_NAME
foreach($String in $IISarray)
# trim the strings and check the length
if($String.Trim().Length -gt 0)
"some code goes here"
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286156%2fpowershell-skip-element-in-array-if-it-blank%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's easiest to use -ne ''
to created a filtered copy of the array that excludes empty entries, courtesy of the ability of many PowerShell operators to act as a filter with an array-valued LHS.
Note: I'm assuming you mean to filter out empty strings, not also blank (all-whitespace) ones, given that undefined environment variables expand to an empty string.
# Sample array with empty elements.
# Note: No need for @(...)
$IISarray = "foo", "", "bar", "baz", ""
# Note the `-ne ''`, which filters out empty elements.
foreach ($string in $IISarray -ne '')
$string # echo
The above yields:
foo
bar
baz
soundstripe's answer offers a Where-Object
solution, which potentially provides added flexibility via the ability to specify an arbitrary filter script block, but the use of a pipeline is a bit heavy-handed for this use case.
Fortunately, PSv4+ offers the .Where()
collection method, which performs noticeably better.
Let me demonstrate it with a solution that also rules out blank (all-whitespace) elements:
# Note the all-whitespace element, which we want to ignore too.
PS> ("foo", " ", "bar", "baz", "").Where( $_.Trim() )
foo
bar
baz
Similar to the Where-Object
cmdlet, you pass a script block to the .Where()
method, inside of which the automatic $_
variable represents the input element at hand.
The .Trim()
method trims leading and trailing whitespace from a string and returns the result.
An all-whitespace string therefore results in the empty string.
In a Boolean context (as the .Where()
method script block implicitly is), the empty string evaluates to $false
, whereas any non-empty string is $true
.
You can choose to be explicit, however ($_.Trim() -ne ''
), or even use a .NET method ([string]::IsNullOrWhiteSpace($_)
).
add a comment |
It's easiest to use -ne ''
to created a filtered copy of the array that excludes empty entries, courtesy of the ability of many PowerShell operators to act as a filter with an array-valued LHS.
Note: I'm assuming you mean to filter out empty strings, not also blank (all-whitespace) ones, given that undefined environment variables expand to an empty string.
# Sample array with empty elements.
# Note: No need for @(...)
$IISarray = "foo", "", "bar", "baz", ""
# Note the `-ne ''`, which filters out empty elements.
foreach ($string in $IISarray -ne '')
$string # echo
The above yields:
foo
bar
baz
soundstripe's answer offers a Where-Object
solution, which potentially provides added flexibility via the ability to specify an arbitrary filter script block, but the use of a pipeline is a bit heavy-handed for this use case.
Fortunately, PSv4+ offers the .Where()
collection method, which performs noticeably better.
Let me demonstrate it with a solution that also rules out blank (all-whitespace) elements:
# Note the all-whitespace element, which we want to ignore too.
PS> ("foo", " ", "bar", "baz", "").Where( $_.Trim() )
foo
bar
baz
Similar to the Where-Object
cmdlet, you pass a script block to the .Where()
method, inside of which the automatic $_
variable represents the input element at hand.
The .Trim()
method trims leading and trailing whitespace from a string and returns the result.
An all-whitespace string therefore results in the empty string.
In a Boolean context (as the .Where()
method script block implicitly is), the empty string evaluates to $false
, whereas any non-empty string is $true
.
You can choose to be explicit, however ($_.Trim() -ne ''
), or even use a .NET method ([string]::IsNullOrWhiteSpace($_)
).
add a comment |
It's easiest to use -ne ''
to created a filtered copy of the array that excludes empty entries, courtesy of the ability of many PowerShell operators to act as a filter with an array-valued LHS.
Note: I'm assuming you mean to filter out empty strings, not also blank (all-whitespace) ones, given that undefined environment variables expand to an empty string.
# Sample array with empty elements.
# Note: No need for @(...)
$IISarray = "foo", "", "bar", "baz", ""
# Note the `-ne ''`, which filters out empty elements.
foreach ($string in $IISarray -ne '')
$string # echo
The above yields:
foo
bar
baz
soundstripe's answer offers a Where-Object
solution, which potentially provides added flexibility via the ability to specify an arbitrary filter script block, but the use of a pipeline is a bit heavy-handed for this use case.
Fortunately, PSv4+ offers the .Where()
collection method, which performs noticeably better.
Let me demonstrate it with a solution that also rules out blank (all-whitespace) elements:
# Note the all-whitespace element, which we want to ignore too.
PS> ("foo", " ", "bar", "baz", "").Where( $_.Trim() )
foo
bar
baz
Similar to the Where-Object
cmdlet, you pass a script block to the .Where()
method, inside of which the automatic $_
variable represents the input element at hand.
The .Trim()
method trims leading and trailing whitespace from a string and returns the result.
An all-whitespace string therefore results in the empty string.
In a Boolean context (as the .Where()
method script block implicitly is), the empty string evaluates to $false
, whereas any non-empty string is $true
.
You can choose to be explicit, however ($_.Trim() -ne ''
), or even use a .NET method ([string]::IsNullOrWhiteSpace($_)
).
It's easiest to use -ne ''
to created a filtered copy of the array that excludes empty entries, courtesy of the ability of many PowerShell operators to act as a filter with an array-valued LHS.
Note: I'm assuming you mean to filter out empty strings, not also blank (all-whitespace) ones, given that undefined environment variables expand to an empty string.
# Sample array with empty elements.
# Note: No need for @(...)
$IISarray = "foo", "", "bar", "baz", ""
# Note the `-ne ''`, which filters out empty elements.
foreach ($string in $IISarray -ne '')
$string # echo
The above yields:
foo
bar
baz
soundstripe's answer offers a Where-Object
solution, which potentially provides added flexibility via the ability to specify an arbitrary filter script block, but the use of a pipeline is a bit heavy-handed for this use case.
Fortunately, PSv4+ offers the .Where()
collection method, which performs noticeably better.
Let me demonstrate it with a solution that also rules out blank (all-whitespace) elements:
# Note the all-whitespace element, which we want to ignore too.
PS> ("foo", " ", "bar", "baz", "").Where( $_.Trim() )
foo
bar
baz
Similar to the Where-Object
cmdlet, you pass a script block to the .Where()
method, inside of which the automatic $_
variable represents the input element at hand.
The .Trim()
method trims leading and trailing whitespace from a string and returns the result.
An all-whitespace string therefore results in the empty string.
In a Boolean context (as the .Where()
method script block implicitly is), the empty string evaluates to $false
, whereas any non-empty string is $true
.
You can choose to be explicit, however ($_.Trim() -ne ''
), or even use a .NET method ([string]::IsNullOrWhiteSpace($_)
).
edited Nov 13 '18 at 18:49
answered Nov 13 '18 at 17:17
mklement0mklement0
128k20241270
128k20241270
add a comment |
add a comment |
You can use Where-Object
to filter out null or empty values. It is very commonly used, so ?
is shorthand for Where-Object
.
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in ($IISarray | ? $_))
"some code goes here"
The $_
is an automatic variable representing each incoming object in the pipeline. Both $null
and the empty string ''
are falsy in Powershell, so only non-null values with length > 0 will be passed in to your for loop.
add a comment |
You can use Where-Object
to filter out null or empty values. It is very commonly used, so ?
is shorthand for Where-Object
.
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in ($IISarray | ? $_))
"some code goes here"
The $_
is an automatic variable representing each incoming object in the pipeline. Both $null
and the empty string ''
are falsy in Powershell, so only non-null values with length > 0 will be passed in to your for loop.
add a comment |
You can use Where-Object
to filter out null or empty values. It is very commonly used, so ?
is shorthand for Where-Object
.
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in ($IISarray | ? $_))
"some code goes here"
The $_
is an automatic variable representing each incoming object in the pipeline. Both $null
and the empty string ''
are falsy in Powershell, so only non-null values with length > 0 will be passed in to your for loop.
You can use Where-Object
to filter out null or empty values. It is very commonly used, so ?
is shorthand for Where-Object
.
$IISarray = @("$ENV:Cashier_NAME", "$ENV:Terminal_NAME", "$ENV:Content_Manager_NAME", "$ENV:Kiosk_BO_NAME")
foreach ($string in ($IISarray | ? $_))
"some code goes here"
The $_
is an automatic variable representing each incoming object in the pipeline. Both $null
and the empty string ''
are falsy in Powershell, so only non-null values with length > 0 will be passed in to your for loop.
edited Nov 13 '18 at 18:55
answered Nov 13 '18 at 17:25
soundstripesoundstripe
49138
49138
add a comment |
add a comment |
# you can skip the `@` and brackets as well as the quotation marks
$IISarray = $ENV:Cashier_NAME, $ENV:Terminal_NAME, $ENV:Content_Manager_NAME, $ENV:Kiosk_BO_NAME
foreach($String in $IISarray)
# trim the strings and check the length
if($String.Trim().Length -gt 0)
"some code goes here"
add a comment |
# you can skip the `@` and brackets as well as the quotation marks
$IISarray = $ENV:Cashier_NAME, $ENV:Terminal_NAME, $ENV:Content_Manager_NAME, $ENV:Kiosk_BO_NAME
foreach($String in $IISarray)
# trim the strings and check the length
if($String.Trim().Length -gt 0)
"some code goes here"
add a comment |
# you can skip the `@` and brackets as well as the quotation marks
$IISarray = $ENV:Cashier_NAME, $ENV:Terminal_NAME, $ENV:Content_Manager_NAME, $ENV:Kiosk_BO_NAME
foreach($String in $IISarray)
# trim the strings and check the length
if($String.Trim().Length -gt 0)
"some code goes here"
# you can skip the `@` and brackets as well as the quotation marks
$IISarray = $ENV:Cashier_NAME, $ENV:Terminal_NAME, $ENV:Content_Manager_NAME, $ENV:Kiosk_BO_NAME
foreach($String in $IISarray)
# trim the strings and check the length
if($String.Trim().Length -gt 0)
"some code goes here"
answered Nov 13 '18 at 17:19
Guenther SchmitzGuenther Schmitz
8801314
8801314
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286156%2fpowershell-skip-element-in-array-if-it-blank%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How about this? if ($varibalename -eq $null) Write-Host "variable is null"
– Ravindranath Barathy
Nov 13 '18 at 17:14
1
$IISarray | where-Object $_ | Foreach-Object {...
– iRon
Nov 13 '18 at 17:19