Automation

 View Only
  • 1.  PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 11:52 AM

    Greetings

    Admittedly this is probably more focused on PowerShell and less on VMware but VMware tools is the target. :smileyhappy:

    I am attempting to write a script to install VMware tools remotely onto VMs that don't have VMware tools installed.

    So far I have the below...

    Import-Module VMware.VimAutomation.Cis.Core

    Import-Module VMware.VimAutomation.Core

    Import-Module VMware.VimAutomation.HA

    Import-Module VMware.VimAutomation.Sdk

    Import-Module VMware.VimAutomation.Storage

    Import-Module VMware.VimAutomation.Vds

    Connect-VIServer vCenterserver.domain


    $username = "domain\username"

    $SecurePassword = Read-Host -Prompt "Enter **DOMAIN** password" -AsSecureString

    $Credentials = New-Object System.Management.Automation.PSCredential `

       -ArgumentList $UserName, $SecurePassword


       $VM = "Test01"

       Get-vm $VM | Dismount-Tools

       Get-vm $VM | Mount-Tools

       $RMsession = New-PSSession -ComputerName $VM -Credential $Credentials

       Invoke-Command -Session $RMSession -ScriptBlock {$CDRom = Get-WmiObject -class Win32_CDROMDrive | select Drive | ForEach {$_.Drive}}

       Invoke-Command -Session $RMSession -ScriptBlock {$Subpath = '\setup.exe -s -v-qn ADDLOCAL=ALL REBOOT=R'}

       Invoke-Command -Session $RMSession -ScriptBlock {$ExecuteEXE = Join-Path -Path $CDRom -ChildPath $Subpath}

       Invoke-Command -Session $RMSession -ScriptBlock {start-process $executeEXE}

       exit

       Remove-PSSession *

       Disconnect-VIServer * -Confirm:$False


    I have confirmed that $executeEXE does indeed = Z:\setup.exe -s -v-qn ADDLOCAL=ALL REBOOT=R

    When I run the script in the ISE I get :

    Start-Process : This command cannot be executed due to the error: The system cannot find the file specified.

    At line:1 char:14

    + start-process <<<< $executeEXE

      + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException

      + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

    Admittedly I'm very inexperienced with PowerShell remoting.

    It would appear that when I execute the whole script it doesn't seem to actually be invoking the command on the remote computer but rather on the local computer. The VMware tools are mounting and the path in $executeEXE is 100% correct and the PSSession is also definitely open but no magic?

    Any guidance would be greatly appreciated.



  • 2.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 12:47 PM

    Are you sure the remote session is established ?

    What is in $RMSession ?

    You could run the commands interactively and then use Enter-PSSession to check if that works



  • 3.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 12:54 PM

    Hey man.

    Yep. Its definitely established.

    PS C:\> $RMsession

    Id Name            ComputerName    State         ConfigurationName     Availability
    -- ----            ------------    -----         -----------------     ------------
      1 Session1        Test01          Opened        Microsoft.PowerShell     Available

    Running through the commands manually works but not the $ExecuteEXE. That always breaks as above. The string is right but its almost like the shell doesn't understand it.



  • 4.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 01:25 PM

    Take care of the scope.

    Place all these lines in 1 block and use 1 Invoke-Command



  • 5.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 02:25 PM

    Greetings again

    Ok so I changed it to this (I'm not sure what you meant by "take care of the scope") :

    Invoke-Command -Session $RMSession -ScriptBlock {$CDRom = Get-WmiObject -class Win32_CDROMDrive | select Drive `

    | ForEach {$_.Drive}; $Subpath = '\setup.exe -s -v-qn ADDLOCAL=ALL REBOOT=R'; $ExecuteEXE = Join-Path -Path $CDRom -ChildPath $Subpath; start-process $executeEXE}

    Assuming that's what you meant...

    The result is below. Still no joy. :smileyconfused:

    This command cannot be executed due to the error: The system cannot find the file specified.

      + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException

      + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

      + PSComputerName : Test01

    Followed by some checks by me... :

    PS C:\> $RMsession

    Id Name ComputerName State ConfigurationName Availability

    -- ---- ------------ ----- ----------------- ------------

      2 Session2 Test01 Opened Microsoft.PowerShell Available

     

     

    PS C:\> Enter-PSSession $RMsession

    [Test01]: PS C:\> $cdrom

    Z:

    [Test01]: PS C:\> $ExecuteEXE

    Z:\setup.exe -s -v-qn ADDLOCAL=ALL REBOOT=R

    [Test01]: PS C:\> 

    Thank you very much for your help!



  • 6.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 03:42 PM

    That was what I meant, the scope is per Invoke-Command.

    Can you perhaps display the content from the $ExecuteEXE variable before you call Start-Process ?

    The error is quite clear, the EXE can not be found.

    Update: just noticed you did display the content.

    If you do a Enter-PSSesion, can you run that Z:\setup.exe .... as is (from the prompt) ?



  • 7.  RE: PowerShell remoting to install VMWare tools
    Best Answer

    Posted Jul 17, 2015 05:45 PM

    Greetings LucD

    Looks like I may have found the problem.

    In looking for a way to run the process as a background process so I could do a get-process while it was running to show you that it was executing, I discovered that start-process allows for specifying the  -ArgumentList parameter. (windows - Start a detached background process in PowerShell - Stack Overflow) (Answer 2)

    So it looks like this :

    Invoke-Command -Session $RMSession -ScriptBlock {$CDRom = Get-WmiObject -class Win32_CDROMDrive | select Drive `

    | ForEach {$_.Drive}; $Subpath = '\setup.exe'; $ExecuteEXE = Join-Path -Path $CDRom -ChildPath $Subpath; start-process $executeEXE -ArgumentList '-s -v-qn ADDLOCAL=ALL REBOOT=R'}


    That much to my surprise results in:

    [Test01]: PS C:\> Get-Process setup*

     

    Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName

    -------  ------    -----      ----- -----   ------     -- -----------

         68      10     1540       5088    56     0.17   3220 setup     

        108      10     2324       7340    76     0.14   3504 setup64  

    And VMware tools being installed.

    Thank you very much for your time and trouble. Always very much appreciated!



  • 8.  RE: PowerShell remoting to install VMWare tools

    Posted Jul 17, 2015 06:21 PM

    Glad you found it, and you're welcome