linux 脚本 计算机,用于在 Linux 桌面计算机上执行操作的示例 PowerCLI 脚本

.DESCRIPTION

The Tool supports:

1. Power off VMs

2. Power on VMs

3. Shutdown VMs

4. Restart VMs

5. Restart VM guest

6. Delete VMs from Disk

.NOTES

#>

#--------------------- Functions --------------------

function IsVMExists ($VMExists)

{

Write-Host "Checking if the VM $VMExists Exists"

[bool]$Exists = $false

#Get all VMS and check if the VMs is already present in VC

$listvm = Get-vm

foreach ($lvm in $listvm)

{

if($VMExists -eq $lvm.Name )

{

$Exists = $true

Write-Host "$VMExists is Exist"

}

}

return $Exists

}

function Delete_VM($VMToDelete)

{

Write-Host "Deleting VM $VMToDelete"

Get-VM $VMToDelete | where { $_.PowerState –eq "PoweredOn" } | Stop-VM –confirm:$false

Get-VM $VMToDelete | Remove-VM –DeleteFromDisk –confirm:$false

}

#------------------ Handle input ---------------------

"-----------------------------------------------------"

$vcAddress = Read-Host 'Your vCenter address'

$vcAdmin = Read-Host 'Your vCenter admin user name'

$vcPassword = Read-Host 'Your vCenter admin user password' -AsSecureString

$vcPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vcPassword))

"-----------------------------------------------------"

#$csvFile = Read-Host 'Csv File '

$action = Read-Host 'Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM'

$sleepTime = Read-Host 'Wait time (seconds) between each VM'

"-----------------------------------------------------"

switch ($action)

{

1

{

"Your selection is 1). Power On"

}

2

{

"Your selection is 2). Power Off"

}

3

{

"Your selection is 3) Shutdown"

}

4

{

"Your selection is 4). Restart VM"

}

5

{

"Your selection is 5). Restart VM Guest"

}

6

{

"Your selection is 6). Delete VM"

}

default

{

"Invalid selection for action: $action"

exit

}

}

$csvFile = '.\CloneVMs.csv'

#check if file exists

if (!(Test-Path $csvFile))

{

write-host -ForeGroundColor Red "CSV File not found"

exit

}

"-----------------------------------------------------"

#--------------------- Main --------------------------

#Read input CSV file

Disconnect-VIServer $vcAddress -Confirm:$false

#Connect-VIServer $vcAddress -ErrorAction Stop -user $vcAdmin -password $vcPassword

Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword

$csvData = Import-CSV $csvFile

foreach ($line in $csvData)

{

$VMName = $line.VMName

switch ($action)

{

1

{

Get-VM $VMName | Start-VM -Confirm:$false

}

2

{

Get-VM $VMName | Stop-VM -Confirm:$false

}

3

{

Get-VM $VMName | Shutdown-VMGuest -Confirm:$false

}

4

{

Get-VM $VMName | Restart-VM -Confirm:$false

}

5

{

Get-VM $VMName | Restart-VMGuest -Confirm:$false

}

6

{

if (IsVMExists ($VMName))

{

Delete_VM ($VMName)

}

}

default{}

}

Start-Sleep -s $sleepTime

}

Disconnect-VIServer $vcAddress -Confirm:$false

exit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值