Exit script if any one of three 'if' statements are true

Hi All,

We’re doing a VDI deployment and several people / teams are involved. Bits and pieces of builds are getting done left, right and centre. I can see it being a disaster.

I’ve started on a VDI build script. I would like it to check that no entries already exist in AD, Citrix or VMware before proceeding with the build process

Here’s a simplified version of it:

$vdis = Import-Csv C:\test.csv

foreach ($vdi in $vdis) { $n = $vdi.name
if (Get-ADComputer -identity $n) {"$n is already in AD."}
if (Get-BrokerMachine -HostedMachineName $n) {"$n is already in Citrix."}
if (Get-ProvVM -VMName $n) {"$n is already in VMware."}

This works fine. I’m finding objects that exist in AD but not in VMware etc.etc.

However, after it’s found all its entries, I’d like to add a ‘Press any key to exit’ so I can investigate and delete the various objects (or just remove the relevant spread sheet entries).

If no entries are found at all, I’d like it to proceed with going through the build process (New-ADComputer -Name $n, New-ProvVm -VMName $n etc.etc)

In principle, I guess I’m trying to add a big ‘IF’ statement at the beginning to look for any existing entries and then a big ‘ELSE’ at the end (to move on to the building phase) if no entries are found across the three products.

Anyone got any ideas? I’m not having much luck with this.




How about this. Please note there are 2 options in my script. Either check one at the time, or check all in one statement. I threw ErrorAction in there, however I do not know if you are using advanced cmdlets or just regular functions:


you can add Break in your if statements to exit a loop


Thanks Tore / Mark,

Tore - I’ve tried your suggestion, but this part is giving me a false positive:

if ($AdComputer) {
Write-Host -ForegroundColor Green “$n already exists in AD.”

It tells me that the computer account exists in AD, when it doesn’t.

Mark - thanks for the tip on the break. I actually want the entire spread sheet of say 100 machines to be checked and provide a single list of objects that have already been created.

I guess I could break it into two separate scripts. The first script would check for any existing objects across the three products and the 2nd script to build the AD/VMware/Citrix elements once the clean-up has been done. I just thought it’d be nice to be able to run it, identify any issues, correct them and then run it again to build the VDI machines.

If any of your three IF statements are true, you want to skip that specific item. Otherwise, you want to carry out some action(s) immediately on the item.

Do I understand correctly?

Ahh. You could set a flag at the beginning of the script:
[bool]$FoundErrors = $false

In your IF statements, include $FoundErrors = $True as a statement
Ex. If …{"$n is already in AD."; $FoundErrors = $True}

After the ForEach loop,
If ($FoundErrors) { “There are Errors !”; break }

OK, I’ll look into doing the flag thing. Thanks.

Just to answer your previous question and clarify the requirements a bit more…

If any one of my three IF statements are true, then I want that specific condition to be output on to the screen (i.e. if the computer account exists in AD, then I want it to tell me that it exists in AD. Same for any that exist in Citrix and VMware).

At that point I would like a list of objects that I need to investigate (whether AD, Citrix or VMware) to be printed on the screen and then a ‘Press any key to exit’ so I can stop the script at that point.

I’ll delete the AD accounts / Virtual Machines / XenDesktop entries (or possibly just remove the lines from the csv file if appropriate).

Once that clean-up has been done, I’ll run the script again and it then shouldn’t find any existing entries and should proceed straight on to the build phase and create the AD, VMware and Citrix objects for the entire list of machines.

You might get a kick out of this.

BTW “Get-ADComputer -ident” throws an error even if -ea silentlyContinue is user. Using -filter “blah blah” will not throw an error.

$vdis = Import-Csv C:\test.csv

$Errors = @(
foreach ($vdi in $vdis) {
  $n = $vdi.name

  $ADComputer = If (Get-ADComputer -filter "name -eq '$n'") { $True } Else { $False }
  $Citrix = If (Get-BrokerMachine -HostedMachineName $n) { $True } Else { $False } #-ErrorAction SilentlyContinue
  $VmWare = If (Get-ProvVM -VMName $n) { $True } Else { $False } #-ErrorAction SilentlyContinue

  if($ADComputer -or $Citrix -or $VmWare) {
        name = $n
        ADExists = $ADComputer
        CitrixExists = $Citrix
        VmWareExists = $VmWare

If ($Errors) { $Errors | Out-GridView }

Thanks so much to both of you! I really appreciate your help.

Mark - yes your Out-GridView idea was a great suggestion.

This is working perfectly now and I managed to build 200 new machines at the weekend with no problems at all.

Thanks again to both of you. I’ve got a whole bunch of really happy users here!!!