您可以自訂和使用下列範例指令碼,將組態檔 config 和 viewagent-custom.conf 上傳到多個 Linux 虛擬機器 (VM)。此指令碼使用 SSH 在 Linux 虛擬機器上執行命令。
若要複製和貼上不含分頁符號的指令碼內容,請使用本主題的 HTML 版本,您可以從 Horizon7 文件頁面取得,網址是:https://www.vmware.com/support/pubs/view_pubs.html。
指令碼輸入
此指令碼會讀取一個輸入檔,在用於部署 Linux 桌面平台的範例 PowerCLI 指令碼輸入檔 中有加以描述。此指令碼也會互動地要求下列資訊:
- vCenter Server 的 IP 位址
- vCenter Server 的管理員登入名稱
- vCenter Server 的管理員密碼
- Linux 虛擬機器的使用者登入名稱
- Linux 虛擬機器的使用者密碼
指令碼內容
<# Upload the configuration files config and viewagent-custom.conf to Linux VMs using SSH #> #------------------------- Functions ------------------------- function GetInput { Param($prompt, $IsPassword = $false) $prompt = $prompt + ": " Write-Host $prompt -NoNewLine [Console]::ForegroundColor = "Blue" if ($IsPassword) { $input = Read-Host -AsSecureString $input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input)) } else { $input = Read-Host } [Console]::ResetColor() return $input } function Check_SSH_Client { Param($IsPlink, $IsPSCP) if ($IsPlink) { if (Test-Path ".\plink.exe") { write-host -ForeGroundColor Yellow 'SSH client "plink.exe" found' } else { write-host -ForeGroundColor Red 'SSH client "plink.exe" not found, please download from its official web site' exit } } if ($IsPSCP) { if (Test-Path ".\pscp.exe") { write-host -ForeGroundColor Yellow 'SSH client "pscp.exe" found' } else { write-host -ForeGroundColor Red 'SSH client "pscp.exe" not found, please download from its official web site' exit } } } function RunCmdViaSSH { Param($VM_Name, $User, $Password, $Cmd, $returnOutput = $false) $VM= Get-VM $VM_Name $IP = $VM.guest.IPAddress[0] write-host "Run cmd on $VM_Name ($IP)" if($returnOutput) { $command = "echo yes | .\plink.exe -ssh -l $user -pw $password $IP " + '"' + $cmd +'"' $output = Invoke-Expression $command return $output } else { echo yes | .\plink.exe -ssh -l $user -pw $password $IP "$cmd" } } function UploadFileViaSSH { Param($VM_Name, $User, $Password, $LocalPath, $DestPath) $VM= Get-VM $VM_Name $IP = $VM.guest.IPAddress[0] $command = "echo yes | .\pscp.exe -l $User -pw $Password $LocalPath $IP" + ":" + "$DestPath" write-host "Upload file: $command" Invoke-Expression $command } #------------------------- Handle Input ------------------------- "-----------------------------------------------------" Check_SSH_Client -IsPlink $true -IsPSCP $true "-----------------------------------------------------" write-host -ForeGroundColor Blue 'Please ensure your config file and viewagent-custom.conf file are in current working directory' $vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false $vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false $vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true "-----------------------------------------------------" $guestUser = GetInput -prompt 'Your VM guest OS user name' -IsPassword $false $guestPassword = GetInput -prompt 'Your VM guest OS user password' -IsPassword $true "-----------------------------------------------------" $csvFile = '.\CloneVMs.csv' $setConfig = $false $setCustomConf = $false $config_File = "config" $customConf_File = "viewagent-custom.conf" #check if config file exists if(Test-Path $config_File) { $setConfig = $true write-host -ForeGroundColor Yellow '"config" file found' } else { write-host -ForeGroundColor Yellow '"config" file not found, skip it' } if(Test-Path $customConf_File) { $setCustomConf = $true write-host -ForeGroundColor Yellow '"viewagent-custom.conf" file found' } else { write-host -ForeGroundColor Yellow '"viewagent-custom.conf" file not found, skip it' } if (($setConfig -eq $false)-AND ($setCustomConf -eq $false)) { write-host -ForeGroundColor Red 'Both file not found, exit' exit } #Connect to vCenter $VC_Conn_State = Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword if([string]::IsNullOrEmpty($VC_Conn_State)) { Write-Host 'Exit since failed to login vCenter' exit } else { Write-Host 'vCenter is connected' } #Read input CSV file $csvData = Import-CSV $csvFile $destFolder = "/home/$guestUser/" #Handle VMs one by one foreach ($line in $csvData) { "`n-----------------------------------------------------" $VMName = $line.VMName write-host -ForeGroundColor Yellow "VM: $VMName`n" #Try to delete the configuration file from home folder on destination VM $cmd = "rm -rf config viewagent-custom.conf" Write-Host "Run cmd '$cmd' in VM '$VMName' with user '$guestUser'" RunCmdViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -Cmd $cmd if ($setConfig) { Write-Host "Upload File '$config_File' to '$destFolder' of VM '$VMName' with user '$guestUser'" UploadFileViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -LocalPath $config_File -DestPath $destFolder $cmd = "sudo mv ./$config_File /etc/vmware/"; Write-Host "Move configuraton file: $cmd" RunCmdViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -Cmd $cmd } if ($setCustomConf) { Write-Host "Upload File '$customConf_File' to '$destFolder' of VM '$VMName' with user '$guestUser'" UploadFileViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -LocalPath $customConf_File -DestPath $destFolder $cmd = "sudo mv ./$customConf_File /etc/vmware/"; Write-Host "Move configuraton file: $cmd" RunCmdViaSSH -VM_Name $VMName -User $guestUser -Password $guestPassword -Cmd $cmd } } Disconnect-VIServer $vcAddress -Confirm:$false exit
指令碼執行
下列訊息是來自指令碼的執行:
PowerCLI C:\scripts> .\UpdateOptionFile.ps1 -------------------------------------------------- Please ensure your config file and view-agent.conf file are in current working directory. Your vCenter address: 10.117.44.17 Your vCenter admin user name: administrator Your vCenter admin user password: ******* -------------------------------------------------- Your VM guest OS user name: ViewUser Your VM guest OS user password: *******