1 2 Previous Next 20 Replies Latest reply on May 15, 2018 6:11 AM by ym2018 Go to original post
      • 15. Re: Copy-VMGuestFile - Error while trying to copy file from host to VM
        ym2018 Novice

        Sorry for the confusion. I never used PowerCLI before. I'm new at this.

        Thanks for your help so far.

        • 16. Re: Copy-VMGuestFile - Error while trying to copy file from host to VM
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          Ok, can you try with this updated version?

          It restarts the guest OS after the revert to snapshot.

           

          If that still gives an issue, try the copy without doing the revert to snapshot.

           

          #connect VIserver:

           

          Write-Host " Connecting to your server $VIserver"

          $con = Connect-VIServer $VIserver -User $vCenterUser -Password $vCenterPass -WarningAction SilentlyContinue

          Write-Host " Get the snapshot object to revert"

          $snapshot = Get-Snapshot -VM $VMName -Name $Snapname


          if ($snapshot -ne $null) {

             Set-VM -VM $VMName -Snapshot $snapshot  -Confirm:$false

             Write-Host "Snapshot reverted to $Snapname"

            

             $guest = Restart-VMGuest -VM $VMName -Confirm:$false


             $guest = Get-VMGuest -Name $VMName

             while($vm.State -ne 'Running'){

            sleep 5

             $guest = Get-VMGuest -Name $VMName

            }

          }

          else{

             Write-Host "Snapshot name given does not exist " -ForegroundColor Red

             Write-Host "Snapshot is not reverted " -ForegroundColor Red

          }

          ########################Code to copy script to VM #########################

          Write-Host "Copying in process.....Data trasfering from $Source to $Destination " -ForegroundColor Green 

          $sCopy = @{

            Source = "$Source"

            Destination = "$Destination"

            LocalToGuest = $true

            VM = $Machinename

            GuestUser = "$VMGuestUserID"

            GuestPassword = "$VMGuestPassword"

            Verbose = $true

          }

          Copy-VMGuestFile @sCopy

          • 17. Re: Copy-VMGuestFile - Error while trying to copy file from host to VM
            ym2018 Novice

            I'm getting the following error:

             

            Snapshot reverted to Clean

            Restart-VMGuest : 5/15/2018 3:45:34 PM  Restart-VMGuest         Operation "Restart VM guest" failed for VM "TestVM" for

            the following reason: Cannot complete operation because VMware Tools is not running in this virtual machine.

            At C:\Automation Framework\PowerCLI-NewVMDeployment\RevertCleanSnapshot.ps1:37 char:13

            +    $guest = Restart-VMGuest -VM $VMName -Confirm:$false

            +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                + CategoryInfo          : NotSpecified: (:) [Restart-VMGuest], VimException

                + FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RestartVmGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.C

               ommands.RestartVmGuest

             

             

            Get-VMGuest : A parameter cannot be found that matches parameter name 'Name'.

            At C:\Automation Framework\PowerCLI-NewVMDeployment\RevertCleanSnapshot.ps1:40 char:25

            +    $guest = Get-VMGuest -Name $VMName

            +                         ~~~~~

                + CategoryInfo          : InvalidArgument: (:) [Get-VMGuest], ParameterBindingException

                + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuest

             

             

            *** I have to mention that I have VMWare tools installed on VM

            • 18. Re: Copy-VMGuestFile - Error while trying to copy file from host to VM
              ym2018 Novice

              Copy without doing the revert to snapshot - works.

              • 19. Re: Copy-VMGuestFile - Error while trying to copy file from host to VM
                LucD Guru
                Community WarriorsvExpertUser Moderators

                That confirms my suspicion that the VMware Tools are not immediately available after the revert to snapshot.

                This tries with an additional While-loop to wait for the VMware Tools to come back.
                And it doesn't restart the Guest OS anymore.

                 

                 

                 

                #connect VIserver:

                 


                Write-Host " Connecting to your server $VIserver"

                $con = Connect-VIServer $VIserver -User $vCenterUser -Password $vCenterPass -WarningAction SilentlyContinue

                Write-Host " Get the snapshot object to revert"

                $snapshot = Get-Snapshot -VM $VMName -Name $Snapname


                if ($snapshot -ne $null) {

                   Set-VM -VM $VMName -Snapshot $snapshot  -Confirm:$false

                   Write-Host "Snapshot reverted to $Snapname"


                   $vm = Get-VM -Name $VMName

                   while($vm.Guest.State -ne 'Running'){

                    sleep 5

                     $vm = Get-VM -Name $VMName

                  }

                }

                else{

                   Write-Host "Snapshot name given does not exist " -ForegroundColor Red

                   Write-Host "Snapshot is not reverted " -ForegroundColor Red

                }

                ########################Code to copy script to VM #########################

                Write-Host "Copying in process.....Data trasfering from $Source to $Destination " -ForegroundColor Green

                $sCopy = @{

                  Source = "$Source"

                  Destination = "$Destination"

                  LocalToGuest = $true

                  VM = $Machinename

                  GuestUser = "$VMGuestUserID"

                  GuestPassword = "$VMGuestPassword"

                  Verbose = $true

                }

                Copy-VMGuestFile @sCopy

                1 2 Previous Next