Update VMToolsManagement.psm1

Added advanced function: Get-VMToolsUpgradePolicy
Updated the following advanced functions to add functionatlity:
- Set-VMToolsUpgradePolicy - Supports UpgradeAtPowerCycle and Manual
- Update-VMToolsConfInVM - Supports all log levels now
Misc other minor updates: 
- cmdlets changed to 'advanced function'
- 'vcetner' changed to 'vcenter'
This commit is contained in:
Kyle Ruddy
2018-01-05 16:13:39 -05:00
committed by GitHub
parent 7217cec777
commit 23f215be35

View File

@@ -1,4 +1,4 @@
# Script Module : VMToolsManagement # Script Module : VMToolsManagement
# Version : 1.0 # Version : 1.0
# Copyright © 2017 VMware, Inc. All Rights Reserved. # Copyright © 2017 VMware, Inc. All Rights Reserved.
@@ -37,10 +37,10 @@ New-VIProperty -Name ToolsBuildNumber -Object VirtualMachine -Value {
Function Get-VMToolsInfo { Function Get-VMToolsInfo {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet retrieves the VMTools info of specified virtual machines. This advanced function retrieves the VMTools info of specified virtual machines.
.DESCRIPTION .DESCRIPTION
This cmdlet retrieves the VMTools version and build number info of specified virtual machines. This advanced function retrieves the VMTools version and build number info of specified virtual machines.
.PARAMETER VM .PARAMETER VM
Specifies the virtual machines which you want to get the VMTools info of. Specifies the virtual machines which you want to get the VMTools info of.
@@ -50,7 +50,7 @@ Function Get-VMToolsInfo {
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Get-VMToolsInfo C:\PS> Get-VM -Server $VCServer | Get-VMToolsInfo
Retrieves VMTools info of all virtual machines which run in the $VCServer vCetner Server. Retrieves VMTools info of all virtual machines which run in the $VCServer vCenter Server.
.EXAMPLE .EXAMPLE
C:\PS> Get-VM "*rhel*" | Get-VMToolsInfo C:\PS> Get-VM "*rhel*" | Get-VMToolsInfo
@@ -74,7 +74,7 @@ Function Get-VMToolsInfo {
Retrieves VMTools info of virtual machines which run on the "MyESXiHostName" ESXi host. Retrieves VMTools info of virtual machines which run on the "MyESXiHostName" ESXi host.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
The tools build number is not supported in VMTools before 10.2.0 The tools build number is not supported in VMTools before 10.2.0
.NOTES .NOTES
@@ -100,17 +100,17 @@ Function Get-VMToolsInfo {
) )
Process { Process {
Get-VM $VM | Select Name, @{Name="ToolsVersion"; Expression={$_.Guest.ToolsVersion}}, ToolsBuildNumber Get-VM $VM | Select-Object Name, @{Name="ToolsVersion"; Expression={$_.Guest.ToolsVersion}}, ToolsBuildNumber
} }
} }
Function Get-VMToolsInstallLastError { Function Get-VMToolsInstallLastError {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet retrieves the error code of last VMTools installation. This advanced function retrieves the error code of last VMTools installation.
.DESCRIPTION .DESCRIPTION
This cmdlet retrieves the error code of last VMTools installation on specified virtual machines. This advanced function retrieves the error code of last VMTools installation on specified virtual machines.
.PARAMETER VM .PARAMETER VM
Specifies the virtual machines which you want to get the error code of. Specifies the virtual machines which you want to get the error code of.
@@ -120,7 +120,7 @@ Function Get-VMToolsInstallLastError {
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Get-VMToolsInstallLastError C:\PS> Get-VM -Server $VCServer | Get-VMToolsInstallLastError
Retrieves the last VMTools installation error code of all virtual machines which run in the $VCServer vCetner Server. Retrieves the last VMTools installation error code of all virtual machines which run in the $VCServer vCenter Server.
.EXAMPLE .EXAMPLE
C:\PS> Get-VM "*win*" | Get-VMToolsInstallLastError C:\PS> Get-VM "*win*" | Get-VMToolsInstallLastError
@@ -144,7 +144,7 @@ Function Get-VMToolsInstallLastError {
Retrieves the last VMTools installation error code of virtual machines which run on the "MyESXiHostName" ESXi host. Retrieves the last VMTools installation error code of virtual machines which run on the "MyESXiHostName" ESXi host.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
@@ -171,7 +171,7 @@ Function Get-VMToolsInstallLastError {
Process { Process {
$result = @() $result = @()
foreach ($_ in $VM) { foreach ($_ in $VM) {
$errorCodeInfo = $_.ExtensionData.Config.ExtraConfig.GetEnumerator() | where {$_.Key -eq "guestinfo.toolsInstallErrCode"} $errorCodeInfo = $_.ExtensionData.Config.ExtraConfig.GetEnumerator() | Where-Object {$_.Key -eq "guestinfo.toolsInstallErrCode"}
$info = New-Object PSObject $info = New-Object PSObject
$info | Add-Member -type NoteProperty -name VmName -value $_.Name $info | Add-Member -type NoteProperty -name VmName -value $_.Name
@@ -186,10 +186,10 @@ Function Get-VMToolsInstallLastError {
Function Get-VMToolsGuestInfo { Function Get-VMToolsGuestInfo {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet retrieves the guest info of specified virtual machines. This advanced function retrieves the guest info of specified virtual machines.
.DESCRIPTION .DESCRIPTION
This cmdlet retrieves the guest info such as HostName, IP, ToolsStatus, ToolsVersion, This advanced function retrieves the guest info such as HostName, IP, ToolsStatus, ToolsVersion,
ToolsInstallType and GuestFamily of specified virtual machines. ToolsInstallType and GuestFamily of specified virtual machines.
.PARAMETER VM .PARAMETER VM
@@ -200,7 +200,7 @@ Function Get-VMToolsGuestInfo {
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Get-VMToolsGuestInfo C:\PS> Get-VM -Server $VCServer | Get-VMToolsGuestInfo
Retrieves guest info of all virtual machines which run in the $VCServer vCetner Server. Retrieves guest info of all virtual machines which run in the $VCServer vCenter Server.
.EXAMPLE .EXAMPLE
C:\PS> Get-VM "*win*" | Get-VMToolsGuestInfo C:\PS> Get-VM "*win*" | Get-VMToolsGuestInfo
@@ -236,7 +236,7 @@ Function Get-VMToolsGuestInfo {
Retrieves guest info of virtual machines which run on the "MyESXiHostName" ESXi host. Retrieves guest info of virtual machines which run on the "MyESXiHostName" ESXi host.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
@@ -261,7 +261,7 @@ Function Get-VMToolsGuestInfo {
) )
Process { Process {
Get-VM $VM | Select Name, @{Name="HostName"; Expression={$_.Guest.HostName}}, Get-VM $VM | Select-Object Name, @{Name="HostName"; Expression={$_.Guest.HostName}},
@{Name="IP"; Expression={$_.Guest.ExtensionData.IpAddress}}, @{Name="IP"; Expression={$_.Guest.ExtensionData.IpAddress}},
@{Name="ToolsStatus"; Expression={$_.Guest.ExtensionData.ToolsStatus}}, @{Name="ToolsStatus"; Expression={$_.Guest.ExtensionData.ToolsStatus}},
@{Name="ToolsVersion"; Expression={$_.Guest.ToolsVersion}}, @{Name="ToolsVersion"; Expression={$_.Guest.ToolsVersion}},
@@ -274,10 +274,10 @@ Function Get-VMToolsGuestInfo {
Function Get-VMByToolsInfo { Function Get-VMByToolsInfo {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet retrieves the virtual machines with specified VMTools info. This advanced function retrieves the virtual machines with specified VMTools info.
.DESCRIPTION .DESCRIPTION
This cmdlet retrieves the virtual machines with specified VMTools version, This advanced function retrieves the virtual machines with specified VMTools version,
running status or version status. running status or version status.
.PARAMETER VM .PARAMETER VM
@@ -297,7 +297,7 @@ Function Get-VMByToolsInfo {
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Get-VMByToolsInfo C:\PS> Get-VM -Server $VCServer | Get-VMByToolsInfo
Retrieves the virtual machines with VMTools not running in vCetner Server $VCServer. Retrieves the virtual machines with VMTools not running in vCenter Server $VCServer.
.EXAMPLE .EXAMPLE
C:\PS> Get-VM | Get-VMByToolsInfo -ToolsRunningStatus guestToolsNotRunning C:\PS> Get-VM | Get-VMByToolsInfo -ToolsRunningStatus guestToolsNotRunning
@@ -328,7 +328,7 @@ Function Get-VMByToolsInfo {
Retrieves the virtual machines with VMTools that need to upgrade on the "MyESXiHostName" ESXi host. Retrieves the virtual machines with VMTools that need to upgrade on the "MyESXiHostName" ESXi host.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
@@ -376,65 +376,60 @@ Function Get-VMByToolsInfo {
} }
if ($ToolsVersion) { if ($ToolsVersion) {
$vmList = $vmList | Where {$_.Guest.ToolsVersion -like $ToolsVersion} $vmList = $vmList | Where-Object {$_.Guest.ToolsVersion -like $ToolsVersion}
} }
if ($ToolsRunningStatus) { if ($ToolsRunningStatus) {
$vmList = $vmList | Where {$_.Guest.ExtensionData.ToolsRunningStatus -eq $ToolsRunningStatus} $vmList = $vmList | Where-Object {$_.Guest.ExtensionData.ToolsRunningStatus -eq $ToolsRunningStatus}
} }
if ($ToolsVersionStatus) { if ($ToolsVersionStatus) {
$vmList = $vmList | Where {$_.Guest.ExtensionData.ToolsVersionStatus -eq $ToolsVersionStatus} $vmList = $vmList | Where-Object {$_.Guest.ExtensionData.ToolsVersionStatus -eq $ToolsVersionStatus}
} }
$vmList $vmList
} }
} }
Function Set-VMToolsUpgradePolicy { Function Get-VMToolsUpgradePolicy {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet sets the VMTool's upgrade policy to "upgradeAtPowerCycle". This advanced function retrieves the VMTools upgrade policy info of specified virtual machines.
.DESCRIPTION .DESCRIPTION
This cmdlet sets the VMTool's upgrade policy to "upgradeAtPowerCycle" of specified virtual machines. This advanced function retrieves the VMTools upgrade policy info of specified virtual machines.
.PARAMETER VM .PARAMETER VM
Specifies the virtual machines which you want to set the VMTool's upgrade policy of. Specifies the virtual machines which you want to query VMTools status of.
.EXAMPLE .EXAMPLE
C:\PS> Import-Module .\VMToolsManagement.psm1 C:\PS> Get-VM "*rhel*" | Get-VMToolsUpgradePolicy
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> Name VMToolsUpgradePolicy
C:\PS> Get-VM -Server $VCServer | Set-VMToolsUpgradePolicy ------ ----------------------
111394-RHEL-6.8-0 manual
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of all virtual machines in the $VCServer vCetner Server. 111394-RHEL-6.8-1 manual
111393-RHEL-Server-7.2 upgradeAtPowerCycle
Retrieves VMTools upgrade policy info of virtual machines with name containing "rhel".
.EXAMPLE .EXAMPLE
C:\PS> Get-VM "*win*" | Set-VMToolsUpgradePolicy C:\PS> Get-VM -Location "MyClusterName" | Get-VMToolsUpgradePolicy
Retrieves VMTools upgrade policy info from virtual machines which run in the "MyClusterName" cluster.
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of virtual machines with name containing "win".
.EXAMPLE .EXAMPLE
C:\PS> Get-VM -Location "MyClusterName" | Set-VMToolsUpgradePolicy C:\PS> Get-VMHost "MyESXiHostName" | Get-VM | Get-VMToolsUpgradePolicy
Retrieves VMTools upgrade policyinfo of virtual machines which run on the "MyESXiHostName" ESXi host.
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of virtual machines in the "MyClusterName" cluster.
.EXAMPLE
C:\PS> Get-VMHost "MyESXiHostName" | Get-VM | Set-VMToolsUpgradePolicy
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of virtual machines on the "MyESXiHostName" ESXi host.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES .NOTES
Author : Daoyuan Wang Author : Kyle Ruddy
Author email : daoyuanw@vmware.com Author email : kmruddy@gmail.com
Version : 1.0 Version : 1.0
==========Tested Against Environment========== ==========Tested Against Environment==========
VMware vSphere Hypervisor(ESXi) Version : 6.5 (build 4564106) VMware vSphere Hypervisor(ESXi) Version : 6.5 (build 7388607)
VMware vCenter Server Version : 6.5 (build 4602587) VMware vCenter Server Version : 6.5 (build 7312210)
PowerCLI Version : PowerCLI 6.5 (build 4624819) PowerCLI Version : PowerCLI 6.5 (build 7155375)
PowerShell Version : 5.1 PowerShell Version : 5.1
#> #>
@@ -448,8 +443,81 @@ Function Set-VMToolsUpgradePolicy {
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VM [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VM
) )
Process {
Get-VM $VM | Select-Object Name, @{Name="VMToolsUpgradePolicy"; Expression={$_.ExtensionData.Config.Tools.ToolsUpgradePolicy}}
}
}
Function Set-VMToolsUpgradePolicy {
<#
.SYNOPSIS
This advanced function sets the VMTool's upgrade policy to either "manual" or "upgradeAtPowerCycle".
.DESCRIPTION
This advanced function sets the VMTool's upgrade policy to either "manual" or "upgradeAtPowerCycle" of specified virtual machines.
.PARAMETER VM
Specifies the virtual machines which you want to set the VMTool's upgrade policy of.
.EXAMPLE
C:\PS> Import-Module .\VMToolsManagement.psm1
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Set-VMToolsUpgradePolicy -UpgradePolicy manual
Sets VMTool's upgrade policy to "manual" of all virtual machines in the $VCServer vCenter Server.
.EXAMPLE
C:\PS> Get-VM "*win*" | Set-VMToolsUpgradePolicy -UpgradePolicy upgradeAtPowerCycle
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of virtual machines with name containing "win".
.EXAMPLE
C:\PS> Get-VM -Location "MyClusterName" | Set-VMToolsUpgradePolicy -UpgradePolicy upgradeAtPowerCycle
Sets VMTool's upgrade policy to "upgradeAtPowerCycle" of virtual machines in the "MyClusterName" cluster.
.EXAMPLE
C:\PS> Get-VMHost "MyESXiHostName" | Get-VM | Set-VMToolsUpgradePolicy -UpgradePolicy manual
Sets VMTool's upgrade policy to "manual" of virtual machines on the "MyESXiHostName" ESXi host.
.NOTES
This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES
Author : Daoyuan Wang
Author email : daoyuanw@vmware.com
Version : 1.1
Update Author : Kyle Ruddy
Update email : kmruddy@gmail.com
==========Tested Against Environment==========
VMware vSphere Hypervisor(ESXi) Version : 6.5 (build 4564106)(build 7388607)
VMware vCenter Server Version : 6.5 (build 4602587)(build 7312210)
PowerCLI Version : PowerCLI 6.5 (build 4624819)(build 7155375)
PowerShell Version : 5.1
#>
[CmdletBinding(SupportsShouldProcess)]
Param (
[Parameter(Mandatory=$true,
ValueFromPipeLine = $true,
ValueFromPipelinebyPropertyName=$True,
Position = 0)]
[ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine[]] $VM,
[Parameter(Mandatory=$false,
Position = 1)]
[ValidateSet("upgradeAtPowerCycle",
"manual")]
[String] $UpgradePolicy
)
Begin { Begin {
$UpgradePolicy = "upgradeAtPowerCycle"
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = $UpgradePolicy $vmConfigSpec.Tools.ToolsUpgradePolicy = $UpgradePolicy
@@ -463,6 +531,7 @@ Function Set-VMToolsUpgradePolicy {
if ($vmView.Config.Tools.ToolsUpgradePolicy -ne $UpgradePolicy) { if ($vmView.Config.Tools.ToolsUpgradePolicy -ne $UpgradePolicy) {
Write-Verbose "Applying 'upgradeAtPowerCycle' setting to $($_.Name)..." Write-Verbose "Applying 'upgradeAtPowerCycle' setting to $($_.Name)..."
$vmView.ReconfigVM($vmConfigSpec) $vmView.ReconfigVM($vmConfigSpec)
Get-VMToolsUpgradePolicy -VM $_
} }
} }
} }
@@ -471,10 +540,10 @@ Function Set-VMToolsUpgradePolicy {
Function Invoke-VMToolsListProcessInVM { Function Invoke-VMToolsListProcessInVM {
<# <#
.Synopsis .Synopsis
This cmdlet lists the processes in the virtual machine. This advanced function lists the processes in the virtual machine.
.Description .Description
This cmdlet lists the running processes in the virtual machine. This advanced function lists the running processes in the virtual machine.
.PARAMETER VM .PARAMETER VM
Specifies the virtual machine which you want to list the processes of. Specifies the virtual machine which you want to list the processes of.
@@ -503,7 +572,7 @@ Function Invoke-VMToolsListProcessInVM {
List the processes in the "MyVMName" VM. List the processes in the "MyVMName" VM.
.NOTES .NOTES
This cmdlet lists processes in the guest OS of virtual machine. This advanced function lists processes in the guest OS of virtual machine.
A VMTools should already be running in the guest OS. A VMTools should already be running in the guest OS.
.NOTES .NOTES
@@ -559,10 +628,10 @@ Function Invoke-VMToolsListProcessInVM {
Function Update-VMToolsImageLocation { Function Update-VMToolsImageLocation {
<# <#
.Synopsis .Synopsis
This cmdlet updates the link /productLocker in ESXi host. This advanced function updates the link /productLocker in ESXi host.
.Description .Description
This cmdlet updates the link /productLocker in ESXi host directly to avoid host reboot. This advanced function updates the link /productLocker in ESXi host directly to avoid host reboot.
.Parameter VMHost .Parameter VMHost
Specifies the ESXi host on which you want to update the /productLocker link. Specifies the ESXi host on which you want to update the /productLocker link.
@@ -574,7 +643,7 @@ Function Update-VMToolsImageLocation {
Specifies the password you want to use for authenticating with the ESXi host. Specifies the password you want to use for authenticating with the ESXi host.
.Parameter ImageLocation .Parameter ImageLocation
Specifies the new image location where you want /producterLocker to link. Specifies the new image location Where-Object you want /producterLocker to link.
.Example .Example
C:\PS> Import-Module .\VMToolsManagement.psm1 C:\PS> Import-Module .\VMToolsManagement.psm1
@@ -586,9 +655,9 @@ Function Update-VMToolsImageLocation {
Update the link /producterLocker on $SampleHost to point to '/locker/packages/6.5.0/'. Update the link /producterLocker on $SampleHost to point to '/locker/packages/6.5.0/'.
.NOTES .NOTES
This cmdlet connects to ESXi host to execute shell command directly. This advanced function connects to ESXi host to execute shell command directly.
Make sure the SSH service on ESXi host is enabled, and a SSH library(Posh-SSH or SSH-Sessions etc.) Make sure the SSH service on ESXi host is enabled, and a SSH library(Posh-SSH or SSH-Sessions etc.)
for powershell is already installed on client where you call This cmdlet. for powershell is already installed on client Where-Object you call this advanced function.
You can instal Posh-SSH by executing: You can instal Posh-SSH by executing:
iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev") iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")
For SSH-Sessions installation and usage, please refer to For SSH-Sessions installation and usage, please refer to
@@ -628,7 +697,7 @@ Function Update-VMToolsImageLocation {
Process { Process {
if (-not (Get-Command New-SSHSession)) { if (-not (Get-Command New-SSHSession)) {
Throw "This cmdlet depends on SSH library. Please ensure a SSH library is already installed!" Throw "This advanced function depends on SSH library. Please ensure a SSH library is already installed!"
} }
$password = new-object System.Security.SecureString $password = new-object System.Security.SecureString
@@ -656,18 +725,21 @@ Function Update-VMToolsImageLocation {
} }
} }
Function Update-VMToolsConfInVM { Function Set-VMToolsConfInVM {
<# <#
.Synopsis .Synopsis
This cmdlet updates the tools.conf content in guest OS. This advanced function sets the tools.conf content in guest OS.
.Description .Description
This cmdlet copies the tools.conf in gueset OS of virtual machine to localhost, This advanced function copies the tools.conf in gueset OS of virtual machine to localhost,
then updates it locally by setting "vmtoolsd.level = info" and copies it back to the guest OS. then sets it locally by setting "vmtoolsd.level" to a valid level and copies it back to the guest OS.
.PARAMETER VM .PARAMETER VM
Specifies the virtual machine to update. Specifies the virtual machine to update.
.PARAMETER LogLevel
Specifies the desired log level to log.
.Parameter GuestUser .Parameter GuestUser
Specifies the user name you want to use for authenticating with the guest OS. Specifies the user name you want to use for authenticating with the guest OS.
@@ -684,16 +756,18 @@ Function Update-VMToolsConfInVM {
Updates the tools.conf in $SampleVM, changes the vmtoolsd log level to info ("vmtoolsd.level = info") for example. Updates the tools.conf in $SampleVM, changes the vmtoolsd log level to info ("vmtoolsd.level = info") for example.
.NOTES .NOTES
This cmdlet updates the tools.conf in guest OS. A VMTools should already be running in the guest OS. This advanced function updates the tools.conf in guest OS. A VMTools should already be running in the guest OS.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
Author email : daoyuanw@vmware.com Author email : daoyuanw@vmware.com
Version : 1.0 Version : 1.1
Update Author : Kyle Ruddy
Update email : kmruddy@gmail.com
==========Tested Against Environment========== ==========Tested Against Environment==========
VMware vSphere Hypervisor(ESXi) Version : 6.5 (build 4564106) VMware vSphere Hypervisor(ESXi) Version : 6.5 (build 4564106)(build 7388607)
VMware vCenter Server Version : 6.5 (build 4602587) VMware vCenter Server Version : 6.5 (build 4602587)(build 7312210)
PowerCLI Version : PowerCLI 6.5 (build 4624819) PowerCLI Version : PowerCLI 6.5 (build 4624819)(build 7155375)
PowerShell Version : 5.1 PowerShell Version : 5.1
#> #>
@@ -707,6 +781,17 @@ Function Update-VMToolsConfInVM {
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $VM, [VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine] $VM,
[Parameter(Mandatory=$true,
Position = 1)]
[ValidateSet("none",
"critical",
"error",
"warning",
"message",
"info",
"debug")]
[String] $LogLevel,
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[String] $GuestUser, [String] $GuestUser,
@@ -756,7 +841,7 @@ Function Update-VMToolsConfInVM {
# Updates tools.conf by setting vmtoolsd.level = info, just for example. # Updates tools.conf by setting vmtoolsd.level = info, just for example.
############################################################################# #############################################################################
$confContent = Get-Content $localToolsConfFile $confContent = Get-Content $localToolsConfFile
$updatedContent = "vmtoolsd.level = info" $updatedContent = "vmtoolsd.level = $LogLevel"
Write-Verbose "Editing tools.conf (set 'vmtoolsd.level = info' for example)..." Write-Verbose "Editing tools.conf (set 'vmtoolsd.level = info' for example)..."
if ($confContent -match "vmtoolsd\.level") { if ($confContent -match "vmtoolsd\.level") {
@@ -775,19 +860,23 @@ Function Update-VMToolsConfInVM {
-GuestUser $GuestUser -GuestPassword $GuestPassword -Force -ErrorAction:Stop -GuestUser $GuestUser -GuestPassword $GuestPassword -Force -ErrorAction:Stop
} catch { } catch {
Write-Error "Failed to update tools.conf of $VM" Write-Error "Failed to update tools.conf of $VM"
Write-Verbose "Removing the local tools configuration file"
Remove-Item $localToolsConfFile
return return
} }
Write-Host "The tools.conf updated in $VM successfully." -ForegroundColor Green Write-Host "The tools.conf updated in $VM successfully." -ForegroundColor Green
Write-Verbose "Removing the local tools configuration file"
Remove-Item $localToolsConfFile
} }
} }
Function Invoke-VMToolsVIBInstall { Function Invoke-VMToolsVIBInstall {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet installs VMTool VIB in ESXi hosts. This advanced function installs VMTool VIB in ESXi hosts.
.DESCRIPTION .DESCRIPTION
This cmdlet installs VMTool VIB in specified ESXi hosts. This advanced function installs VMTool VIB in specified ESXi hosts.
.PARAMETER VMHost .PARAMETER VMHost
Specifies the ESXi hosts which you want to install VMTool VIB in. Specifies the ESXi hosts which you want to install VMTool VIB in.
@@ -816,7 +905,7 @@ Function Invoke-VMToolsVIBInstall {
Installs VMTool VIB in ESXi host of the "MyClusterName" cluster. Installs VMTool VIB in ESXi host of the "MyClusterName" cluster.
.NOTES .NOTES
This cmdlet assumes that you are connected to at least one vCenter Server system. This advanced function assumes that you are connected to at least one vCenter Server system.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
@@ -846,7 +935,7 @@ Function Invoke-VMToolsVIBInstall {
foreach ($_ in $VMHost) { foreach ($_ in $VMHost) {
$esxcli = Get-EsxCLI -VMHost $_ -V2 $esxcli = Get-EsxCLI -VMHost $_ -V2
$result = $esxcli.software.vib.list.Invoke() | where {$_.name -match 'tools'} $result = $esxcli.software.vib.list.Invoke() | Where-Object {$_.name -match 'tools'}
Write-Verbose "Existing tools VIB on $_ before installing: $($result.Name)_$($result.Version)" Write-Verbose "Existing tools VIB on $_ before installing: $($result.Name)_$($result.Version)"
# Install VIBs # Install VIBs
@@ -861,7 +950,7 @@ Function Invoke-VMToolsVIBInstall {
Write-Error "Failed to install VMTools VIB package!" Write-Error "Failed to install VMTools VIB package!"
} else { } else {
Write-Verbose $result.Message Write-Verbose $result.Message
$result = $esxcli.software.vib.list.Invoke() | where {$_.name -match 'tools'} $result = $esxcli.software.vib.list.Invoke() | Where-Object {$_.name -match 'tools'}
Write-Verbose "Tools VIB on $_ after installing: $($result.Name)_$($result.Version)" Write-Verbose "Tools VIB on $_ after installing: $($result.Name)_$($result.Version)"
Write-Host "VMTools VIB package installed on $_ successfully." -ForegroundColor Green Write-Host "VMTools VIB package installed on $_ successfully." -ForegroundColor Green
} }
@@ -872,10 +961,10 @@ Function Invoke-VMToolsVIBInstall {
Function Invoke-VMToolsUpgradeInVMs { Function Invoke-VMToolsUpgradeInVMs {
<# <#
.SYNOPSIS .SYNOPSIS
This cmdlet upgrades VMTools to the version bundled by ESXi host. This advanced function upgrades VMTools to the version bundled by ESXi host.
.DESCRIPTION .DESCRIPTION
This cmdlet upgrades VMTools of specified virtual machines to the version This advanced function upgrades VMTools of specified virtual machines to the version
bundled by ESXi host. You can also specify the number of virtual machines bundled by ESXi host. You can also specify the number of virtual machines
to upgrade in parallel. to upgrade in parallel.
@@ -896,7 +985,7 @@ Function Invoke-VMToolsUpgradeInVMs {
C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password> C:\PS> $VCServer = Connect-VIServer -Server <vCenter Server IP> -User <vCenter User> -Password <vCenter Password>
C:\PS> Get-VM -Server $VCServer | Invoke-VMToolsUpgradeInVMs -MaxParallelUpgrades 5 C:\PS> Get-VM -Server $VCServer | Invoke-VMToolsUpgradeInVMs -MaxParallelUpgrades 5
Upgrades VMTools of all virtual machines in the $VCServer vCetner Server, 5 at a time in parallel. Upgrades VMTools of all virtual machines in the $VCServer vCenter Server, 5 at a time in parallel.
.EXAMPLE .EXAMPLE
C:\PS> Get-VM | Invoke-VMToolsUpgradeInVMs -GuestOSType windows -MaxParallelUpgrades 1 | ft -Autosize C:\PS> Get-VM | Invoke-VMToolsUpgradeInVMs -GuestOSType windows -MaxParallelUpgrades 1 | ft -Autosize
@@ -929,7 +1018,7 @@ Function Invoke-VMToolsUpgradeInVMs {
Upgrades VMTools of virtual machines on the "MyESXiHostName" ESXi host, 5 at a time. Upgrades VMTools of virtual machines on the "MyESXiHostName" ESXi host, 5 at a time.
.NOTES .NOTES
This cmdlet assumes an old VMTools is already running in the virtual machine. This advanced function assumes an old VMTools is already running in the virtual machine.
.NOTES .NOTES
Author : Daoyuan Wang Author : Daoyuan Wang
@@ -1026,7 +1115,7 @@ Function Invoke-VMToolsUpgradeInVMs {
# Load PowerCLI module and connect to VCServer, as child thread environment is independent with parent # Load PowerCLI module and connect to VCServer, as child thread environment is independent with parent
if(-not $global:DefaultVIServer) { if(-not $global:DefaultVIServer) {
$moduleName = "vmware.vimautomation.core" $moduleName = "vmware.vimautomation.core"
if(-not (Get-Module | Where {$_.name -eq $moduleName})) { if(-not (Get-Module | Where-Object {$_.name -eq $moduleName})) {
try { try {
Import-Module $moduleName -ErrorAction SilentlyContinue | Out-Null Import-Module $moduleName -ErrorAction SilentlyContinue | Out-Null
} }
@@ -1130,7 +1219,7 @@ Function Invoke-VMToolsUpgradeInVMs {
End { End {
#Verify all threads completed #Verify all threads completed
while (($jobs | Where {$_.Handle.iscompleted -ne Completed}).Count -gt 0) { while (($jobs | Where-Object {$_.Handle.iscompleted -ne Completed}).Count -gt 0) {
Start-Sleep -Seconds 5 Start-Sleep -Seconds 5
} }