it-twak
Goto Top

Copy-VMFile

Hey,

ich sitze jetzt an einem *.ps1 Script und würde gern folgendes realisieren:

eine Datei von meinem Host auf ein Gastbetriebssystem bringen.

Zum Einsatz kommt ein Windows Server 2012 R2 und Hyper-V.

Hier mein Code:


If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host "Running elevated..."
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}


[system.reflection.assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Sourcepath = [Microsoft.VisualBasic.interaction]::inputbox('Bitte Pfad der Sicherung angeben','*.bak an den SQL Server senden', '')

Copy-VMFile -VMName "!Installation_FS_SQL" -SourcePath $Sourcepath -DestinationPath "E:\copytest\" -CreateFullPath -FileSource Host


Bekomme folgenden Fehler:


Copy-VMFile : "!Installation_FS_SQL" konnte die Datei nicht kopieren (ID des virtuellen Computers: 589F7856-0EAB-48F1-B147-504404125A70).
"!Installation_FS_SQL" konnte keine Dateien in das Gastbetriebssystem kopieren: Mindestens ein Argument ist ungültig. (0x80070057). (ID des virtuellen Computers:
589F7856-0EAB-48F1-B147-504404125A70)
"!Installation_FS_SQL" konnte die Quelldatei "D:\TEST-FS\test\web.config" nicht in das Ziel "E:\copytest\" des Gastbetriebssystems kopieren: Mindestens ein Argument ist ungültig.
(0x80070057). (ID des virtuellen Computers: 589F7856-0EAB-48F1-B147-504404125A70)
An den Vorgang wurde ein ungültiger Parameter übergeben.
In D:\TEST-FS\Scripts\test.ps1:15 Zeichen:1
Copy-VMFile -VMName "!Installation_FS_SQL" -SourcePath $VMName -DestinationPath ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (Microsoft.Hyper...FileToGuestTask:VMCopyFileToGuestTask) [Copy-VMFile], VirtualizationOperationFailedException
FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.CopyVMFileCommand


Könnte mir da jemand weiterhelfen?

Es scheint als mag er die Variable nicht nehmen.. face-confused

Möchte aber gern die Angabe des Quellpfades so variabel wie möglich halten.

Gruß

t

Content-Key: 310903

Url: https://administrator.de/contentid/310903

Printed on: April 18, 2024 at 21:04 o'clock

Mitglied: 129813
129813 Jul 26, 2016 updated at 15:41:25 (UTC)
Goto Top
Hi.
D:\TEST-FS\
Is this as mapped drive ? If yes you have to take into account that if you run the script with different credentials than the current user, shares won't be mapped inside this session.
-DestinationPath "E:\copytest\"
Give the full destination path including the filename and try again.

Take a note: -SourcePath has no Wildcard support!

Regards
Member: IT-twak
IT-twak Jul 27, 2016 at 07:47:11 (UTC)
Goto Top
Hi,


Zitat von @129813:

Hi.
D:\TEST-FS\
Is this as mapped drive ? If yes you have to take into account that if you run the script with different credentials than the current user, shares won't be mapped inside this session.
-DestinationPath "E:\copytest\"
Give the full destination path including the filename and try again.

Take a note: -SourcePath has no Wildcard support!

Regards

D:\TEST-FS\ ist just my second Harddrive.

Do i have to give the full Destination Path? I am trying to copy individual *.bak-Files into this VM... so i dont know which file-name i should type in there.

Can this command handle with variables?

regards
Mitglied: 129813
129813 Jul 27, 2016 updated at 08:08:06 (UTC)
Goto Top
Getting only the file name part from a path is really easy with split-path
[system.reflection.assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
$Sourcepath = [Microsoft.VisualBasic.interaction]::inputbox('Bitte Pfad der Sicherung angeben','*.bak an den SQL Server senden', '')  

Copy-VMFile -VMName "!Installation_FS_SQL" -SourcePath $Sourcepath -DestinationPath "E:\copytest\$(Split-Path $Sourcepath -Leaf)" -CreateFullPath -FileSource Host  
Member: IT-twak
IT-twak Jul 27, 2016 at 08:27:09 (UTC)
Goto Top
Nice that work's!

Now the last Problem:

it seems that he can't handle the "-Sourcepath $Sourcepath" <-

Any ideas?

Thank's so far
Mitglied: 129813
Solution 129813 Jul 27, 2016 updated at 08:36:02 (UTC)
Goto Top
Like i said $sourcepath has to be a valid accessible path on the Hyper-V Host see the documentation!
And check it with Test-Path before using it!
Member: IT-twak
IT-twak Jul 27, 2016 at 08:46:24 (UTC)
Goto Top
Jesus that works! face-smile

Thanks alot!

I just tested it with C:\tmp\test.txt as $SourcePath and it worked!

Thank you man! face-smile

Have a nice day