Hi Luc,
good morning,
could you review following code i am trying to combine both control (getting snapshot) and control(deleting snapshot) in same function .
do you recommend such practice and if not what could be the issues?
also for some reasons $snap1 is not being displayed on console that is very weired.
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=get-vm -Location $cluster
$snap1=$vms|Get-Snapshot|select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}
$snap=$vms|get-snapshot
$snap1
if($snap -ne $null)
{
$res=read-host "do yu want to delete snapshot y/n"
if($res -eq 'y')
{
write-host "deleting snapshot"
$snap|remove-snapshot -WhatIf
}
}
}
}
If you want to collect all snapshots and then delete them in 1 go, you would need to do something like this
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$snap = @()
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=Get-VM -Location $cluster
$snap += $vms | 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 snapshot y/n"
if($res -eq 'y')
{
Write-Host "deleting snapshot"
$snap | Remove-Snapshot -WhatIf
}
}
}
Alan did a post on a snapshot reporting script some time ago, see SNAPREMINDER
It uses one of my snippets, based on the Events, to find out who created a snapshot.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would not ask the question inside the function, but rather ask before calling the function, and then pass the answer as a switch to the function.
Would that work for you?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have been trying to convert all mximum possible vsphere realted operations into following format .
1:search-function
2:call function
3:provide inputs either through parameters declared inside param block or through read-host .
so that those who dontknow powercli or powershell can work conveniently.
is there any way following code works .i.e print both snpshots through $snap1 and ask for deletion of snpshot through $snap.??
You could do the following, no need to collect the snapshots twice.
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=Get-VM -Location $cluster
$snap = $vms | 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 snapshot y/n"
if($res -eq 'y')
{
Write-Host "deleting snapshot"
$snap | Remove-Snapshot -WhatIf
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
for some reasons it is not working s desired .still not giving option to delte snapshot .howver below is working
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=Get-VM -Location $cluster
$snap = $vms | Get-Snapshot
$snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}
}
$res=read-host "do yu need removal of snapshot"
if($res -eq 'y')
{
write-host "deleting snapshot"
$snap
}
else
{
write-host "check with application owners"
}
}
I'm not sure why that would be better than the script I provided.
My version just doesn't ask the question when there are no snapshots on the VM.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
well what is observed in your version .
1:if condition is within foreach loop so every time loop runs it will get vms of specific cluster and ask for deletion of snapshots if any.
but what i have been trying to achieve is to first collect snapshots of all cutsers and then delete in one question. however i am checking again ....
also if you could tell me if there is any property that can display snapshot "created by ".
If you want to collect all snapshots and then delete them in 1 go, you would need to do something like this
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$snap = @()
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=Get-VM -Location $cluster
$snap += $vms | 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 snapshot y/n"
if($res -eq 'y')
{
Write-Host "deleting snapshot"
$snap | Remove-Snapshot -WhatIf
}
}
}
Alan did a post on a snapshot reporting script some time ago, see SNAPREMINDER
It uses one of my snippets, based on the Events, to find out who created a snapshot.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks Luc it worked .
iam checking snpreminder.
however i have one basic question if you could check orange code.
it is also giving the same result if we don't store in separate array as you suggested (which is marked correct).
is this correct ?
function get-snapshot_deletesnpshot_utilities
{
[cmdletbinding()]
param (
[parameter(mandatory = $true,
valuefrompipeline = $true,
valuefrompipelinebypropertyname = $true)]
[string]$filepath
)
$csv=Import-Csv "$filepath\clusters.csv"
foreach($line in $csv)
{
$cluster=Get-Cluster $line.cluster
$vms=Get-VM -Location $cluster
$snap = $vms | Get-Snapshot
$snap | Select @{N='snapshot';E={$_.name}},@{N='vmname';E={$_.vm}},@{N='datecreated';E={$_.created}}
}
$res=read-host "do yu need removal of snapshot"
if($res -eq 'y')
{
write-host "deleting snapshot"
$snap
}
else
{
write-host "check with application owners"
}
}
Since you are populating the $snap variable inside the ForEach loop, the variable will only contains the snapshost om that specific cluster.
If your testing with 1 cluster, that will not be a problem.
But if you have more than 1 cluster in the CSV file, only the snapshots of the last cluster in the CSV will be removed.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i was thinking in that direction but for some reasons it took snapshots of all clusters .not sure how it happened .but it should be done the way yu suggested .