risingflight
Enthusiast
Enthusiast

vmware tools

Hi experts

Can anyone correct this script, i have vms in text file i need to install vmware tools on these machines.

$GuestCrd = Get-Credential Administrator

$VMs = Get-content .\servers.txt

foreach $VM in $VMS {

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 '

-ComputerName $VM.Name '

-ArgumentList $cmd

if ($go.ReturnValue -no 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."

}

}

0 Kudos
15 Replies
Sreejesh_D
Virtuoso
Virtuoso

pleas provide the details on error you receive. A screenshot or error copy will be helpful.

0 Kudos
LucD
Leadership
Leadership

There are a few minor issues in your script.

Try like this

$GuestCrd = Get-Credential Administrator

$VMs = Get-content .\servers.txt

foreach ($VM in $VMS) {

#    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 '

        -ComputerName $VM.Name '

        -ArgumentList $cmd

    

    if ($go.ReturnValue -ne 0)

    {

        Write-Warning "Installer returned code $($go.ReturnValue) | Unmounting media"

        Dismount-Tools -VM $VM -Confirm:$false

    }

    else

    {

        Write-Verbose "Tool installation successfully triggered on"

            "$($VM.Name) media will be ejected upon completion."

    }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

I am getting the below error experts plz help me 

Mount-Tools : Cannot validate argument on parameter 'Guest'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At c:\script.ps1:7 char:16
+     Mount-Tools <<<<  $VM
     + CategoryInfo          : InvalidData: (:) [Mount-Tools], ParameterBinding
    ValidationException
     + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
    ation.ViCore.Cmdlets.Commands.MountVMTools

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum
ent is null or empty. Supply an argument that is not null or empty and then try
  the command again.
At c:\script..ps1:9 char:72
+     $DrvLetter = Get-WmiObject -Class 'Win32_CDROMDRIVE'  -ComputerName <<<<
  $VM.Name  -Credential $GuestCred |?{$_.VolumeName -match "VMware Tools"}|Selec
t -ExpandProperty Drive
     + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindi
    ngValidationException
     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
    Shell.Commands.GetWmiObjectCommand

Invoke-WmiMethod : Cannot validate argument on parameter 'ComputerName'. The ar
gument is null or empty. Supply an argument that is not null or empty and then
try the command again.
At c:\script.ps1:13 char:97
+     $go = Invoke-WMIMethod -path Win32_Process -Name Create -Credential $Gues
tCred -ComputerName <<<<  $VM.Name -ArgumentList $cmd
     + CategoryInfo          : InvalidData: (:) [Invoke-WmiMethod], ParameterBi
    ndingValidationException
     + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
    Shell.Commands.InvokeWmiMethod

WARNING: Installer returned code $ (.ReturnValue) | Unmounting media
Dismount-Tools : Cannot validate argument on parameter 'VM'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At c:\script.ps1:18 char:27
+         Dismount-Tools -VM <<<<  $VM
     + CategoryInfo          : InvalidData: (:) [Dismount-Tools], ParameterBind
    ingValidationException
     + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
    ation.ViCore.Cmdlets.Commands.DismountVmTools

0 Kudos
LucD
Leadership
Leadership

What do you have in the file servers.txt?

That should have the VM names, one per line, no empty lines.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

Hi experts

need an urgent help, i am able to mount the vmware tools in CDRom.(servers.txt contains my vmnames on which i want to update vmware tools)

from the lin  $cmd = "$($DrvLetter)  \setup.exe /S /v`"/qn  REBOOT=ReallySuppress  ADDLOCAL=ALL`"" i am getting errors, please correct the script 

$GuestCred = Get-Credential myaccount1
$VMs = Get-content .\myservers.txt
#$vms = get-vm

foreach ($VM in $VMs)
{
     #Mount VMware Tools Media
     Mount-Tools -VM $VM
# $hostname = $vm.Guest.HostName
         #Find the drive letter of the mounted media
     $DrvLetter = Get-WmiObject -Class Win32_CDROMDRIVE  -ComputerName $vm -Credential $GuestCred |?{$_.VolumeName -match "VMware Tools"}|Select -ExpandProperty Drive

$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 '
-ComputerName $VM.Name '
-ArgumentList $cmd
if ($go.ReturnValue -no 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."
}


}

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Can you tell us which error message you get?

The space between $(DrvLetter) and \setup.exe must be removed for the script to work correctly.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

If you look at my corrected script above you'll notice you put in an extra blank.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

hi

i have executed $cmd = "$($DrvLetter)setup.exe /S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL`""

and          $cmd = "$($DrvLetter)setup64.exe /S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL`""

but still same issue

0 Kudos
LucD
Leadership
Leadership

What error message do you get?
Did you try adding MSI logging (/l*v)?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

i am running the script line by line

As i reach till below line, nothing happens , i can see vmware tools have been mounted on the virtual machines but setup doesnot run i cannot see any installer running.

i tried setup.exe and setup64.exe but nothing is happening.

$cmd = "$($DrvLetter)setup.exe /S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL`""

$cmd = "$($DrvLetter)setup64.exe /S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL`""

i dont know the way iam apporaching to install vmware tools is the right way to upgrade on bulk servers.

i want to achieve this through script only.experts i am struck please do help me on the script.

0 Kudos
LucD
Leadership
Leadership

You seem to have left out the back-slash after the drive letter.
And "When you specify a path with a drive letter but no initial backslash, it is typically interpreted as a relative path to the current directory on the specified drive."

Try changing the line, as it also says in the original, with a slash behind the drive letter.

$cmd = "$($DrvLetter)\setup.exe /S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL`""


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

Hi experts

I can mount the vmware tools and then  i am confused

from the line #$cmd = "$($DrvLetter) to -ArgumentList $cmd i am lost

my confusion is also at the line -ComputerName $vm  or ComputerName $VM.Name

$GuestCred = Get-Credential .\administrator

$VMs = Get-content .\myservers.txt

#$vms = get-vm

foreach ($VM in $VMs)

{

    #Mount VMware Tools Media

    Mount-Tools -VM $VM

    $DrvLetter = Get-WmiObject -Class Win32_CDROMDRIVE  -ComputerName $vm -Credential $GuestCred |?{$_.VolumeName -match "VMware Tools"}|Select-Object -ExpandProperty Drive

#$cmd = "$($DrvLetter)\setup64.exe /S /v`"/qn  REBOOT=ReallySuppress  ADDLOCAL=ALL`""

$cmd = "$($DrvLetter)\setup64.exe /S /v`"/qn  REBOOT=ReallySuppress  REINSTALL`""

$go = Invoke-WMIMethod -path Win32_Process '

-Name Create '

-Credential $GuestCred '

#-ComputerName $vm '

-ComputerName $VM.Name '

-ArgumentList $cmd

}

0 Kudos
LucD
Leadership
Leadership

Can you show us any errors you are getting?

And can you attach the .ps1 file you are using?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
risingflight
Enthusiast
Enthusiast

Hi i have attached ps1

0 Kudos
LucD
Leadership
Leadership

Try with the attached changed version


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos