Hi experts
I need an help, i want to install vmware tools from the scrip(My goal is to install to vmware tools and shutdown virtual machines) and manually upgrade virtual hardware and power on the virtual machines.
I am looking for a script to install vmware tools
$vCname = Read-Host “Enter the vCenter or ESXi host name to connect:”
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-VIserver $vcname
$VirtualMachines = Get-Content "VMList.txt"
foreach ($vm in (Get-VM -Name $VirtualMachines)){
if($vm.config.Tools.ToolsUpgradePolicy -ne "UpgradeAtPowerCycle") {
$config = New-Object VMware.Vim.VirtualMachineConfigSpec
$config.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$config.Tools = New-Object VMware.Vim.ToolsConfigInfo
$config.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ExtensionData.ReconfigVM($config)
Write-Host "Update Tools Policy on $vm completed" -ForegroundColor green
}
else {
Write-Host "$vm couldn't found on vCenter Server" -ForegroundColor red
}
}
Hi Below is the script for tools installation on windows PCs but i need to test it. That will take some time.
Hope, till now you have got the required info, do not forget to mark this resolved.
Connect-VIServer 192.168.0.106
$GuestCred = Get-credential Administrator
$VM = Get-content "c:\hi.txt"
#Mount vmware tools media
mount-tools -vm $VM
#Find the drive letter of the mounted media
$DrvLetter = Get-WmiObject -class 'win32_CDROMDrive' `
-ComputerName $VM.Name `
-Credential $GuestCred |
Where-Object {$_.Volumename -match "VMware Tools"} |
Select-object -ExpandProperty Drive
#Build our cmd line
$cmd = "$($DrvLetter)\setup.exe /S /V`"/qn Reboot=ReallySuppress Addlocal=All`"
#spawn a new process on the remote VM, and execute setup
$go = Invoke-WmiMethod -path Win32_process`
-Name Create`
-Credential $GuestCred`
-ArgumentList $cmdif ($go.Returnvalue -ne 0)
{
write-warning "Installer returned code $($go.ReturnValue)!
Unmounting media"
Dismount-Tools -VM $VM
}
else
{
Write-Verbose "Tool installation successfully triggered on`
$($VM.Name) media will be ejected upon completion."
}
Update tools at power cycle option is best option for tool upgrade. Find the below script which will enable this option in all VMs and also will give you the result in csv files.
First get connect with the Vcenter server on which you are going to perform this then list the VMs in VMname.txt if it is not for all VMs else use only Get-VM.
-------------------------------------------------------------------------------
#list name of servers on which changes required in file
$n = Get-Content "D:\Jitu\VMName.txt"
foreach ($b in $n){
#Command will extact details of VM
$vm = Get-VM -Name $b | % {Get-View $_.ID}
$value = New-Object VMware.Vim.VirtualMachineConfigSpec
$value.tools = New-Object VMware.Vim.ToolsConfigInfo
#This is where you mention which policy need to enter ,here we only need to enable checkbox to uprade vmware tools at power cycle. Same has been selected
$value.tools.toolsUpgradePolicy = "upgradeAtPowerCycle"
$vm.ReconfigVM_Task($value)
#Below command will export the policy details of all VMs(updated/yet to be updated) in respective VC in csv format.
get-view -ViewType virtualmachine | select name,@{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy } } | Export-Csv "D:\ToolsOutput.csv" -UseCulture -NoTypeInformation
}
#or if we only need to export of tasks executed for number of VM then remove last line and add below instead of get-view:
$vm.ReconfigVM_Task($value)| Export-Csv "D:\ToolsOutput.csv" -UseCulture -NoTypeInformation
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hi
thanks alot, wht does upgradeAtPowerCycle mean
This will not restart the servers right?
My plan is to shutdown the servers after installing vmware tools
Yes it will not powercycle the VM instantly.
upgrade at power cycle means, whenever VM gets restarted for any reason, it will check the Tools and upgrade it if older.
Just add below commands from powershell
connect your vcenter server first and then run below command.
get-vm | update-tools (upgrade the tools immidiately and restart the VM.
get-vm | shutdown-vmguest -Confirm:$false (Once above command gets successfull then this command will shutdown again all VMs.)
This will shutdown the vm gracefully and in that case, vm tool should work however, you can also force shutdown by using command below
get-vm | stop-vm
Hope, this will help you.
Thanks alot for the support
Will the below script work for me(i.e install or upgrade vmware tools and shutdown the VMs)
#list name of servers on which changes required in file
$n = Get-Content "D:\Jitu\VMName.txt"
foreach ($b in $n){
$vm = Get-VM -Name $b | % {Get-View $_.ID}
$value = New-Object VMware.Vim.VirtualMachineConfigSpec
$value.tools = New-Object VMware.Vim.ToolsConfigInfo
get-vm | update-tools
get-vm | shutdown-vmguest -Confirm:$false
$vm.ReconfigVM_Task($value)| Export-Csv "D:\ToolsOutput.csv" -UseCulture -NoTypeInformation
}
hi jitu
could you cross check once
Please use vRO tool(if you have in your organization) to install vmware tool and shutdown VMs.
script is only to check the option update tools at powercycle. So leave that a side.
You just need to run few commands and you are done.
connect-viserver vcenter_server_name
get-vm | update-tools
once all VM tools gets updated run below command to shutdown all VMs
get-vm | shutdown-vmguest -confirm:$false
i just wrote below script will this work
i have a text file of VMs, is update-tools is right syntax, and there are two options to install vmware tools one is typical and other complete, if i need to install complete, how will i install
$VMs = Get-Content C:\VMList.txt
Foreach ($VM in $VMs)
{
update-tools
Shutdown-VMGuest $VM -Confirm:$false
Write-Host "$VM Shutdown initiated"
}
do i need to add Update-Tools -NoReboot
$VMs = Get-Content C:\VMList.txt
Foreach ($VM in $VMs)
{
Update-Tools -NoReboot
Shutdown-VMGuest $VM -Confirm:$false
Write-Host "$VM Shutdown initiated"
}
yes, that is good.
add -noreboot so that after installing the tools, it wont reboot the VMs. The next command will shutdown the VMs.
Thanks alot Jitu
If i need to go with complete installation of vmware tools rather than typical how will it work
i dont have any VM to test , can any expert who has test environment confirm. I will be thankful
$VMs = Get-Content C:\VMList.txt
Foreach ($VM in $VMs)
{
Update-Tools –NoReboot -RunAsync
Shutdown-VMGuest $VM -Confirm:$false
Write-Host "$VM Shutdown initiated"
}
Hi Below is the script for tools installation on windows PCs but i need to test it. That will take some time.
Hope, till now you have got the required info, do not forget to mark this resolved.
Connect-VIServer 192.168.0.106
$GuestCred = Get-credential Administrator
$VM = Get-content "c:\hi.txt"
#Mount vmware tools media
mount-tools -vm $VM
#Find the drive letter of the mounted media
$DrvLetter = Get-WmiObject -class 'win32_CDROMDrive' `
-ComputerName $VM.Name `
-Credential $GuestCred |
Where-Object {$_.Volumename -match "VMware Tools"} |
Select-object -ExpandProperty Drive
#Build our cmd line
$cmd = "$($DrvLetter)\setup.exe /S /V`"/qn Reboot=ReallySuppress Addlocal=All`"
#spawn a new process on the remote VM, and execute setup
$go = Invoke-WmiMethod -path Win32_process`
-Name Create`
-Credential $GuestCred`
-ArgumentList $cmdif ($go.Returnvalue -ne 0)
{
write-warning "Installer returned code $($go.ReturnValue)!
Unmounting media"
Dismount-Tools -VM $VM
}
else
{
Write-Verbose "Tool installation successfully triggered on`
$($VM.Name) media will be ejected upon completion."
}
Hi jitu
I will try to test and update u
If I need to run on 50 vms all my vms are in text file can I run the script
yes try that.
getting erro
At C:\testt.ps1:23 char:25
+ write-warning "Installer <<<< returned code $($go.ReturnValue)!
+ CategoryInfo : ParserError: (Installer:String) [], ParseExcepti
on
+ FullyQualifiedErrorId : UnexpectedToken