PowerCLI: Export VM Guest Logs files to Local Computer

March 14, 2017

no comments

Last week I needed to export vmware.log files.

In the normal way you can read using putty client or download log using Datastore Browser.

But you also can download logs using PowerCLI.

to do it, we using cmdlet Copy-DatastoreItem 

cmdlet Syntax:

Copy-DatastoreItem [-Item] <Object[]> [[-Destination] <Object>] [-Force] [-PassThru] [-Recurse] [-WhatIf] [-Confirm] [<CommonParameters>]

 

To find the location of a virtual machine run this script:

get-vm MyVirtualMachine| Get-Datastore |Select name

 

Connect to Datastore using New-PSDrive cmdlet and change location to VM folder using Set-Location cmdlet.

New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root "\"
Set-Location ds:\
cd $vm

To display list log files, simple run ls *.log

 

 

 

At this point we can begin copy files from Datastore to local computer:

Copy-DatastoreItem -Item *.log -Destination $target

you can see the download datastore item progress bar;

 

Once the download done, you can found the files at the location we set as target;

 

SCRIPT All-in-One:

# This Script export virtual machine logs from a Datastore to local computer.
# Script Author: Haim Cohen 2017
# Version 1.0

$vm = get-vm "VM NAME HERE"
$target = New-Item -ItemType Directory -Force -Path c:\VM_Logs\$vm
$datastore = get-vm $vm | Get-Datastore
New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root "\"
Set-Location ds:\
cd $vm
Copy-DatastoreItem -Item *.log -Destination $target
set-Location C:
Remove-PSDrive -Name ds -Confirm:$false

 

Haim Cohen 2017

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*