Reputation: 1603
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?
Upvotes: 5
Views: 11329
Reputation: 437618
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 @(...), unless there's just *one* element.
$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($_)
).
Upvotes: 4
Reputation: 1474
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.
Upvotes: 3
Reputation: 1999
# 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"
}
}
Upvotes: 2