VMware Cloud Community
sford19
Contributor
Contributor
Jump to solution

check to see if VM's exists in multiple vcenters

I am building a script to grab DNS A record > ping them all > if they do not ping write to a file > get-vm cmdlet > if they do not exists write to another file.

Below is the part that is not working. It is writing all the VM's to the file. Can someone help me get this to work. I have tried try catch loops as well and can not get it to work.

Connect-VIServer $vcenter -User $username -Password $password

$Exists = Import-Csv C:\tmp\test1.csv

$array = @()

foreach ($VMName in $Exists){

$not = get-vm -name $VMName -ErrorAction SilentlyContinue 

$writeme = $VMName

If ($not){

  #   Write "VM is there $VMName" 

}

Else {

#    Write "VM not there $VMName $writeme" 

    $array += $writeme

}

}

$array | Export-Csv c:\tmp\test2.csv -NoTypeInformation -UseCulture

Disconnect-VIServer $vcenter -Confirm $false -Force

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is probably because of the layout of your input CSV file.

If it looks like this

Name

vm1

vm2

The following script should do the trick

Connect-VIServer $vcenter -User $username -Password $password

$Exists = Import-Csv C:\tmp\test1.csv

$array = @()

foreach ($VM in $Exists){

    $not = get-vm -name $VM.Name -ErrorAction SilentlyContinue

    If ($not){

          #   Write "VM is there $VMName"

    }

    Else {

        #    Write "VM not there $VMName $writeme"

            $array += $VM.Name

    }

}

$array | Export-Csv c:\test2.csv -NoTypeInformation -UseCulture

Disconnect-VIServer $vcenter -Confirm $false -Force


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

I hope I understood the question correctly.

Try like this

Connect-VIServer $vcenter -User $username -Password $password

$Exists = Import-Csv C:\tmp\test1.csv

$array = @()

foreach ($VMName in $Exists){

    $not = get-vm -name $VMName -ErrorAction SilentlyContinue

    If ($not){

          #   Write "VM is there $VMName"

    }

    Else {

        #    Write "VM not there $VMName $writeme"

            $array += $VMName

    }

}

$array | Export-Csv c:\tmp\test2.csv -NoTypeInformation -UseCulture

Disconnect-VIServer $vcenter -Confirm $false -Force


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sford19
Contributor
Contributor
Jump to solution

LucD

It did the same thing. It adds to the array when the vm is their or not. I need it to only add the vm to the array if the vm is not in vCenter.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is probably because of the layout of your input CSV file.

If it looks like this

Name

vm1

vm2

The following script should do the trick

Connect-VIServer $vcenter -User $username -Password $password

$Exists = Import-Csv C:\tmp\test1.csv

$array = @()

foreach ($VM in $Exists){

    $not = get-vm -name $VM.Name -ErrorAction SilentlyContinue

    If ($not){

          #   Write "VM is there $VMName"

    }

    Else {

        #    Write "VM not there $VMName $writeme"

            $array += $VM.Name

    }

}

$array | Export-Csv c:\test2.csv -NoTypeInformation -UseCulture

Disconnect-VIServer $vcenter -Confirm $false -Force


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sford19
Contributor
Contributor
Jump to solution

it was the layout but i still need the variable in-between the array and the csv for some reason or it would only print the number of characters.


$Exists = Import-Csv C:\tmp\noping.csv -Header "VMs"

$array = @()

foreach ($VMName in $Exists){

$not = get-vm -name $VMName.VMs -ErrorAction SilentlyContinue

If ($not){ 

Else

     Write "VM not there $VMName $writeme"

   $writeme = $VMName

   $array += $writeme

}

$array | Export-Csv c:\tmp\nodns.csv -NoTypeInformation -UseCulture

Disconnect-VIServer "vcenter" -Confirm $false -Force

0 Kudos