VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

finding and deleting snapshot_powercli(get-view)

Hi Luc ,

could you check the following code. this is to find vms with snapshot in cluster using get-view commands and then delet it .

i have issue in blue color line where in i want to call method to delete snapshot as i cant use $vm_snapshot|remove-snapshot .

what method and how to find it in api explorer or anywhere else.

also this is how  i want to remediate our environent and this is one of the requirements.

$conn=$global:defaultviserver

if ($conn -cne $null)

{

write-output "there is already session to" $conn.name

disconnect-viserver "*" -force -confirm:$true

}

$vc=read-host "provide the vcenter name"

connect-viserver -server $vc -username "administrator@vsphere.local" -password ""

$cluster ="provide the cluster name"

$vm_snapshot=Get-View -ViewType VirtualMachine -Property Name,snapshot -SearchRoot $cluster.MoRef|?{$_.snapshot -cne $null}

$vm_snapshot.name

$rem=read-host "do yu want to remediate it"

if ($rem -eq "yes")

{

write-output "deleting snapshot please wait"

$vm_snapshot.RemoveAllSnapshots

}

thanks for your help .

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.

A method call shall always have parenthesis, and this specific method requires 1 parameter, to say if consolidation is needed or not.

$conn=$global:defaultviserver

if ($conn -cne $null)

{

    Write-Output "there is already session to" $conn.name

    Disconnect-VIServer "*" -force -confirm:$true

}

$vc=Read-Host "provide the vcenter name"

Connect-VIServer -server $vc -username "administrator@vsphere.local" -password ""

$cluster ="provide the cluster name"

Get-View -ViewType VirtualMachine -Property Name,snapshot -SearchRoot $cluster.MoRef |?{$_.snapshot -cne $null} | %{

    $_.name

    

    $rem=Read-Host "do you want to remediate it"

    if ($rem -eq "yes")

    {

        Write-Output "deleting snapshot please wait"

        $_.RemoveAllSnapshots($true)

    }

}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.

A method call shall always have parenthesis, and this specific method requires 1 parameter, to say if consolidation is needed or not.

$conn=$global:defaultviserver

if ($conn -cne $null)

{

    Write-Output "there is already session to" $conn.name

    Disconnect-VIServer "*" -force -confirm:$true

}

$vc=Read-Host "provide the vcenter name"

Connect-VIServer -server $vc -username "administrator@vsphere.local" -password ""

$cluster ="provide the cluster name"

Get-View -ViewType VirtualMachine -Property Name,snapshot -SearchRoot $cluster.MoRef |?{$_.snapshot -cne $null} | %{

    $_.name

    

    $rem=Read-Host "do you want to remediate it"

    if ($rem -eq "yes")

    {

        Write-Output "deleting snapshot please wait"

        $_.RemoveAllSnapshots($true)

    }

}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks  Luc it worked.

is the below document  and powercli screenshot related to this method ?? .its little harder to understand initially.

vSphere Web Service API - VMware API Explorer - VMware {code}

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Correct, hat's where the methods on the vSphere objects are documented.

There is also a Programmer's Guide, that helps understanding the concept a bit.

But no PowerCLI examples I'm afraid.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks ,

iam checking this .

0 Kudos