VMware Cloud Community
tdubb123
Expert
Expert

powercli to download single .txt file from multiple vm folders

I need to download a txt file from multiple vm folders on multiple datastores. 

 

Is there a script to do this?

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership

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

Reply
0 Kudos
tdubb123
Expert
Expert

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

Reply
0 Kudos
tdubb123
Expert
Expert

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
}

Reply
0 Kudos
tdubb123
Expert
Expert

ok I got wrong variable. never mind. Thanks lucd

Reply
0 Kudos
tdubb123
Expert
Expert

is there way to modify this t grab only the vmsn file from my csv file of vms?

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
tdubb123
Expert
Expert

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

Reply
0 Kudos
LucD
Leadership
Leadership

You mean <Vm name>.vmsn?


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

Reply
0 Kudos
tdubb123
Expert
Expert

yes

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos