I need to download a txt file from multiple vm folders on multiple datastores.
Is there a script to do this?
You could do something like this.
- the script copies the files to a Temp folder, but you can change the destination via the $tgtFolder variable
- note that the filename (in $txtFileName) is case sensitive
- if not all VM shall be used, the Get-VM at the beginning can be adapted
$txtFileName = 'Test.txt'
$tgtFolder = $env:Temp
Get-VM -PipelineVariable vm |
ForEach-Object -Process {
$dsName = ($vm.ExtensionData.Config.Files.VmPathName.Split())[0]
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$($vm.Name)/$txtFileName" -Destination "$($tgtFolder)\$($vm.Name)-$txtFileName"
Remove-PSDrive -Name DS -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc
what does
Get-VM -PipelineVariable vm
do ?
I want to get all a list of vms in csv file
Name |
vm1 |
vm2 |
The download the vmname.vmsn file
i tried this but it did not download the vmsn file
$txtFileName = 'vmname.vmsn'
$tgtFolder = $env:Temp
$vm = get-vm -name vmname
Get-VM $vm |
ForEach-Object -Process {
$dsName = ($vm.ExtensionData.Config.Files.VmPathName.Split())[0]
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$($vm.Name)/$txtFileName" -Destination "$($tgtFolder)\$($vm.Name)-$txtFileName"
Remove-PSDrive -Name DS -Confirm:$false
}
ok I got wrong variable. never mind. Thanks lucd
is there way to modify this t grab only the vmsn file from my csv file of vms?
Sure, something like this
$txtFileName = 'vmname.vmsn'
$tgtFolder = $env:Temp
Import-Csv -Path .\vmnames.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
Get-VM -Name $row.Name -PipelineVariable vm |
ForEach-Object -Process {
$dsName = ($vm.ExtensionData.Config.Files.VmPathName.Split())[0]
$dsName = $dsName.Trim('[]')
$ds = Get-Datastore -Name $dsName
New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null
Copy-DatastoreItem -Item "DS:$($vm.Name)/$txtFileName" -Destination "$($tgtFolder)\$($vm.Name)-$txtFileName"
Remove-PSDrive -Name DS -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
for the vmsn file name I tried using
$vm = Get-VM -Name $vmName
$snap = Get-Snapshot -VM $vm -Name $snapName
($vm.ExtensionData.Layout.Snapshot | where{$_.Key -eq $snap.ExtensionData.Snapshot}).SnapShotFile | where{$_ -match ".vmsn$"}
how do I trim the output to show only the vmsn file and not the full datastore path/vmsn file
thanks
You mean <Vm name>.vmsn?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes
Try with
Where-Object { $_.Key -eq $snap.ExtensionData.Snapshot }).SnapShotFile |
Where-Object { $_ -match ".vmsn$"} |
%{$_.Split('/')[-1]}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference