VMware Cloud Community
gcarmichael
Contributor
Contributor
Jump to solution

Copying vmware.log files from ESXi

All,

Need a bit of help here. We have been asked to retain the vmware*.log files found in each VM's folder. Using the viToolkitExtensions, I can get to the files in each directory, but I'm having trouble getting them copied off to my Windows server so they can be backed up. It was suggested I use Copy-TkeDatastoreFile, but I've run into a wall getting it to work. Here's what I have so far, after connecting to VC.

$x = get-vm VMNAME | get-view | % { $_.config.files.vmpathname }

$y = Extract-TkeDatastorePathElements $x

$z = get-datastore $y.datastore | get-tkedatastorefile | where { $_.path -like "vmwarelog" }

I can now iterate through each log found in the folder, but I can't get Copy-tkeDatastoreFile to work to copy each file to a destination on the Windows server where this script will run, i.e. D:\Data\logs. Any ideas as to how I finish this off?

Gary

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Here's a transcript of a session that worked for me. I added -noSize -noType and -noModification to Get-TkeDatastoreFile because it speeds things up quite a bit.

C:\> get-datastore

Name                               FreeSpaceMB      CapacityMB
----                               -----------      ----------
Storage1                                 48292          225536

C:\> get-datastore Storage1 | Get-TkeDatastoreFile -noSize -noType -noModification | Where { $_.Path -like "*.log" }

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-7.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-8.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-9.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-10.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-11.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-12.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware.log                                  0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-1...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-2...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-3...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-4...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] 2k8 64 bit/vmware...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] 2k8 64 bit/vmware...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Openfiler/vmware.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 4.0 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Demo VM 1/vmware.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Stat VM/vmware.log                                 0 1/1/0001 12:00:00 AM           Storage1                      


C:\> $allLogs = get-datastore Storage1 | Get-TkeDatastoreFile -noSize -noType -noModification | Where { $_.Path -like "*.log" }
C:\> $z = $allLogs[40]
C:\> $z

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      


C:\> $z.Path
[Storage1] win2k3-64-vi4/vmware-8.log
C:\> $datacenter = get-vm win2k3-64-vi4 | get-datacenter
Get-VM : 2/20/2009 8:14:50 AM    Get-VM        VM with name 'win2k3-64-vi4' not found, using the specified filter(s).    
At line:1 char:21
+ $datacenter = get-vm <<<<  win2k3-64-vi4 | get-datacenter
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_ContainerCmdletBase_ObjectNotFoundByName,VMware.VimAutomation.Commands.GetVM
 
C:\> get-vm

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
Stat VM              PoweredOff 1        256        
Win 2003 SP2         PoweredOn  1        1024       
Ubuntu 8.04          PoweredOff 2        512        
2k8 64 bit -- Los... PoweredOff 1        2048       
Win2k3-64bit-VC4     PoweredOn  1        1024       
Openfiler NSA 30Gb   PoweredOff 1        512        
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 3.5 Master I... PoweredOff 1        1024       


C:\> get-vm Win2k3-* | get-datacenter

Name                                     Id                                      
----                                     --                                      
ha-datacenter                            Datacenter-ha-datacenter                


C:\> $datacenter = get-vm Win2k3-* | get-datacenter
C:\> $z

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      


C:\> Copy-TkeDatastoreFile -source $z.Path -destination c:\temp.log -sourceDatacenter $datacenter

Notice that I use $z.Path as the source, not the entire $z argument.

Also, local files refer to files that are local to ESX, so they begin with /.

View solution in original post

0 Kudos
11 Replies
admin
Immortal
Immortal
Jump to solution

Could you post the command you're trying to use? Note that you need to supply Copy-TkeDatastoreFile with a datacenter argument in this case (don't ask me why, it's in the underlying API).

You can get that by just running $vmInQuestion | Get-Datacenter and send that in as -sourceDatacenter.

0 Kudos
gcarmichael
Contributor
Contributor
Jump to solution

Yeah, I ran into that the first time I ran the command and the error told me I needed that parameter, so I ran $datacenter = get-datacenter DATACENTERNAME and supplied that as -sourceDatacenter. For testing purposes, I'm modifying the $z variable so it just pulls the vmware.log file. The command I'm trying is:

Copy-TkeDatastoreFile -source $z -sourcedatacenter $datacenter -destination "c:\test"

I get the following error.

Exception calling "OpenRead" with "1" argument(s): "The remote server returned an error: (404) Not Found."

At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\viToolkitExtensions\viToolkitExtensions.psm1:1119 char:37

+ $sourceStream = $sourceWC.OpenRead &lt;&lt;&lt;&lt; ($sourceUrl)

+ CategoryInfo : NotSpecified: (Smiley Happy [], ParentContainsErrorRecordException

+ FullyQualifiedErrorId : DotNetMethodException

Looking through the Copy-TkeDatastoreFile function, it looks like it it looking for a leading "/" to determine if the copy is going to a local file, which won't work with Windows:

$destinationIsLocalPath = $destination[0] -eq "/"

Gary

0 Kudos
admin
Immortal
Immortal
Jump to solution

Here's a transcript of a session that worked for me. I added -noSize -noType and -noModification to Get-TkeDatastoreFile because it speeds things up quite a bit.

C:\> get-datastore

Name                               FreeSpaceMB      CapacityMB
----                               -----------      ----------
Storage1                                 48292          225536

C:\> get-datastore Storage1 | Get-TkeDatastoreFile -noSize -noType -noModification | Where { $_.Path -like "*.log" }

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] esxconsole-49341a...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-7.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-8.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-9.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-10.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-11.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware-12.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Win2k3/vmware.log                                  0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-1...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-2...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-3...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware-4...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Win2k3/vmware.log                                0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] t/Ubuntu 8.04/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Ubuntu 8.04/vmwar...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] 2k8 64 bit/vmware...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] 2k8 64 bit/vmware...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 3.5 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Openfiler/vmware.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] ESXi 4.0 Master I...                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Demo VM 1/vmware.log                               0 1/1/0001 12:00:00 AM           Storage1                      
[Storage1] Stat VM/vmware.log                                 0 1/1/0001 12:00:00 AM           Storage1                      


