Search This Blog

Thursday, August 22, 2019

Script to Get VMX File



param ($VM,
$Destination = "$pwd",
[switch]$WhatIf = $False,
[switch]$Verbose = $False )

process
{

#check if VM = pipeline
if ($_){ $VM = $_ }

#use $pwd if no -Dest

#Get VMX path\file
$vmx = Get-VM $VM | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L="VmPathName"}

#Parse path
$vmxpath = $vmx.VmPathName.split("[")[1].split("]").trim()

#Get Datastore
$ds = get-datastore $vmxpath[0]

#Get Browse path for DIR
$vmstorepath = $ds.DatastoreBrowserPath + "\" +  ($vmxpath[1] -replace "/", "\")

Copy-DatastoreItem $vmstorepath -Verbose:$Verbose -Dest $Destination -Force -WhatIf:$WhatIf

$msg = "" | Select VM, Source, Dest, WhatIf
$msg.vm = $vm.name
$msg.Source = $vmstorepath
$msg.Dest = $Destination.trim() + "\" + ($vmxpath).split("/")[ $vmxpath.split("/").count -1 ]
$msg.WhatIf = $WhatIf
$msg

#$msg = ($Destination).trim() + "\" + ($vmxpath).split("/")[ $vmxpath.split("/").count -1 ] + $msg


}

No comments:

Post a Comment