Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
Vasiliy Vegas Avatar asked Nov 13 '18 17:11

Vasiliy Vegas


People also ask

How do you remove Blank values from an array in PowerShell?

Remove and consider only $null as empty In PowerShell version 4 and up, you can use the LINQ-based, optimized (faster, but likely more memory-consuming) "Where" method on the array to remove the $null elements. You can of course also use the regular pipeline-based cmdlet Where-Object as you can in PowerShell v2.

Is null or empty check in PowerShell?

If you use $null in a string, then it's a blank value (or empty string).

What does @() mean in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.

What is $null in PowerShell?

In PowerShell, $null is an automatic variable that carries a NULL or unidentified value. Windows PowerShell considered $null as an object that carries a Null value. The $null value can be used with numeric equations, arrays, functions, etc.


2 Answers

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($_)).

like image 118
mklement0 Avatar answered Oct 16 '22 21:10

mklement0


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.

like image 45
soundstripe Avatar answered Oct 16 '22 21:10

soundstripe