VMware Cloud Community
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

copy a folder with it's content to a remote VM

Hi,

I need your help with a script that i am building.I need it to:`

# to find a VM and see if it is turned off or on.

# if it is off then turn it on and copy a folder with the content to the VM machine.

Regrades,

Michel Vaillancourt

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
jpsider
Expert
Expert
Jump to solution

You would put it before, Something like this:

$vmName = 'Target'

$folderName = 'C:\Folder'

Invoke-VMScript "del $folderName" -vm $vmName -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat"

Get-VM -Name $vmName | where{$_.PowerState -ne 'PoweredOn'} | Start-VM -Confirm:$false

sleep 30

Copy-VMGuestFile  -LocalToGuest -Source $folderName -Destination $folderName -Confirm:$false

You might need to play with the command following Invoke-vmscript

$cmd = "del $folderName"

Invoke-VMScript "$cmd" -vm $vmName -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat"

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

You mean something like this.

Note that you can replace the sleep with a loop that checks if the guest OS is started up completely.

$vmName = 'Target'

$folderName = 'C:\Folder'

Get-VM -Name $vmName | where{$_.PowerState -ne 'PoweredOn'} | Start-VM -Confirm:$false

sleep 30

Copy-VMGuestFile  -LocalToGuest -Source $folderName -Destination $folderName -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

Hi,

Thank's for your help. I'm getting the error that " WARNING: The guest OS for the virtual machine 'VM' is unknown. The operation may fail"

I have checked the name and it is VM..

Regards,

Michel v

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Destination parameter allows you to define the path inside the guest.

In the sample script I used the same path "C:\Folder" as the local source, but you can use a 2nd variable with another path.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

Hi,

It works but i need it to delete the content of the old folder ( in the destination location) and replace it with the new one.

Michel v

0 Kudos
jpsider
Expert
Expert
Jump to solution

‌how about adding:

Invoke-VMScript "del $myfolder" -vm $VM -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat" 

You may have to adjust the variable names And add the force flag on the delete command.

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

were do i put the script that you wrote?before the privies script or after?

michel

0 Kudos
jpsider
Expert
Expert
Jump to solution

You would put it before, Something like this:

$vmName = 'Target'

$folderName = 'C:\Folder'

Invoke-VMScript "del $folderName" -vm $vmName -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat"

Get-VM -Name $vmName | where{$_.PowerState -ne 'PoweredOn'} | Start-VM -Confirm:$false

sleep 30

Copy-VMGuestFile  -LocalToGuest -Source $folderName -Destination $folderName -Confirm:$false

You might need to play with the command following Invoke-vmscript

$cmd = "del $folderName"

Invoke-VMScript "$cmd" -vm $vmName -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType "bat"

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

ok,

And the resolute will be that the folder will copy it self from the target to the destination and the folder in the dest computer will be deleted and replaced with the new folder???

Regards,

Michel V

0 Kudos
jpsider
Expert
Expert
Jump to solution

Yes, its a rough script and could probably use some testing and clean up, but you have all the parts above.

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

ok, thank you very much.

Do you know a way to get all of the alarms that esxi can publish? i need the full list

michel

0 Kudos
jpsider
Expert
Expert
Jump to solution

I would open a new thread for that.  It makes it easier for other folks to search for similar topics

0 Kudos
michelvaillanco
Enthusiast
Enthusiast
Jump to solution

ok.

Thank you.

0 Kudos