VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

Test whether a variable has anything in it so a script can continue - if($var1 -gt 0)

Hi,

I was wondering how you perform a test to see if a variable created from a previous command has got anything in it. My script keeps failing because it is comparing it to 0 with a if greater than.

For example I have:

$var = get-vm

if i want to test whether there is anything in it how do I make the following test:

If($var -gt 0)

{

$var

}

else

{

"nothing in variable"

}

It seems sometimes it works others it throws one of those red errors.

Thanks in advance for any help,

Dan

0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

I normaly use:

If ($var) {
	$var
} else {
	"nothing in variable"
}

If you want to do the oposite it would be:

If (-not $var) {
	$var
} else {
	"nothing in variable"
}

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
2 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

I normaly use:

If ($var) {
	$var
} else {
	"nothing in variable"
}

If you want to do the oposite it would be:

If (-not $var) {
	$var
} else {
	"nothing in variable"
}

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
a2alpha
Expert
Expert
Jump to solution

alanrenouf, exactly what I was after, thanks so much for the quick response.

Dan

0 Kudos