Export-VM to network path

Feb 19, 2010 at 10:29 AM
Edited Feb 19, 2010 at 10:30 AM

First of all thanks for Hyper-v PS library! (sorry for my English - I'm from Russia))

There are error after running a cmdlet:

export-vm -vm MyVM -path \\MyFileServer\Share -copystate -wait -force

Exporting the VM kmrvs-virt-02

Test-WMIResult : Attempt to export MyVM to \\MyFileServer\Share failed. Failed to copy file from 'C:\VirtualMachines\MyVM.vhd' to '\\MyFileServer\Share\MyVM\Virtual Hard Disks\MyVM.vhd': General access denied error (0x80070005) At C:\program files\modules\hyperv\VM.ps1:27 char:113
+ if ( ($VSMgtSvc.ExportVirtualSystem($VM.__path,($CopyState.Ispresent),$path) | Test-wmiResult <<<< -wait:($wait -or $preserve) -JobWaitText ($lstr_ExportOfVm -f $vm.elementName)`
 + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
 + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-WMIResult

I check permissions and share security and virtual machine is stopped - seems everything is ok.

I can run successful cmdlet from PowerShell:

copy "C:\VirtualMachines\MyVM.vhd" "\\MyFileServer\Share\MyVM.vhd"

And it working. But Export-VM says that access denied..

Also I try mount network drive with authorization such as "net use b: \\MyFileServer\share passwd /user:hyperv_backup_user" and Export-VM on this network drive but it's fail with same error.

What I'm doing wrong?

Mar 11, 2010 at 3:36 PM

I suppose the actual export is done by one of the Hyper-V services and which probably have no permission to access your file share.

Mar 18, 2010 at 12:54 AM

I would suggest reviewing Ben Armstrong's blog on 'Exporting / Importing from a network share [Hyper-V]':

http://blogs.msdn.com/virtual_pc_guy/archive/2010/03/15/exporting-to-importing-from-a-network-share-hyper-v.aspx

Ben goes into detail what perms need to be established when exporting to a network share.

Good luck.

Cheers,
John

Mar 18, 2010 at 9:56 PM

for me, export-vm works well on a remote share without any additional permission.

first, before running export-vm, I do invoke-shutdown the VM; I'm very unsure the export function can work on alive machines;
then, from my controller machine (another R2 machine) I do an export to \\server\f$\hyper-v\exports, and it happens to work fine.

I must add that both server and controller are in the same domain, which probably allows me to do that without pain.
Cheers,
Laurent

Sep 22, 2010 at 8:41 PM
Edited Sep 22, 2010 at 8:43 PM

I've got the same issue.

 

I can export to a share using hyper-v mgr (i.e. permissions are setup), but using the pslibrary, I get the general access denied errors.  I tried using the audit log to see which user is authenticating on the target server but there is no record.

 

I can run the command from the host, but not remotely from a different machine.

 

Oct 15, 2010 at 3:40 PM

Did you try adding the "machine account" full access to the network share?

 

 

Feb 28, 2011 at 2:04 PM

 

This is a piece of code that I wrote that I believe does what your looking for, it does the following:

1. Shuts down each VM
2. Takes a snapshot of the VM and waits 30 seconds
3. Starts the VM
4. Copies the Snapshot off to our DR Hyper-V server
5. Shuts down the VM and tells it to remove the snapshots (merge them)
6. Allows the snapshot to merge wiats 90 seconds
7. Starts the VM
8. Writes each step to Copy.txt 
9. moves on to the next VM

10. After all VM have been copied, mails the copy.txt to our support group. 

VM's are located in D:\VM-Config
VM Drives are located in D:\VM-Drive 
Scripts are located in D:\VM-Backup-Script 

I run this as a scheduled task using a cmd file. 
The file must be run as a user that has Admin rights on both Hyper-V servers.

CMD Code:

PowerShell "D:\VM-Backup-Script\new-backup-multi.PS1" /wait

Power Shell Code: (new-backup-multi.PS1)

Set-ExecutionPolicy unrestricted -force
import-module "C:\Program Files\HyperV_Powershell\hyperv.psd1"
Remove-Item D:\VM-Backup-Script\copy.txt
$arrComps = "Server1", "Server2", "Server3"
Start-Transcript D:\VM-Backup-Script\copy.txt
foreach ($arrComps in $arrComps)
{
invoke-vmshutdown $arrComps -force
new-vmsnapshot $arrComps -force -waitstart-sleep -s 30
Start-VM $arrComps -HeartBeatTimeOut 300

$date = Get-Date -format yyyy-MM-dd
New-Item \\phys-hvb\d$\VM-Drives\$date -type directory -force
Copy-Item D:\VM-Drives\$arrComps\*.vhd \\Remote-Server\d$\VM-Drives\$date\
select-vmsnapshot $arrComps | remove-vmsnapshot -wait -tree -force
invoke-vmshutdown $arrComps -force
start-sleep -s 90
Start-VM $arrComps -HeartBeatTimeOut 300
}
Stop-Transcript

$fileData = (Get-Content "D:\VM-Backup-Script\copy.txt" | out-string)
$file = "D:\VM-Backup-Script\copy.txt"
$smtpServer = "your SMTP relay Server"
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "Your-Server@mail.com"
$msg.To.Add("Recipient@mail.com")
$msg.Subject = "Hyper-V Backup Results"
$msg.Body = $fileData
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

Oct 12, 2011 at 8:44 AM

I also have such kind of trouble, the difference is: I try to export VM from Hyper-V cluster (Windows Server 2008 R2) to remote share on Samba. Export job started from user, who have all rigths on this shared folder, but I can't write vhd-file from export. The first action, what my script do on remote shared folder is to rename\remove\create folders for this export. And this actions executed successfully, but only the export cmdlet.

I think, if this cmdlet (Export-VM) has such parameter as -Credential (like Move-Item cmdlet), which transmit credentials, it will works.

Sorry for my bed English, I'm from Ukraine.

Oct 12, 2011 at 11:32 AM

To yeah

First of all, you have to read this http://support.microsoft.com/kb/2008849 and this http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/03/15/exporting-to-importing-from-a-network-share-hyper-v.aspx

If your Hyper-V and Fileshare are in one domain, you may have luck. 

In my case I have no variants, only export to local path and after then - copy to network share.

In russian: 

Кароч, если оба сервера в одном домене, то пройди по второй ссылке и расставь права, как там описано, хотя и в первой тоже самое %)

В моем случае это не пройдёт, у меня файловая шара на Самбе и не в домене. Только экспортировать на локальный диск, а потом копировать на шару.

Aug 5, 2013 at 8:18 PM
Edited Aug 5, 2013 at 8:33 PM
"Dlute," this is WONDERFUL code, but has a number of errors in it.
At least one place is "munged together," when it should have a space between 2 commands.
Apparently, powershell vm-library cmdlets act differently depending on server version (i.e., if my VM host is 2008 R2 vs. 2012 [mine is 2012 server]) and depnding on if you installed/loaded/imported Gold/R2 vs. Stable SP1? (I did pull down the latest, and it appears to have some holes that existed back in SP1 and before).

Apparently, invoke-shutdown doesn't always work, but there is an alternative command "stop-vm" - not sure of the differences. Is "invoke-shutdown just running "stop-vm?"

A couple places, I think you might have "piped" one part of a line to another, but since a space (or 'pipe character') is missing, it's hard to know for sure.
Also, some of the VM library cmdlets apparently want "vm-command -VM actual-vm-name" and others want "vm-command -Name actual-vm-name" - there's a lot of inconsistency here, as well as the fact that some cmdlets don't want any added modifiers (i.e., start-vm vm-name), or don't give any error messages if they fail (possibly in event logs, but no direct messages such as "Hey, dude, I failed!")

Various items also appear that they may depend on version of Integration Services and/or underlying Guest o/s; i.e., W2K3 and XP seem to have some oddities that aren't handled exactly the same vs. 2008 / 2008 R2 / 2012; and, I'm guessing, there are some dependencies & oddities based upon version of Powershell?

The Gold/R2 release of the VM cmdlet library appears to have some fixes UN-DONE from the SP1 release. I guess the SP1 may be the more stable release?
Because... some things had comments in various news articles saying, "Oh, this is FIXED in SP1." Well, if that's true, then why is it BROKEN in the version that is NEWER than SP1? - all FIXES for SP1 should be included/rolled-up into R2 (and maybe the are, and I'm just hallucinating).

In server 2012, after doing "import-module "C:\Program Files\modules\HyperV\hyperv.psd1"," various VM-based cmdlets fail (like invoke-shutdown, start-vm, etc.), UNTIL you exit and re-enter Powershell UI - once you do that, then it resets/fixes some things again (temporarily). When the "start-vm" or "invoke-shutdown" do not work, they do not always give errors, just return to the command line, and then you are left wondering: "Did it work?"

Here is one place you had badly messed up in your script:
"invoke-vmshutdown $arrComps -force
new-vmsnapshot $arrComps -force -waitstart-sleep -s 30
Start-VM $arrComps -HeartBeatTimeOut 300"

Apparently, "-waitstart-sleep" is not a command and needs either a pipe character and/or a carriage return after "-wait" [carriage-return] then "start-sleep -s 30" (or was there a 'pipe' character between -wait and start-sleep?)

Personally, all I want to do is: 1) Stop a vm, 2) Export the vm (don't care about 'snapshot' - I guess someone can tell me why that is so much better than just exporting the stopped vm? - aside from the fact that snapshot is generally quick and I can restart my vm, while the snapshot can then be copied later; vs. waiting for the export to finish before being able to start my vm back running again).

Still, via your script there are at least TWO shutdowns, since you come back and remove the snapshot later; would be nice to have only ONE shutdown. In many case, TWO separate shutdowns would be more disruptive than one long outage to export the VM.