Sunday, April 26, 2020

Using PowerShell to validate variable;

Using PowerShell to validate variable;
Here is one simple way to check if $variable does have value or not. This way is pretty simple and will be handy when adding  checks into your scripts.



   
# check if $myvar has a value
$myvar = 'DATA'
if(!$myvar){ Write-Host 'NULL' -ForegroundColor Red }


# Same check adding else;
if (!$myvar){

Write-Host 'NULL' -ForegroundColor Red

}else{

Write-Host 'NOT NULL!!!' -ForegroundColor Green

}


# Check if $myvar has <ANY> value except $null
$myvar
if($myvar){ Write-Host 'My var value is <NULL>' -ForegroundColor Red }


# Same check adding else;
if ($myvar){

Write-Host 'NULL' -ForegroundColor Red

}else{

Write-Host 'NOT NULL!!!' -ForegroundColor Green

}




Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate




No comments:

Post a Comment