VMware Cloud Community
vmitra
Enthusiast
Enthusiast

Power cli script validation

Hi Masters,

 

Need your help to get the output saved in Separate file for a validation

 

Scenario : 

For an example suppose we need to delete 100 vms from a script but 10 vms skipped due to some error;

So to do validation what changes we should do in a script below so we can get the list of skip vms in a separate file with error received.

 

Script ::

 

=============

Connect -viserver -server vcenter name 

 

Get-Content C:\temp\vmlist.txt | foreach { Remove-vm $_ -DeleteFromDisk - Confirm:$false

==≠====

 

Please ignore any typo mistake

Labels (1)
0 Kudos
3 Replies
LucD
Leadership
Leadership

You could use a try-catch construct to capture the exception and write it to a file.

But I'm not sure how you can address a property ($_.DeleteFromDisk) with a Get-COntent cmdlet

$errorLog = '.\error.txt'
if (Test-Path -Path $errorLog) {
  Remove-Item -Path $errorLog
}
Get-Content C:\temp\vmlist.txt |
ForEach-Object -Process {
  try {
    Remove-VM -VM $_ -ErrorAction Stop
  } catch {
    $error[0].Exception.Message | Out-File -FilePath $errorLog -Append
  }
}

 


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

vmitra
Enthusiast
Enthusiast

Hi Thanks 

We will check with your inputs !!

If you have similar script kindly share with us .

Thanks in Advance

0 Kudos
LucD
Leadership
Leadership

The snippet I posted earlier is an example of such a script.
Not sure what more your would need.
But take my earlier comment into account.


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