PowerShell/PowerCli 从 Out-GridView 传递正确的参数以删除快照
PowerShell/PowerCli to pass Correct parameter from Out-GridView for Snapshot removal
我是PowerShell/PowerCLI新手,为了工作正在学习。我创建了一个简单的几行代码来列出所有快照并使用 out-gridview 在另一个弹出窗口 window 中列出它,选择一个或多个快照并单击 "Ok" 将让我删除所选快照,它没有问题。
#List snapshot
$snapshot = Get-VM | Get-Snapshot | Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple
#remove selected VM's snapshot
$snapshot | foreach { Remove-Snapshot $_ }
因为默认 Get-Snapshot
仅显示快照名称、描述和 PowerState,我添加了一个计数器、VM 名称、快照大小(以 GB 为单位)和快照的年龄。
$counter = 1
#List snapshot with more information
$snapshot = Get-VM | Get-Snapshot |
Select-Object @{Name="ID";Expression={$global:counter;$global:counter++}},
VM, Name, @{Name="SizeGB";Expression={[math]::Round($_.sizeGB,2)}},
@{Name="Days";Expression={(New-TimeSpan -End (Get-Date) -Start $_.Created).Days}} |
Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple
#remove selected VM's snapshot
$snapshot | foreach { Remove-Snapshot $_ }
现在 $_
似乎 return Remove-Snapshot
的错误参数并出现错误:
Remove-Snapshot : Cannot bind parameter 'Snapshot'. Cannot convert the
"@{ID=4; VM=Win7-Golden; Name=SS20170227 v3.1; SizeGB=0.00; Days=64}"
value of type "Selected.VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl"
to type "VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot".
At D:\powershell\snapshot_with_index.ps1:10 char:39
+ $snapshot | foreach { Remove-Snapshot $_ }
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Snapshot], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveSnapshot
如果有人可以指导我如何解决此错误,传递正确的参数,我将不胜感激。
Select-Object
将对象类型更改为 PSCustomObject
,但 Remove-Snapshot
似乎需要一个快照对象。
我无权访问 vSphere 系统,但是如果你想允许通过带有 "enhanced information" 的 gridview 进行过滤,你可能需要先将快照存储在一个数组中,然后获取选定的快照从该数组中删除。
类似这样的方法可能有效:
$snapshots = @(Get-VM | Get-Snapshot)
0..($snapshots.Count - 1) | ForEach-Object {
$i = $_
$snapshots[$i] | Select-Object @{n='ID';e={$i}}, ...
} | Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple | ForEach-Object {
Remove-Snapshot $snapshots[$_.ID]
}
我是PowerShell/PowerCLI新手,为了工作正在学习。我创建了一个简单的几行代码来列出所有快照并使用 out-gridview 在另一个弹出窗口 window 中列出它,选择一个或多个快照并单击 "Ok" 将让我删除所选快照,它没有问题。
#List snapshot
$snapshot = Get-VM | Get-Snapshot | Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple
#remove selected VM's snapshot
$snapshot | foreach { Remove-Snapshot $_ }
因为默认 Get-Snapshot
仅显示快照名称、描述和 PowerState,我添加了一个计数器、VM 名称、快照大小(以 GB 为单位)和快照的年龄。
$counter = 1
#List snapshot with more information
$snapshot = Get-VM | Get-Snapshot |
Select-Object @{Name="ID";Expression={$global:counter;$global:counter++}},
VM, Name, @{Name="SizeGB";Expression={[math]::Round($_.sizeGB,2)}},
@{Name="Days";Expression={(New-TimeSpan -End (Get-Date) -Start $_.Created).Days}} |
Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple
#remove selected VM's snapshot
$snapshot | foreach { Remove-Snapshot $_ }
现在 $_
似乎 return Remove-Snapshot
的错误参数并出现错误:
Remove-Snapshot : Cannot bind parameter 'Snapshot'. Cannot convert the "@{ID=4; VM=Win7-Golden; Name=SS20170227 v3.1; SizeGB=0.00; Days=64}" value of type "Selected.VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl" to type "VMware.VimAutomation.ViCore.Types.V1.VM.Snapshot". At D:\powershell\snapshot_with_index.ps1:10 char:39 + $snapshot | foreach { Remove-Snapshot $_ } + ~~ + CategoryInfo : InvalidArgument: (:) [Remove-Snapshot], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveSnapshot
如果有人可以指导我如何解决此错误,传递正确的参数,我将不胜感激。
Select-Object
将对象类型更改为 PSCustomObject
,但 Remove-Snapshot
似乎需要一个快照对象。
我无权访问 vSphere 系统,但是如果你想允许通过带有 "enhanced information" 的 gridview 进行过滤,你可能需要先将快照存储在一个数组中,然后获取选定的快照从该数组中删除。
类似这样的方法可能有效:
$snapshots = @(Get-VM | Get-Snapshot)
0..($snapshots.Count - 1) | ForEach-Object {
$i = $_
$snapshots[$i] | Select-Object @{n='ID';e={$i}}, ...
} | Out-GridView -Title "Snapshot Viewer" -OutputMode Multiple | ForEach-Object {
Remove-Snapshot $snapshots[$_.ID]
}