I’m trying to use the following command to copy file from host to VM:
Copy-VMGuestFile -Source "$TestFilePath" -Destination "$DestinationPath" -LocalToGuest -Force -VM $Machinename -GuestUser "$VMGuestUserID" -GuestPassword "$VMGuestPassword" -ToolsWaitSecs -10 -Verbose
The problem is that I’m getting an error:
> WARNING: The guest OS for the virtual machine 'TestVM' is unknown. The operation may fail. Copy-VMGuestFile : Copy-VMGuestFile The guest operations agent could not be contacted.
After googling this error, in several posts, there was a recommendation to install VMWare tools. I did. But still copy file command is failing.
Virtual machine is Microsoft Windows 10 (64-bit). Firewall is off. VMWare tools is installed.
Powershell version 5.1.
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.
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Are the VMware Tools running in the guest OS?
What does this return?
Get-VM <YourVM> | Select -ExpandProperty Guest
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, VMWare Tools is installed in the guest OS.
Get-VM <YourVM> | Select -ExpandProperty Guest, returned:
State IPAddress OSFullName
----- --------- ----------
Running {172.10.10.66, fe... Microsoft Windows 10 (64-bit)
Ok, that seems to be ok, and VMware Tools obviously discovered that the target is running the Windows 10 guest OS.
Can you try this version, and show what is returned?
Note I used splatting to improve readibility.
Source = "$TestFilePath"
Destination = "$DestinationPath"
LocalToGuest = $true
VM = $Machinename
GuestUser = "$VMGuestUserID"
GuestPassword = "$VMGuestPassword"
Verbose = $true
}
Copy-VMGuestFile @sCopy
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm getting the same error:
VERBOSE: 5/15/2018 9:40:34 AM Copy-VMGuestFile Started execution
VERBOSE: Copying source file 'C:\Temp\test.txt' from host to destination 'C:\test\' on VM 'TestVM'.
WARNING: The guest OS for the virtual machine 'TestVM' is unknown. The operation may fail.
Copy-VMGuestFile : 5/15/2018 9:40:34 AM Copy-VMGuestFile The guest operations agent could not be contacted.
At C:\Automation Framework\RevertCleanSnapshot.ps1:52 char:1
+ Copy-VMGuestFile @sCopy
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMGuestFile], GuestOperationsUnavailable
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_DirectoryExistsInGuest_ViError,VMware.VimAutomation.ViCore.C
mdlets.Commands.CopyVMGuestFile
Can you check the vmware.log file? Normally it contains additional info on interactions with the VMware Tools.
It's located in the VM's folder on the datastore.
Some more things to try.
Did you power off/power on the VM after the installation of the VMware Tools?
Did you stop/start your PowerShell/PowerCLI session from which you run the cmdlet?
And which PowerCLI version are you using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you power off/power on the VM after the installation of the VMware Tools? Yes I did. I saved this state in a snapshot. My script revert to this snapshot before trying to copy the file.
Did you stop/start your PowerShell/PowerCLI session from which you run the cmdlet? Yes I did. I closed the PowerShell and re-opened it.
VMware PowerCLI 10.0.0 build 7895300.
That leaves the vmware.log to check.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is part of the log file. I'm not sure what to look for...
2018-05-15T10:56:12.194Z| vcpu-2| I120: HBACommon: First write on scsi0:0.fileName='/vmfs/volumes/574ef75b-ec725889-f36b-2047479865ee/TestVM/TestVM-000001.vmdk'
2018-05-15T10:56:12.194Z| vcpu-2| I120: DDB: "longContentID" = "2cd7ba3357f17a95d74c60c8fd1ff90b" (was "5d7b432d1d4c8bd075aa38c95f55ed5e")
2018-05-15T10:56:12.199Z| vcpu-2| I120: DISKLIB-CHAIN : DiskChainUpdateContentID: old=0x5f55ed5e, new=0xfd1ff90b (2cd7ba3357f17a95d74c60c8fd1ff90b)
2018-05-15T10:56:22.153Z| vmx| I120: MainMem: Completed pending lazy prefault.
2018-05-15T10:56:22.195Z| vcpu-6| W110: GuestRpc: application toolbox-dnd, changing channel 65535 -> 2
2018-05-15T10:56:22.195Z| vcpu-6| I120: GuestRpc: Channel 2, guest application toolbox-dnd.
2018-05-15T10:56:22.195Z| vcpu-3| W110: GuestRpc: application toolbox, changing channel 65535 -> 0
2018-05-15T10:56:22.195Z| vcpu-3| I120: GuestRpc: Channel 0, guest application toolbox.
2018-05-15T10:56:24.041Z| vcpu-1| I120: TOOLS autoupgrade protocol version 2
2018-05-15T10:56:24.042Z| vcpu-1| I120: TOOLS Received tools.set.version rpc call, version = 10246.
2018-05-15T10:56:24.042Z| vcpu-1| I120: ToolsSetVersionWork did nothing; new tools version (10246) matches old Tools version
2018-05-15T10:56:24.046Z| vcpu-1| I120: Starting copy guest manifest.
2018-05-15T10:56:24.085Z| vcpu-2| I120: HGFileCopyCreateSessionCB: Successfully created the session.
2018-05-15T10:56:24.090Z| vcpu-2| I120: ToolsLoadManifestFileCB: Queuing a request to update the manifest information.
2018-05-15T10:56:24.103Z| Worker#0| I120: GetHostManifests: Extracting the manifest file.
2018-05-15T10:56:24.105Z| Worker#0| I120: GetHostManifests: Done extracting the manifest file.
2018-05-15T10:56:24.109Z| Worker#0| I120: ToolsVersionGetStatusWorkerThread: Tools status 3 derived from environment
2018-05-15T10:56:24.109Z| vmx| I120: ToolsUpdateManifestInfoWorkerThreadDone: Compared tools manifest from host and from the guest. Status = 3.
2018-05-15T10:56:24.109Z| vmx| I120: ToolsUpdateManifestInfoWorkerThreadDone: Updating the manifest info.
2018-05-15T10:56:24.109Z| vmx| I120: VMXVmdb_SetToolsVersionStatus: status value set to 'ok', 'current', install possible
2018-05-15T10:56:24.109Z| vmx| I120: TOOLS installed legacy version 10246, available legacy version 9541
2018-05-15T10:56:24.109Z| vmx| I120: TOOLS manifest update status is 3
2018-05-15T10:56:24.110Z| vmx| I120: TOOLS can be autoupgraded.
2018-05-15T10:56:24.110Z| vmx| I120: TOOLS Setting autoupgrade-checked TRUE.
2018-05-15T10:56:24.110Z| vmx| I120: RPT: Disabled. Skipped.
2018-05-15T10:56:28.528Z| vcpu-1| I120: TOOLS state change 4 returned status 1
2018-05-15T10:56:28.528Z| vcpu-1| I120: Vix: [4148225 mainDispatch.c:4210]: VMAutomationReportPowerStateChange: Reporting power state change (opcode=2, err=0).
Nothing obviously wrong in there, everything looks ok.
The only strange entry in there is: TOOLS installed legacy version 10246, available legacy version 9541
Is that VM perhaps configured with a HW version that does not correspond with the ESXi version on which the VM is running?
Or is the VM running on an ESXi version that does not officially support Windows 10?
On the power off/power on of the VM, to make sure did you do that on the VM level, and not on the guest OS level?
Is that VM in a cluster?
If yes, can you vMotion it to another ESXi node in the cluster?
Something drastic you could eventually try:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have some progress -
As i mentioned before, my script revert to a clean snapshot (VMWare tools is installed on the clean snapshot) and then tries to copy a file to the VM.
My script calls the following command:
1. Connect-VIServer $VIserver -User $vCenterUser -Password $vCenterPass -WarningAction SilentlyContinue
2. Get-Snapshot -VM $VMName -Name $Snapname
3. Copy-VMGuestFile -Source "$Source" -Destination "$Destination" -LocalToGuest -Force -VM $Machinename -GuestUser "$VMGuestUserID" -GuestPassword "$VMGuestPassword" -ToolsWaitSecs -10 -Verbose
When I run the script, it failes (with the error mentioned before).
But, if I run the script, without calling Get-Snapshot command, it succeeds to copy the file.
Do you have any ideas why calling Get-Snapshot, causes the copy to fail?
There are a couple of confusing points now.
At this stage I'm wondering if you can show us the actual script you are using, and not the condensed information you have been providing till now.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
My mistake, I forgot a step. after calling Get-Snapshot, I'm calling:
Set-VM -VM $VMName -Snapshot $snapshot -Confirm:$false
I would still like to see the complete script you are using?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
#connect VIserver:
Write-Host " Connecting to your server $VIserver"
$con = Connect-VIServer $VIserver -User $vCenterUser -Password $vCenterPass -WarningAction SilentlyContinue
$snapshot = Get-Snapshot -VM $VMName -Name $Snapname
Write-Host " Get the snapshot object to revert"
if ($snapshot -ne $null) {
$snap1 = Set-VM -VM $VMName -Snapshot $snapshot -Confirm:$false
if ($snap1 -ne $null){
Write-Host "Snapshot reverted to " $Snapname
}
else
{
Write-Host "Snapshot is not reverted " -ForegroundColor Red
}
}
Else {
Write-Host " Snapshot name given does not exist " -ForegroundColor Red
}
########################Code to copy script to VM #########################
Write-Host "Copying in process.....Data trasfering from $Source to $Destination " -ForegroundColor Green
-10 -Verbose
$sCopy = @{
Source = "$Source"
Destination = "$Destination"
LocalToGuest = $true
VM = $Machinename
GuestUser = "$VMGuestUserID"
GuestPassword = "$VMGuestPassword"
Verbose = $true
}
Copy-VMGuestFile @sCopy
Sorry for the confusion. I never used PowerCLI before. I'm new at this.
Thanks for your help so far.
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.
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Copy without doing the revert to snapshot - works.
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.
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference