VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

https://communities.vmware.com/thread/600605_extension

Hi Luc,

good morning

this is extension of previous thread control and action in function_powercli

while your code is perfectly fine i thought of putting more control using if loop .would you like to review it?

especialy colored line .

orange:instead of saving in empty array we can get snpshot information using one liner as powershell has lready done for us .

blue:cant we use get-vm $listofvms(its an array of vms provided by user)

function get-listofsnapshotremovl

{

$snap=get-vm -location cluster1,cluster2,cluster3,cluster4|Get-Snapshot

if($snap -ne $null)

    {

        $snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}

        $res = Read-Host "do yu want to delete all snapshot y/n/partial"

        if($res -eq 'y')

        {

            #Write-Host "deleting snapshot"

            Write-Host -NoNewline '#'

            #$snap | Remove-Snapshot -WhatIf

        }

       

       

        if($res -eq 'partial')

{

    $listofvms = @()

    $answer = ''

    while($answer -ne 'q'){

        $answer = Read-Host "speclify list of vms for which you wnt to delete snpshots(end with Q)"

        if($answer -ne 'q'){

            $listofvms += $answer

        }

    }

      

       

       

       

        write-host "deleting snapshot" -NoNewline 'test' -ForegroundColor DarkCyan

        foreach($vm in $listofvms)

        {

        $v=get-vm $vm

        $v|Get-Snapshot|Remove-Snapshot -WhatIf

        }

       

      

        }

        if($res -eq 'n')

        {

        write-host "yu have chosen not to delete any snpshots right now see you again" -ForegroundColor Blue

        }

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Yes on both questions.

You can do like this

function get-listofsnapshotremovl

{

    $snap = Get-VM -Location cluster1,cluster2,cluster3,cluster4 | Get-Snapshot

    if($snap -ne $null)

    {

        $snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}

        $res = Read-Host "do yu want to delete all snapshot y/n/partial"

        if($res -eq 'y')

        {

            Write-Host "deleting snapshot"

            $snap | Remove-Snapshot -Confirm:$false -WhatIf

        }

        if($res -eq 'partial')

        {

            $listofvms = @()

            $answer = ''

            while($answer -ne 'q'){

                $answer = Read-Host "speclify list of vms for which you wnt to delete snpshots(end with Q)"

                if($answer -ne 'q'){

                    $listofvms += $answer

                 }

            }

            write-host "deleting snapshot" -NoNewline 'test' -ForegroundColor DarkCyan

            Get-VM -Name $listofvms | Get-Snapshot | Remove-Snapshot -Confirm:$false -WhatIf

        }

        if($res -eq 'n')

        {

            write-host "yu have chosen not to delete any snpshots right now see you again" -ForegroundColor Blue

        }

    }

}


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

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Yes on both questions.

You can do like this

function get-listofsnapshotremovl

{

    $snap = Get-VM -Location cluster1,cluster2,cluster3,cluster4 | Get-Snapshot

    if($snap -ne $null)

    {

        $snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}

        $res = Read-Host "do yu want to delete all snapshot y/n/partial"

        if($res -eq 'y')

        {

            Write-Host "deleting snapshot"

            $snap | Remove-Snapshot -Confirm:$false -WhatIf

        }

        if($res -eq 'partial')

        {

            $listofvms = @()

            $answer = ''

            while($answer -ne 'q'){

                $answer = Read-Host "speclify list of vms for which you wnt to delete snpshots(end with Q)"

                if($answer -ne 'q'){

                    $listofvms += $answer

                 }

            }

            write-host "deleting snapshot" -NoNewline 'test' -ForegroundColor DarkCyan

            Get-VM -Name $listofvms | Get-Snapshot | Remove-Snapshot -Confirm:$false -WhatIf

        }

        if($res -eq 'n')

        {

            write-host "yu have chosen not to delete any snpshots right now see you again" -ForegroundColor Blue

        }

    }

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks Luc,

It worked absolutely fine .

however i am not able to understand why following is not working .just changed blue to orange in order to test.(though not good to repeat what has already been printed through $snap but just to check)

function get-listofsnapshotremovl

{

    $snap = Get-VM -Location wars,pias,lndc_mgmt,mopecluster01,grbcluster01 | Get-Snapshot

    if($snap -ne $null)

    {

        $snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}

        $res = Read-Host "do yu want to delete all snapshot y/n/partial"

        if($res -eq 'y')

        {

            Write-Host "deleting snapshot"

            $snap | Remove-Snapshot -Confirm:$false -WhatIf

        }

        if($res -eq 'partial')

        {

            $listofvms = @()

            $answer = ''

            while($answer -ne 'q'){

                $answer = Read-Host "speclify list of vms for which you wnt to delete snpshots(end with Q)"

                if($answer -ne 'q'){

                    $listofvms += $answer

                 }

            }

            write-host "deleting snapshot" -NoNewline 'test' -ForegroundColor DarkCyan

            #Get-VM -Name $listofvms | Get-Snapshot | Remove-Snapshot -Confirm:$false -WhatIf

           $snappartial=get-vm $listofvms|get-snapshot

            $snappartial

        }

        if($res -eq 'n')

        {

            write-host "yu have chosen not to delete any snpshots right now see you again" -ForegroundColor Blue

        }

    }

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should be working.

Perhaps there are no snapshots on the selected VMs?


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

attached is the supporting screenshot.

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No real idea why that would happen.

Can you also check the contents of $listofvms, before doing the Get-VM?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Come to think of it, this might also be a console output thing, due to the previous Select.

Can you try like this?

$snappartial = Get-VM $listofvms | Get-Snapshot

$snappartial | Out-Default


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

that worked using out-default .can yu tell me what it is doing??

also all the variabled defined within functions will not be accesible once it is run.is that correct??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Due to previous Select-Object cmdlet, the PS output engine is expecting similar output.

Just displaying the content of a variable on the console, doesn't correspond with what the PS output engine is expecting.

By using the Out-Default you are in fact resetting the PS output engine.

Yes, variables defined inside a function fall under the function scope.

Outside the function these variables are not known.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks Luc .

Reply
0 Kudos