PowerCLI – 查詢/設定/重新啟動VMware Host NTP服務

參考資料
Setting ESXi DNS and NTP using PowerCLI

ESX(i) NTP Server settings: PowerCLI

$esxHosts = get-VMHost

foreach ($esx in $esxHosts) {
   Write-Host "Configuring NTP Client Policy on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false

   Write-Host "Restarting NTP Client on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false

}

 

$vmhosts = Get-VMHost
$details = @()
foreach ($vmhost in $vmhosts) {
    $detail = "" | select Name,NTPServer
    $detail.Name = $vmhost.Name
    $detail.NTPServer = Get-VMHostNtpServer $vmhost
    $details += $detail
}

$details

 

bash script – 透過SNMP檢查VMWare Host NTP同步狀態

#!/bin/bash

for i in 18 19 21 22 23 24 25 26 27 28 29 30 31 32
do
  echo -n "  UCS:`printf %03d $i` "
  snmpwalk -c public -v 2c -Ov 172.24.2.$i hrSystemDate.0
done

for j in 2 3 4 5 6 7 8 18 19 20 21 22 23 24 34 35 36 37 38 39 40 50 51 52 53
do
  echo -n "Blade:`printf %03d $j` "
  snmpwalk -c public -v 2c -Ov 172.24.18.$j hrSystemDate.0
done

for k in {9..15} {25..31} {41..48} 54 {55..56}
do
  echo -n "Blade:`printf %03d $k` "
  snmpwalk -c public -v 2c -Ov 172.24.18.$k hrSystemDate.0
done

 

csv轉powercli語法大量rename VM

ref:
https://communities.vmware.com/message/2252750

csv example

Old_Name001,VM Description,New_Name001
Old_Name002,VM Description,New_Name002
Old_Name003,VM Description,New_Name003
Old_Name004,VM Description,New_Name004

 

 

<?php

$file1 = file('example.csv');
#print_r($file1);

foreach($file1 as $line){
  $tmp = preg_split("/,/", $line);
  #print_r($tmp);
  $old_name = trim($tmp[0]);
  $new_name = trim($tmp[2]);

  echo "#rename VM $old_name -> $new_name\n";
  echo "\$VM = Get-VM -Name '$old_name'\n";
  echo "Set-VM -VM \$VM -Name '$new_name' -Confirm:\$false\r\n"; #配合Windows txt格式換行符號需調整為\r\n

}//foreach

?>

 

Output

#rename VM Old_Name001 -> New_Name001
$VM = Get-VM -Name 'Old_Name001'
Set-VM -VM $VM -Name 'New_Name001' -Confirm:$false
#rename VM Old_Name002 -> New_Name002
$VM = Get-VM -Name 'Old_Name002'
Set-VM -VM $VM -Name 'New_Name002' -Confirm:$false
#rename VM Old_Name003 -> New_Name003
$VM = Get-VM -Name 'Old_Name003'
Set-VM -VM $VM -Name 'New_Name003' -Confirm:$false
#rename VM Old_Name004 -> New_Name004
$VM = Get-VM -Name 'Old_Name004'
Set-VM -VM $VM -Name 'New_Name004' -Confirm:$false

 

使用Powercli千萬要注意系統proxy設定

參考來源:
http://www.yellow-bricks.com/2011/02/24/connection-problems-with-powercli-due-to-proxy-settings/

 

問題:

硬體Firewall/switch ACL/Windows firewall相關設定都查過

telnet vcenter-ip 443也有通

但使用powercli就是怎樣也連不上

後來google才發現是powercli那台設備有設定proxy,而proxy設備因為firewall policy沒開連不到vCenter

 

解決方式:

關閉proxy或是設定vcenter IP不使用proxy

讓VMware ESXi 5.0 正確辨識SSD及啟用host cache

參考資料:

http://www.yellow-bricks.com/2011/08/18/swap-to-host-cache-aka-swap-to-ssd/

http://www.virtuallyghetto.com/2011/07/how-to-trick-esxi-5-in-seeing-ssd.html

http://www.informationweek.com/the-vmware-write-cache-challenge-solved/240147594

 

環境:

Dell R610 300G sas x4 (RAID5), 200G sata ssd x 2 (RAID1)

裝了2顆SSD但是在vCenter上無法正常辨識為SSD,因此無法啟用host cache,必須利用下面的選項將SSD datastore標記為SSD

 

指令備忘

esxcli storage nmp satp rule add --satp VMW_SATP_LOCAL --device naa.6782bcb01ef0cf001a00e4a3722e6b00 --option=enable_ssd

esxcli storage nmp satp rule list | grep enable_ssd

esxcli storage core claiming reclaim -d naa.6782bcb01ef0cf001a00e4a3722e6b00

esxcli storage core device list --device=naa.6782bcb01ef0cf001a00e4a3722e6b00

vim-cmd hostsvc/storage/refresh

 

 

todo:

host cache是write or read cache?(待確認)

Swapping to Host Cache看起來是VM swap file的write cache

VMware ESXi 5.0 Enable SNMP

install Vsphere CLI first

 

#check setting
vicfg-snmp.pl --server 172.30.65.240 --username root --password xxxxxxxx --show

#set snmp community
vicfg-snmp.pl --server 172.30.65.240 --username root --password xxxxxxxx --communities public

#enable snmp service
vicfg-snmp.pl --server 172.30.65.240 --username root --password xxxxxxxx --enable

 

使用powercli批次更換VM datastore

參考資料: http://winblog.ch/2012/03/19/finding-and-updating-virtual-machines-with-outdated-tools-on-vsphere-using-powershell/

Connect-VIServer -Server 10.10.10.200 -User admin -Password 'testpass'
$VMs=get-vm
foreach ($vm in $VMs)
{
    $VMHost_Name = $vm.vmhost.name
    if ($VMHost_Name -eq "10.10.10.11")
    {
        Write-Host "Change datastore of" $VM.Name
        Move-VM -vm $VM.Name -datastore local_240
    }
}

 

使用powercli批次更新VM tools

今天把兩台ESXi 4.1升級到5.0,發現數十台VM的vm tool都過期需要更新,從vcenter一台一台點選更新實在不太有效率,所以就要靠powercli的幫忙,把更新的動作script化

google找到的參考資料(http://winblog.ch/2012/03/19/finding-and-updating-virtual-machines-with-outdated-tools-on-vsphere-using-powershell/),除了有我要的powercli現成範例之外也說明了如何使用powercli的一些概念,對於初學powercli的人很有幫助

Connect-VIServer your-vi-server
$VMs = Get-VM
foreach ($vm in $VMs)
{
	$ToolsStatus = $vm.ExtensionData.Guest.ToolsStatus
	if ($ToolsStatus -eq "toolsOld")
	{
		Write-Host "Updating the tools of" $VM.Name
		$vm | Update-Tools -NoReboot -RunAsync 
	}
}

注意事項:

line1需要輸入vCenter帳號密碼

line6有稍做微調僅更新有安裝vm tools且過期的VM