PowerShell script exchange
Post Reply
evilaedmin
Expert
Posts: 176
Liked: 30 times
Joined: Jul 26, 2018 8:04 pm
Full Name: Eugene V
Contact:

When not found with Find-VBRViEntity?

Post by evilaedmin »

Greetings,

What's the best way to detect if Find-VBRViEntity returned no results?

Code: Select all

$Result = Find-VBRViEntity -Name "VMthatdoesnotexit"
if (!$Result) { 
	//Do Some kind of counting here
}
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: When not found with Find-VBRViEntity?

Post by oleg.feoktistov » 3 people like this post

Hi Eugene,

By default, If the entity with the name you specified for Find-VBRViEntity cmdlet doesn't exist, powershell will just resolve an empty line.
If you want to configure some kind of message notifying you on the entity absence, I would suggest to use "throw" keyword in your if statement.
In this case, it will resolve a system-like error with a custom text you wrote.
Example:

Code: Select all

$ViEntityName = "VMDoesnotexist"
$Result = Find-VBRViEntity -Name $ViEntityName
if (!$Result) {
    throw "Entity with the name $ViEntityName is not found"
} 
else {
    $Result
}
All the best,
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests