如何在云服务器中挂载ISO、复制文件至虚拟机并执行脚本?
Hey there, I totally get the frustration of migrating PowerCLI scripts from on-prem vSphere to VMware Cloud—those familiar guest-focused cmdlets going missing is a real pain. The good news is you don't have to dive straight into raw API calls; Get-CIView is your bridge here, letting you tap into VMware's underlying API through the PowerCLI framework. Let's break down how to replicate each missing operation:
1. 挂载ISO文件(替代Get-CDDrive/Set-CDDrive)
You can use Get-CIView to access the VM's underlying API object, then modify its CD/DVD drive configuration directly. Here's a working example:
# 连接到VMware Cloud服务器 Connect-CIServer -Server <Cloud_Server_FQDN> -Credential $yourCloudCred # 获取目标VM的CIView对象 $targetVM = Get-CIVM -Name "YourVMName" | Get-CIView # 定位VM上的CD/DVD驱动器设备 $cdDrive = $targetVM.Config.Hardware.Device | Where-Object { $_.DeviceInfo.Label -match "CD/DVD" } # 构建重新配置VM的参数 $configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $cdSpec = New-Object VMware.Vim.VirtualCdromSpec $cdSpec.StartConnected = $true $cdSpec.Connected = $true $cdSpec.FileName = "[YourCloudDatastore] ISO_Folder/your-install.iso" # 替换为你的ISO路径 $cdSpec.Operation = "edit" $cdSpec.Device = $cdDrive $configSpec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec $configSpec.DeviceChange[0].Device = $cdSpec $configSpec.DeviceChange[0].Operation = "edit" # 执行ISO挂载操作 $targetVM.ReconfigVM_Task($configSpec)
2. 复制文件到Guest虚拟机(替代Copy-VMGuestFile)
VMware Cloud supports the Guest Operations API, which you can access via Get-CIView to transfer files. Just make sure VMware Tools is running on the guest and your account has GuestOperations.Modify permissions:
# 创建Guest身份验证对象 $guestAuth = New-Object VMware.Vim.NamePasswordAuthentication $guestAuth.Username = "GuestVM_AdminUser" $guestAuth.Password = "GuestVM_Password" # 定义文件传输参数 $fileTransferSpec = New-Object VMware.Vim.GuestFileManagerFileTransferSpec $fileTransferSpec.SourceFilePath = "C:\Local_Workstation\your-archive.zip" # 本地文件路径 $fileTransferSpec.DestinationFilePath = "C:\Guest_VM\Temp\your-archive.zip" # Guest内目标路径 $fileTransferSpec.Overwrite = $true # 获取GuestFileManager服务并执行文件复制 $guestFileManager = Get-CIServiceInstance | Get-CIView -Id GuestFileManager $guestFileManager.CopyFileToGuest($targetVM.MoRef, $guestAuth, $fileTransferSpec)
3. 在Guest中执行脚本(替代Invoke-VMScript)
同样利用Guest Operations API to run scripts inside the guest. This works for both Windows (PowerShell) and Linux (bash) VMs:
# 定义脚本执行参数(Windows示例) $scriptSpec = New-Object VMware.Vim.GuestProgramSpec $scriptSpec.ProgramPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" $scriptSpec.Arguments = "-ExecutionPolicy Bypass -File C:\Guest_VM\Temp\UnpackAndExecute.ps1" # 获取GuestProcessManager服务并启动脚本 $guestProcessManager = Get-CIServiceInstance | Get-CIView -Id GuestProcessManager $processId = $guestProcessManager.StartProgramInGuest($targetVM.MoRef, $guestAuth, $scriptSpec) # 可选:等待脚本执行完成并检查结果 do { Start-Sleep -Seconds 5 $processStatus = $guestProcessManager.ListProcessesInGuest($targetVM.MoRef, $guestAuth, @($processId)) } while ($processStatus[0].EndTime -eq $null) Write-Host "脚本执行退出码: $($processStatus[0].ExitCode)"
关键注意事项
- VMware Tools: Ensure VMware Tools is installed and running on the guest VM—this is required for all guest operations.
- Permissions: Your Cloud account needs permissions like
GuestOperations.Modify,GuestOperations.Execute, andVirtualMachine.Config.CDto perform these actions. - Linux Adjustments: For Linux guests, update the
ProgramPathto/bin/bashand adjust file paths/arguments to match the Linux environment.
内容的提问来源于stack exchange,提问作者Rob Davis




