Skip to content

Commit cbaa222

Browse files
upload GetVMGuestToolsStatusAndUpgrade.ps1
0 parents  commit cbaa222

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Write-Host "環境變數配置中`n"
2+
[string]$currentPath = Get-Location
3+
$wsh = New-Object -ComObject WScript.Shell
4+
# 檔案選擇視窗函數
5+
Function Get-FileName($initialDirectory) {
6+
# 使用 .NET 框架建立視窗
7+
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
8+
$OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
9+
# 設定初始目錄
10+
$OpenFileDialog.initialDirectory = $initialDirectory
11+
# 設定篩選器為 CSV 檔、所有檔案
12+
$OpenFileDialog.filter = "CSV Files(*.csv)|*.csv|All Files|*.*"
13+
# 顯示視窗
14+
$OpenFileDialog.ShowDialog() | Out-Null
15+
# 傳回選擇的檔案名稱
16+
$OpenFileDialog.filename
17+
}
18+
19+
Write-Host "設定PowerCLI忽略SSL證書檢查`n"
20+
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
21+
Write-Host "`n"
22+
Write-Host "`n"
23+
$vCenter = Read-Host "請輸入vCenter IP"
24+
Write-Host "`n"
25+
Write-Host "注意!! 請等待視窗彈出後再輸入帳號密碼`n(帳號格式範例 administrator@vsphere.local)。`n連線 vCenter IP為" $vCenter
26+
$conn = Connect-VIServer -server $vCenter
27+
Write-Host "`n"
28+
if ($conn.IsConnected -ne "True") {
29+
Write-Host "連線失敗請重新執行`n"
30+
return
31+
}
32+
33+
Write-Host "以下為所有 ESXi 主機名稱"
34+
ForEach ($VMHost in Get-VMHost | select name) {
35+
Write-Host $VMHost.Name
36+
}
37+
Write-Host "`n"
38+
39+
$VMHostName = Read-Host "請輸入 ESXi 主機名稱"
40+
Write-Host "`n"
41+
42+
Write-Host "正在取得VMware Tools 狀態"
43+
$TotalVMs = (Get-VMHost -name $VMHostName | Get-VM).Count
44+
$CurrentVMs = (Get-VMHost -name $VMHostName | Get-VM | % { get-view $_.id } | Where-Object { $_.Guest.ToolsVersionStatus -like "guestToolsCurrent" } | select name).Count
45+
$NeedUpgradeVMs = (Get-VMHost -name $VMHostName | Get-VM | % { get-view $_.id } | Where-Object { $_.Guest.ToolsVersionStatus -like "guestToolsNeedUpgrade" } | select name).Count
46+
$NotInstalledVMs = (Get-VMHost -name $VMHostName | Get-VM | % { get-view $_.id } | Where-Object { $_.Guest.ToolsVersionStatus -like "guestToolsNotInstalled" } | select name).Count
47+
48+
$VMsToolsStatus = New-Object PSObject -Property ([ordered]@{
49+
"VMware Tools is installed, and the version is current. (已安裝最新版總數)" = $CurrentVMs
50+
"VMware Tools is installed, but the version is not current. (需升級總數)" = $NeedUpgradeVMs
51+
"VMware Tools has never been installed. (尚未安裝VMware Tools)" = $NotInstalledVMs
52+
"Total VMs (VM 總數量)" = $TotalVMs
53+
})
54+
55+
$VMsToolsStatus
56+
57+
Write-Host "即將跳出視窗請選擇保存路徑`n"
58+
$csvPath = Get-FileName $currentPath
59+
60+
if ($csvPath -ne "") {
61+
Write-Host "輸出為CSV到" $csvPath "`n"
62+
Get-VMHost -name $VMHostName | Get-VM | % { get-view $_.id } | select name, @{Name = "ToolsVersion"; Expression = { $_.config.tools.toolsversion } }, @{ Name = "ToolStatus"; Expression = { $_.Guest.ToolsVersionStatus } } | Sort-Object ToolStatus | Export-Csv -Path $csvPath -NoTypeInformation
63+
}
64+
else {
65+
Write-Host "路徑錯誤! 不保存CSV"
66+
}
67+
68+
$OutofDateVMs = Get-VMHost -name $VMHostName | Get-VM | % { get-view $_.id } | Where-Object { $_.Guest.ToolsVersionStatus -like "guestToolsNeedUpgrade" } | select name
69+
70+
if ($NeedUpgradeVMs -eq 0) {
71+
Write-Host "沒有 VM 需要升級 Vmware Guest Tools"
72+
}
73+
else {
74+
Write-Host "以下為需要更新 Vmware Guest Tools 的 VM`n"
75+
Write-Host "VM Name"
76+
ForEach ($VMs in $OutOfDateVMs) {
77+
Write-Host $VMs.Name
78+
}
79+
80+
$wsh = New-Object -ComObject WScript.Shell
81+
$answer = $wsh.Popup("開始逐一升級VM上的Vmware Guest Tools", 300, "是否升級Vmware Guest Tools", 4 + 32)
82+
if ($answer -eq 7) {
83+
Write-Host "`n您選擇不升級 Vmware Guest Tools。"
84+
}
85+
if ($answer -eq 6) {
86+
Write-Host "`n您選擇升級 Vmware Guest Tools,開始逐一升級。`n"
87+
ForEach ($VMs in $OutOfDateVMs) {
88+
Write-Host "Start upgrading Vmware Guest Tools on VM:" $VMs.Name
89+
Update-Tools $VMs.Name -Verbose -NoReboot
90+
}
91+
}
92+
}
93+
94+
Write-Host "`n中斷vCenter的連線中"
95+
Disconnect-VIServer -Server $vCenter -Confirm:$false
96+
Write-Host "`n執行結束"

0 commit comments

Comments
 (0)