VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Check VM validating failing

Hi,

I am using the below script to validate if the VM exists, if the VM is valid, the script works fine but for invalid VMs, it is not checking nor I am getting the error message which I defined in the script, directly, I am getting error

$report = @()

$report = Import-Csv ".\VMNotes0.csv"

foreach ($VMAttrib in $report){

if (!$VMAttrib) {

Write-Host "No VM named $VM exists" }

else

{Get-VM $VMAttrib.Name | Set-VM -Notes $VMAttrib.Notes -confirm:$false}

}

Error :

Get-VM : 09-08-2019 17:49:16Get-VM      VM with name 'MyDB10' was not found using the specified filter(s).

At D:\export\3.ps1:9 char:2

+ {Get-VM $VMAttrib.Name | Set-VM -Notes $VMAttrib.Notes -confirm:$fals ...

+  ~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo      : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($row in (Import-Csv ".\VMNotes0.csv")){

   try{

     Get-VM -Name $row.Name -ErrorAction Stop |

     Set-VM -Notes $row.Notes -Confirm:$false

   }

   catch{

     Write-Host "No VM named $($row.Name) exists"

   }

}

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

foreach($row in (Import-Csv ".\VMNotes0.csv")){

   try{

     Get-VM -Name $row.Name -ErrorAction Stop |

     Set-VM -Notes $row.Notes -Confirm:$false

   }

   catch{

     Write-Host "No VM named $($row.Name) exists"

   }

}

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked LucD. Thank you very much Smiley Happy

0 Kudos