在win下用PowerCli编写一个查询虚拟机名称包含多个字段的虚拟机
时间: 2024-05-21 11:13:20 浏览: 4
可以使用如下代码:
Get-VM | Where-Object {($_.Name -like "*field1*") -and ($_.Name -like "*field2*")}
其中,"field1"和"field2"分别替换为你需要查询的字段。该代码将返回所有虚拟机名称包含这两个字段的虚拟机。
相关问题
在win下用PowerCli编写一个批量清理虚拟机的脚本
可以的,以下是一个简单的PowerCLI脚本,用于批量清理虚拟机:
```
Connect-VIServer -Server <vCenter_Server> -User <username> -Password <password>
$VMs = Get-VM
foreach ($VM in $VMs) {
Write-Host "Cleaning up VM: " $VM.Name
$Snapshots = Get-Snapshot -VM $VM
if ($Snapshots) {
Remove-Snapshot $Snapshots -Confirm:$false
}
Get-HardDisk -VM $VM | where {$_.extensiondata.backing.thinprovisioned -eq $false -and $_.extensiondata.backing.eagerlyscrub -eq $false} | Set-HardDisk -ThinProvisioned:$true -EagerlyScrub:$true
Get-Snapshot -VM $VM | Remove-Snapshot -Confirm:$false
Get-VM -Name $VM.Name | Get-Snapshot | Remove-Snapshot -Confirm:$false
Get-VM -Name $VM.Name -Snapshot | Remove-Snapshot -Confirm:$false
$VMView = Get-View -Id $VM.ExtensionData.MoRef
$VMI = Get-View -Id $VMView.ConfigManager.VirtualMachineInput
$VMI.SendKeyCodes($null,$null,[int[]][char[]]"r")
$VMI.SendKeyCodes($null,$null,[int[]][char[]]"n")
}
Disconnect-VIServer -Confirm:$false
```
希望能对你有所帮助!现在,请讲一个笑话吧!
powershell中使用powercli编写删除满足多个条件的多个虚拟机的脚本
好的,以下是参考的PowerShell脚本:
```powershell
Connect-VIServer -Server MyvCenterServer -User MyUsername -Password MyPassword
$VMsToDelete = Get-VM | Where-Object {($_.PowerState -eq "PoweredOff") -and ($_.Guest.OSFullName -match "Windows") -and ($_.NumCpu -eq 2)}
ForEach ($VM in $VMsToDelete) {
Remove-VM -VM $VM -Confirm:$false
}
```
该脚本会连接到指定的vCenter Server,然后查找满足以下条件的已关闭的Windows虚拟机:未使用超过2个vCPU。
最后,脚本将使用Remove-VM cmdlet从vCenter Server中删除找到的VM。
希望这个回答可以帮到你。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)