C:\> $allLogs = get-datastore Storage1 | Get-TkeDatastoreFile -noSize -noType -noModification | Where { $_.Path -like "*.log" }
C:\> $z = $allLogs[40]
C:\> $z

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      


C:\> $z.Path
[Storage1] win2k3-64-vi4/vmware-8.log
C:\> $datacenter = get-vm win2k3-64-vi4 | get-datacenter
Get-VM : 2/20/2009 8:14:50 AM    Get-VM        VM with name 'win2k3-64-vi4' not found, using the specified filter(s).    
At line:1 char:21
+ $datacenter = get-vm <<<<  win2k3-64-vi4 | get-datacenter
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_ContainerCmdletBase_ObjectNotFoundByName,VMware.VimAutomation.Commands.GetVM
 
C:\> get-vm

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 4.0 Master I... PoweredOff 1        2048       
ESXi 3.5 Master I... PoweredOff 1        1024       
Stat VM              PoweredOff 1        256        
Win 2003 SP2         PoweredOn  1        1024       
Ubuntu 8.04          PoweredOff 2        512        
2k8 64 bit -- Los... PoweredOff 1        2048       
Win2k3-64bit-VC4     PoweredOn  1        1024       
Openfiler NSA 30Gb   PoweredOff 1        512        
ESXi 3.5 Master I... PoweredOff 1        1024       
ESXi 3.5 Master I... PoweredOff 1        1024       


C:\> get-vm Win2k3-* | get-datacenter

Name                                     Id                                      
----                                     --                                      
ha-datacenter                            Datacenter-ha-datacenter                


C:\> $datacenter = get-vm Win2k3-* | get-datacenter
C:\> $z

Path                                                       Size Modification                   Datastore                     
----                                                       ---- ------------                   ---------                     
[Storage1] win2k3-64-vi4/vmw...                               0 1/1/0001 12:00:00 AM           Storage1                      


C:\> Copy-TkeDatastoreFile -source $z.Path -destination c:\temp.log -sourceDatacenter $datacenter

Notice that I use $z.Path as the source, not the entire $z argument.

Also, local files refer to files that are local to ESX, so they begin with /.

0 Kudos
gcarmichael
Contributor
Contributor
Jump to solution

Well, I'm not versed well enough to say why yours works, but it does. It's the difference in the way you pull the filename, as I've run tests back and forth and that's what breaks mine for some reason. In any case, it works and I am VERY grateful for your assistance with this Carter!! Thank you!

Gary

0 Kudos
admin
Immortal
Immortal
Jump to solution

Glad you like it.

Let me know if you have any troubles with Copy-TkeDatastoreFile freezing after a lot of copies. I think I worked all those problems out but I don't think I ever copied as many files as you're planning to do.

0 Kudos
gcarmichael
Contributor
Contributor
Jump to solution

Will do. Thanks!

0 Kudos
halr9000
Commander
Commander
Jump to solution

This particular use case, reading VM log files, needs a cmdlet (or advanced function in the TKE).






[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
admin
Immortal
Immortal
Jump to solution

Sounds good, care to write it? I'd be happy to put it in or give you commit access.

0 Kudos
gcarmichael
Contributor
Contributor
Jump to solution

Well, I've got 95% of the script working, but I've run into one issue that I haven't been able to overcome.

The script is running through about 110 VMs to copy the logs off, and I haven't seen an issue of freezing. What I am seeing is the copy-tkedatastorefile command erroring when it tries to copy the log file that is open at the time. So I'll just strip out vmware.log out of the list of logs since that should be the active log...but it's not in all cases. I have a small subset of VMs that are using vmware-##.log as their active log, which immediately stops the script. I've tried using "-Erroraction SilentlyContinue" but it doesn't seem to work with this particular error. Any ideas of what I can do to let the script ignore any of the files that can't be copied? I've attached the full script if it will help.

The error text is:

Exception calling "OpenRead" with "1" argument(s): "The remote server returned an error: (500) Internal Server Error."

At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\viToolkitExtensions\viToolkitExtensions.psm1:1119 char:37

+ $sourceStream = $sourceWC.OpenRead &lt;&lt;&lt;&lt; ($sourceUrl)

+ CategoryInfo : NotSpecified: (Smiley Happy [], ParentContainsErrorRecordException

+ FullyQualifiedErrorId : DotNetMethodException

Gary

0 Kudos
admin
Immortal
Immortal
Jump to solution

Can you try this before you start copying files:

$errorActionPreference = "SilentlyContinue"

I'm not sure that the switch you tried actually had any effect.

0 Kudos
gcarmichael
Contributor
Contributor
Jump to solution

Unfortunately, I still receive the error, even after specifying that variable immediately before the copy command.

Gary

0 Kudos